【发布时间】:2016-01-14 06:30:17
【问题描述】:
我刚开始使用 Tensorflow,我有一个新手问题。
我知道 Tensorflow 是关于神经网络的,但我只是从它的机制开始。我试图让它加载、调整大小、翻转和保存两个图像。应该是一个简单的操作吧,它让我从基础开始。
到目前为止,这是我的代码:
import tensorflow as tf
import numpy as np
print("resizing images")
filenames = ['img1.png', 'img2.png' ]
filename_queue = tf.train.string_input_producer(filenames, num_epochs=1)
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
images = tf.image.decode_png(value)
resized = tf.image.resize_images(images, 180,180, 1)
resized.set_shape([180,180,3])
flipped_images = tf.image.flip_up_down(resized)
resized_encoded = tf.image.encode_jpeg(flipped_images,name="save_me")
init = tf.initialize_all_variables()
sess = tf.Session()
with sess.as_default():
tf.train.start_queue_runners()
sess.run(init)
f = open("/tmp/foo1.jpeg", "wb+")
f.write(resized_encoded.eval())
f.close()
f = open("/tmp/foo2.jpeg", "wb+")
f.write(resized_encoded.eval())
f.close()
它工作正常,调整两个图像的大小并保存它们。但它总是以错误结束:
W tensorflow/core/common_runtime/executor.cc:1076] 0x7f97240e7a40
Compute status: Out of range: Reached limit of 1
我显然做错了什么。如果我去掉 num_epochs=1,那么它就不会出错。
我有几个问题:
我该如何正确地做到这一点?
另外,如果我想保留从 filename_queue 到最后的原始文件名,以便我可以使用原始名称保存它们,我该怎么做?我怎么知道我需要保存多少文件?假设我通过读取目录来制作文件名列表。我尝试了很多不同的事情,但我永远无法知道我是如何知道何时到达终点的。
我调用 resized_encoded.eval() 两次对我来说似乎很奇怪。
谢谢,我确信这是一个非常基本的问题,但我不明白这是如何工作的。
编辑:我创建了一个更简单的行为演示:
import tensorflow as tf
import numpy as np
filenames = ['file1.png', 'file2.png' ]
filename_queue = tf.train.string_input_producer(filenames,
num_epochs=1, name="my_file_q")
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
init = tf.initialize_all_variables()
sess = tf.Session()
with sess.as_default():
print("session started")
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range (2):
print(key.eval())
coord.request_stop()
coord.join(threads)
这给出了同样的警告。我不明白为什么。
【问题讨论】:
标签: tensorflow