在Tensorflow中创建模型后,如何将图像传递给模型进行分类

时间:2016-03-29 03:57:11

标签: tensorflow

我使用Tensorflow创建了模型

Initialized
Minibatch loss at step 0 : 3.51275
Minibatch accuracy: 6.2%
Validation accuracy: 12.8%
Minibatch loss at step 50 : 1.48703
Minibatch accuracy: 43.8%
Validation accuracy: 50.4%
Minibatch loss at step 100 : 1.04377
Minibatch accuracy: 68.8%
Validation accuracy: 67.4%
Minibatch loss at step 150 : 0.601682
Minibatch accuracy: 68.8%
Validation accuracy: 73.0%
Minibatch loss at step 200 : 0.898649
Minibatch accuracy: 75.0%
Validation accuracy: 77.8%
Minibatch loss at step 250 : 1.3637
Minibatch accuracy: 56.2%
Validation accuracy: 75.4%
Minibatch loss at step 300 : 1.41968
Minibatch accuracy: 62.5%
Validation accuracy: 76.0%
Minibatch loss at step 350 : 0.300648
Minibatch accuracy: 81.2%
Validation accuracy: 80.2%
Minibatch loss at step 400 : 1.32092
Minibatch accuracy: 56.2%
Validation accuracy: 80.4%
Minibatch loss at step 450 : 0.556701
Minibatch accuracy: 81.2%
Validation accuracy: 79.4%
Minibatch loss at step 500 : 1.65595
Minibatch accuracy: 43.8%
Validation accuracy: 79.6%
Minibatch loss at step 550 : 1.06995
Minibatch accuracy: 75.0%
Validation accuracy: 81.2%
Minibatch loss at step 600 : 0.223684
Minibatch accuracy: 100.0%
Validation accuracy: 82.3%
Minibatch loss at step 650 : 0.619602
Minibatch accuracy: 87.5%
Validation accuracy: 81.8%
Minibatch loss at step 700 : 0.812091
Minibatch accuracy: 75.0%
Validation accuracy: 82.4%
Minibatch loss at step 750 : 0.276302
Minibatch accuracy: 87.5%
Validation accuracy: 82.3%
Minibatch loss at step 800 : 0.450241
Minibatch accuracy: 81.2%
Validation accuracy: 82.3%
Minibatch loss at step 850 : 0.137139
Minibatch accuracy: 93.8%
Validation accuracy: 82.3%
Minibatch loss at step 900 : 0.52664
Minibatch accuracy: 75.0%
Validation accuracy: 82.2%
Minibatch loss at step 950 : 0.623835
Minibatch accuracy: 87.5%
Validation accuracy: 82.1%
Minibatch loss at step 1000 : 0.243114
Minibatch accuracy: 93.8%
Validation accuracy: 82.9%
Test accuracy: 90.0%

这是我在完成所有迭代后获得的准确度。我想保存这个模型,以便我可以传递一个随机图像并显示我的模型输出(分类)给定图像。

在caffe中,我们用来创建.caffemodel文件然后用于在命令行中传递图像并用于显示图像最有可能的前5个可能对象,我想在Tensorflow中执行类似的操作但不知道如何这样做。

任何帮助都将不胜感激。

修改

在mrry的建议之后,我尝试寻找tf.train.saver()obejct,看它是如何工作的。

这是在网站上写的用于存储变量的内容:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add an op to initialize the variables.
init_op = tf.initialize_all_variables()

# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, initialize the variables, do some work, save the
# variables to disk.

with tf.Session() as sess:
  sess.run(init_op)
  # Do some work with the model.
  ..
  # Save the variables to disk.
  save_path = saver.save(sess, "/tmp/model.ckpt")
  print("Model saved in file: %s" % save_path)

用于恢复:

# Create some variables.
v1 = tf.Variable(..., name="v1")
v2 = tf.Variable(..., name="v2")
...
# Add ops to save and restore all the variables.
saver = tf.train.Saver()

# Later, launch the model, use the saver to restore variables from disk, and
# do some work with the model.
with tf.Session() as sess:
  # Restore variables from disk.
  saver.restore(sess, "/tmp/model.ckpt")
  print("Model restored.")
  # Do some work with the model
  ...

如何将图像传递给模型? ,它写在评论中,对模型做了一些工作,但究竟应该如何将我的图像传递给模型。

1 个答案:

答案 0 :(得分:2)

您可以使用TensorFlow的 Feed 机制重复使用经过培训的网络进行评估。通常,您将拥有一个名为input的张量,其包含一批图像,并作为模型第一层的输入。我们还假设您有一个名为logits的张量,它是网络最后一层的输出(在传递给损失函数之前)。

通过将logits传递给tf.nn.softmax()操作符后跟tf.nn.top_k()操作,您可以获得前五个最可能的类。生成的程序如下所示:

input = ...  # batch_size x height x width x channels tensor
# [...rest of network defined as a function of `input`...]
logits = ...

predictions = tf.nn.top_k(tf.nn.softmax(logits), k=5)

eval_image = ...  # 1 x height x width x channels NumPy array.
                  # NOTE: you may need to reshape this to match `input`.

predicted_classes = sess.run(predictions, feed_dict={input: eval_image})
print predicted_classes