tensorflow 使用队列读取图像信息

原文链接: 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']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值