tensorflow 读取图片数据---读本地图片

网上找了个能跑通的 把图片读入队列的 程序,顺便试了下把这段代码改成读txt数据的,也跑通了。写个学习总结。

想写个机器学习模型,第一步当然是把数据读进来。读数据除了用python本身的一些方法,tensorflow当然也提供了更加高效的数据读入方式。今天看的是WholeFileReader 将数据组织成队列的方式。

import tensorflow as tf
import os
import matplotlib.pyplot as plt
num_epoch=3

def file_name2(file_dir):   #特定类型的文件
    L=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == '.jpg':#这里指定文件类型,如果要读txt,就把jpg换成txt
                L.append(os.path.join(root, file))
    return L
#这里指定文件路径 
path = file_name2(r'D:\PY\tensorbord')
 

 
#在这里把文件路径放进去,就能把路径下的所有指定类型的文件都读到队列里去。
file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=num_epoch) 
image_reader = tf.WholeFileReader()
#key 就是读进来以后的那个tensor,image是tensor的值。
key, image = image_reader.read(file_queue)
image = tf.image.decode_jpeg(image)

with tf.Session() as sess:
    tf.local_variables_initializer().run()
    threads = tf.train.start_queue_runners(sess=sess)
    for _ in path*num_epoch:
        plt.figure
        plt.imshow(image.eval())
        plt.show()
        print(key)
看tensorflow的帮助文档,WholeFileReader的两个返回值,第一个key是输出的名字,一般是没啥用,第二个是这个tensor的数值。后面对读入的数据做一些后续处理,比如切片啊,reshape啊,就用第二个返回值。

class WholeFileReader(ReaderBase)
 |  A Reader that outputs the entire contents of a file as a value.
 |
 |  To use, enqueue filenames in a Queue.  The output of Read will
 |  be a filename (key) and the contents of that file (value).

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hi小蜗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值