在C ++中运行经过训练的张量流模型 [英] Running trained tensorflow model in C++

查看:50
本文介绍了在C ++中运行经过训练的张量流模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用tensorflow在python中训练了图像分类网络.训练后的模型另存为 .pb .现在,我想测试模型,我需要用C ++来完成.

I have trained an image classification network in python using tensorflow. The trained model was saved as a .pb. Now, I want to test the model, I need this to be done in C++.

我在处理和处理数据时使用了 numpy .在训练阶段,图像以numpy数组的形式传入.图像被拉伸为一维数组,并且类别标签位于该数组的前面.

I had used numpy in manipulating and handling data. During training phase the image is passed in as a numpy array. The image is stretched out as a 1D array and the class label is prepended to this array.

我对在C ++中运行模型时如何传递图像数据感到困惑,因为 numpy 对我不可用.我使用 numpy 操作来处理和处理数据.如果必须在C ++中执行数据,则应以哪种格式传递数据.

I'm confused as to how to pass the image data while running the model in C++, where numpy isn't available to me. I use numpy operations to manipulate and handle the data. In what format should I pass in the data if I have to execute it in C++.

下面是我训练和保存模型的方式

Below is how I train and save my model

def trainModel(data):
    global_step = tf.Variable(0, name='global_step', trainable=False)
    X, y,keep_prob = modelInputs((741, 620, 1),4)
    logits = cnnModel(X,keep_prob)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y), name="cost")
    optimizer = tf.train.AdamOptimizer(.0001, name='Adam').minimize(cost)
    prediction = tf.argmax(logits, 1, name="prediction")
    correct_pred = tf.equal(prediction, tf.argmax(y, 1), name="correct_pred")
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32), name='accuracy')
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()
        batch_size = 30
        for e in range(11):
            batch_x, batch_y = data.next_batch(batch_size)
            batch_y = batch_y.astype('int32')
            x = np.reshape(batch_x, [batch_size, 741, 620, 1])
            labels = np.zeros(shape=(batch_size,4))
            labels[np.arange(len(labels)),batch_y]=1
            sess.run(optimizer, feed_dict={X: x, y: labels,keep_prob:0.5})
            global_step.assign(e).eval()
        saver.save(sess, './my_test_model',global_step=global_step)

* 741x620是图像的大小

*741x620 is the size of the image

推荐答案

在C ++中使用图形的说明可以在此处.

Instructions for using a graph in C++ can be found here.

以下是一些代码,可以将您的图片用作输入:

Here is some code to use your image as input:

tensorflow::Tensor keep_prob = tensorflow::Tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape());
keep_prob.scalar<float>()() = 1.0;

tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({1,height,width,depth}));
auto input_tensor_mapped = input_tensor.tensor<float, 4>();
const float * source_data = (float*) img.data;  // here img is an opencv image, but if it's just a float array this code is very easy to adapt
// copying the image data into the corresponding tensor
for (int y = 0; y < height; ++y) {
    const float* source_row = source_data + (y * width * depth);
    for (int x = 0; x < width; ++x) {
        const float* source_pixel = source_row + (x * depth);
        for (int c = 0; c < depth; ++c) {
            const float* source_value = source_pixel + c;
            input_tensor_mapped(0, y, x, c) = *source_value;
        }
    }
}
std::vector<tensorflow::Tensor> finalOutput;

tensorflow::Status run_status = this->tf_session->Run({{InputName,input_tensor}, 
                                                       {dropoutPlaceHolderName, keep_prob}},
                                                      {OutputName},
                                                      {},
                                                      &finalOutput);

这篇关于在C ++中运行经过训练的张量流模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆