Running trained tensorflow model in C++

Viewed 21108

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++.

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.

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 is the size of the image

2 Answers

You could use the C++ API as shown in the previous answer, nevertheless, compiling with the TensorFlow C++ API can be a headache. I recommend you using cppflow which a simple and easy to use wrapper to de C API. It allows you to feed data to the network as C++ vectors:

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>();

You can use this wrapper without installing full TensorFlow, you just need to download the .so of the C API.

Related