原文链接: tensorflow 使用队列读取图像信息
上一篇: TensorFlow 队列 queue
下一篇: one hot 编码 转化
参考
https://zhuanlan.zhihu.com/p/27238630
多个reader读csv
https://blog.csdn.net/gsww404/article/details/78083169
使用队列读取图片信息
import tensorflow as tf
with tf.Session() as sess:
filename = ['a.jpg', 'b.jpg', 'c.jpg']
filename_queue = tf.train.string_input_producer(filename,
shuffle=False,
num_epochs=1)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
tf.local_variables_initializer().run()
# Create a coordinator, launch the queue runner threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
i = 0
try:
while not coord.should_stop():
while True:
i += 1
# image_data = sess.run(value)
key, image_data = sess.run(reader.read(filename_queue))
img = tf.decode_raw(image_data, tf.uint8)
tf_image = tf.image.decode_jpeg(image_data, channels=3)
print(sess.run(tf_image).shape)
print(sess.run(img))
print(key, image_data)
with open('read/test_%d.jpg' % i, 'wb') as f:
f.write(image_data)
except tf.errors.OutOfRangeError:
# When done, ask the threads to stop.
print('Done training -- epoch limit reached')
finally:
coord.request_stop()
# Wait for threads to finish.
coord.join(threads)
可以自己手动转为u8 类型,但是会失去维度信息 ,直接使用tf的image转换函数,就可以得到图像矩阵,进行后面的操作
(127, 112, 3)
(?, ?, 3)
[137 80 78 ... 66 96 130]
多个reader读取
import tensorflow as tf
with tf.Session() as sess:
filename = ['a.jpg', 'b.jpg', 'c.jpg']
filename_queue = tf.train.string_input_producer(filename,
shuffle=False,
num_epochs=3)
reader = tf.WholeFileReader()
example_list = [reader.read(filename_queue)] * 10 # Reader设置为2
batch_key, batch_val = tf.train.batch_join(
example_list, batch_size=4) # 每次读四个
tf.local_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
i = 0
try:
while not coord.should_stop():
while True:
i += 1
print(sess.run(batch_key))
except tf.errors.OutOfRangeError:
print('Done training -- epoch limit reached')
finally:
coord.request_stop()
coord.join(threads)
由于队列中只写入了9张图片,所以只读了两次
[b'a.jpg' b'b.jpg' b'c.jpg' b'a.jpg']
[b'b.jpg' b'c.jpg' b'a.jpg' b'b.jpg']