我只是按照本教程使用 tensorflow 轻松训练图像分类器:
https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/
我现在在我的 Ubuntu 笔记本电脑上有一个 Docker box,我设法训练了图像分类器,它工作正常。现在我想对一批新图像进行分类并在我的笔记本电脑上存储结果(即概率)。
我的目标是在我的笔记本电脑上将以下结果保存在如下“output.csv”文件中:
pic1 0.95 0.05
pic2 0.94 0.06
pic3 0.97 0.03
pic4 0.09 0.91
我将新图像放在“测试”文件夹中,然后运行以下 python 代码,尝试写入输出文件。
我在 Docker 终端中运行:
python /tf_files/label_batch_images.py /tf_files/tested
label_batch_images.py 的代码是:
import tensorflow as tf, sys
import glob
folder_path = sys.argv[1]
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("/tf_files/retrained_labels.txt")]
# Unpersists graph from file
with tf.gfile.FastGFile("/tf_files/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
with tf.Session() as sess:
out = open('output.csv', 'a')
for image_path in glob.glob(folder_path+'/*'):
# Read in the image_data
image_data = tf.gfile.FastGFile(image_path, 'rb').read()
# Feed the image_data as input to the graph and get first prediction
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
predictions = sess.run(softmax_tensor, \
{'DecodeJpeg/contents:0': image_data})
print("%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0]))
#THIS ACTUALLY WORKS, I see in my terminal "/tf_files/tested/pic1.jpg 0.00442768 0.995572"
out.write("%s\t%s\t%s\n" % (image_path,predictions[0][1],predictions[0][0]))
#This does not work, because output.csv is not modified
out.close()
虽然它有效 - 我在我的 Docker 终端中看到了每个图像的预期结果 - 如何将它保存在我的笔记本电脑上的文件中?
请您参考如下方法:
使用卷
与您的主机、笔记本电脑共享一个所谓的主机卷,以直接在主机上写下结果。例如当开始你的形象时
docker run -v /home/me/docker/results:/data/results <image>
在您的容器中,当您在/data/results 上写入时,所有文件将被同步回/放置在/home/me/docker/results 下的主机上
你当然可以同时调整主机或容器路径
用户 docker cp
您可以轻松地从 docker 容器(或到 docker 容器)用户复制文件 docker cp
所以
docker cp <yourimageid>:/path/to/the/file/on/the/container /home/me/docker/results
在关闭容器之前执行此操作
