在C++中运行训练的张量流模型

dpk*_*dpk 8 c++ tensorflow

我使用tensorflow在python中训练了一个图像分类网络.训练有素的模型保存为.pb.现在,我想测试模型,我需要在C++中完成.

我用过numpy操纵和处理数据.在训练阶段,图像作为numpy数组传递.图像作为一维数组延伸,类标签前置于此数组.

我很困惑如何在C++中运行模型时传递图像数据numpy,我无法使用.我使用numpy操作来操纵和处理数据.如果我必须在C++中执行它,我应该以什么格式传入数据.

以下是我训练和保存模型的方法

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)
Run Code Online (Sandbox Code Playgroud)

*741x620是图像的大小

gde*_*lab 9

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

以下是一些使用您的图像作为输入的代码:

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);
Run Code Online (Sandbox Code Playgroud)


Ser*_*rdo 7

您可以使用上一个答案中所示的 C++ API,但是,使用 TensorFlow C++ API 进行编译可能会令人头疼。我建议您使用cppflow,它是一个简单易用的 de C API 包装器。它允许您将数据作为 C++ 向量提供给网络:

Model m("mymodel.pb");
m.restore("./my_test_model");

auto X = new Tensor(m, "X");
auto y = new Tensor(m, "y");
auto keep_prob = new Tensor(m, "keep_prob");
auto result = new Tensor(m, "prediction");

std::vector<float> xdata, ydata;
// Fill the vectors with data
X->set_data(xdata);
y->set_data(ydata);

m.run({X,y,keep_prob}, result);

std::vector<float> myresult = result->get_data<float>();
Run Code Online (Sandbox Code Playgroud)

您可以在不安装完整 TensorFlow 的情况下使用此包装器,您只需要下载 C API 的 .so。