我剛開始使用Tensorflow,我有一個新手問題。在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)
這給出了同樣的警告。我不明白爲什麼。