【发布时间】:2017-12-15 02:15:17
【问题描述】:
我已经使用 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)
*741x620 是图片的大小
【问题讨论】:
-
Tensorflow 确实可以轻松构建如此复杂的网络,您甚至不知道自己拥有什么。您计划如何从 C++ 运行您的 NN?手卷网络或图书馆?注意:图书馆推荐在这里是题外话
-
@MSalters 我打算像this 那样做。我不确定解决方案是否可能涉及图书馆推荐。我想用 C++ 调用模型,但是当我无法访问 numpy 时,我不知道如何传递数据。
-
好的,这很重要。你打算仍然使用 tensorflow。这留下了如何将输入插入到 tensorflow 中的问题。
-
@MSalters 完全正确。
标签: c++ tensorflow