深度学习代码案例-狗图片读取

源程序代码如下:

"""
   __author__="dazhi"
    2021/3/18-14:10
"""
#对于图片元素有三个维度:图片长度、图片宽度、图片通道数(rgb是3黑白是1)
import os
import tensorflow as tf
def picture_read(file_list):
    tf.compat.v1.disable_eager_execution()
    #1、-----------------------------------------------构建文件名队列
    file_queue = tf.compat.v1.train.string_input_producer(file_list)
    #2、-----------------------------------------------读取操作
    reader = tf.compat.v1.WholeFileReader()
    #key是文件名value是我们默认读取的一个样本
    key,value = reader.read(file_queue)
    #print("key=",key)
    #print("value=",value)
    #解码操作
    image = tf.image.decode_jpeg(value)
    #print("image = ",image)
    #图片形状修改
    image_resize = tf.compat.v1.image.resize_images(image,[200,200])
    #静态形状修改,添加通道数
    image_resize.set_shape(shape=[200,200,3])
    #3、-------------------------------------------------进行批处理(在这之前必须确定形状)
    #batch_size表示一次性处理多少,num_threads表示用几个线程,capacity容量
    image_bach = tf.compat.v1.train.batch([image_resize],batch_size=100,num_threads=1,capacity=100)
    #开启会话
    with tf.compat.v1.Session() as sess:
        #开启线程
        #首先创建线程协调员
        coord = tf.train.Coordinator()
        threads = tf.compat.v1.train.start_queue_runners(sess=sess,coord=coord)
        #会话运行变量
        key_new,value_new,image_new,image_resize_new,image_bach_new = sess.run([key,value,image,image_resize,image_bach])
        #print(key_new)
        #print(value_new)
        #print(image_new)
        #进行线程的回收操作
        coord.request_stop()
        coord.join(threads)
        #最后读入之后转为张量的结果:
        print("image_bach_new = ",image_bach_new)

    return None


if __name__ == '__main__':
    #构造路径+文件名列表
    filename = os.listdir("./dog")
    #拼接路径和文件名
    file_list = [os.path.join("./dog/",file) for file in filename]
    #print(filename)
    #print(file_list)
    picture_read(file_list)

各个变量输出结果如下:(输出位置在代码中已经注释了)

filename

在这里插入图片描述

file_list

在这里插入图片描述

key

在这里插入图片描述

value

在这里插入图片描述

key_new

在这里插入图片描述

value_new

在这里插入图片描述

image

在这里插入图片描述

image_new

在这里插入图片描述

最后读入图片并转为张量的结果是:

在这里插入图片描述

用到的dog文件夹:

用到的dog文件~~~~~(点我)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

喜欢地上爬的孩子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值