tensorflow如何批量读取图片

yipeiwu_com7年前Python基础

本文实例为大家分享了tensorflow如何批量读取图片的具体代码,供大家参考,具体内容如下

代码:

import tensorflow as tf
import os


def picread(filelist):
 """
 读取狗的图片并转换成张量
 :param filelist: 文件路f径+名字的列表
 :return: 每张图片的张量
 """
 # 1.构造文件的队列
 file_queue = tf.train.string_input_producer(filelist)

 # 2.构造阅读器去读取图片内容(默认读取一张图片)
 reader = tf.WholeFileReader()
 key,value = reader.read(file_queue)

 # 3.对读取的图片进行解码
 image = tf.image.decode_jpeg(value)

 # 4.处理图片的大小(统一大小)
 image_resize = tf.image.resize_images(image,[200,200])

 # 注意:一定要把样本的形状固定,在批处理中要求所有数据的形状必须固定
 image_resize.set_shape([200,200,3])


 # 5.进行批处理
 image_resize_batch = tf.train.batch([image_resize],batch_size=3,num_threads=1,capacity=3)


 return image_resize


#批处理大小,跟队列,数据的数量没有影响,只决定 这批次处理多少数据

if __name__ == "__main__":
 # 1.找到文件,放入列表 路径+名字 ->列表当中
 file_name = os.listdir("./data/dogpic/")

 filelist = [os.path.join("./data/dogpic/",file) for file in file_name ]
 image_batch= picread(filelist)

 #开启会话运行结果
 with tf.Session() as sess:
  #定义一个线程协调器
  coord = tf.train.Coordinator()

  #开启读文件的线程
  threads = tf.train.start_queue_runners(sess,coord=coord)

  #打印读取的内容
  print(sess.run([image_batch]))

  #回收子线程
  coord.request_stop()
  coord.join(threads)

结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

pip命令无法使用的解决方法

今天在学习Python时需要安装Requests    使用命令:pip install requests    &...

Python编程中用close()方法关闭文件的教程

 close()方法方法关闭打开的文件。关闭的文件无法读取或写入更多东西。文件已被关闭之后任何操作会引发ValueError。但是调用close()多次是可以的。 Python...

解决django前后端分离csrf验证的问题

第一种方式ensure_csrf_cookie 这种方方式使用ensure_csrf_cookie 装饰器实现,且前端页面由浏览器发送视图请求,在视图中使用render渲染模板,响应给前...

python实现udp数据报传输的方法

本文实例讲述了Python实现UDP数据报传输的方法,非常具有实用价值。分享给大家供大家参考。具体方法分析如下: 服务端代码: import socket port = 8081...

python redis连接 有序集合去重的代码

python redis连接 有序集合去重的代码如下所述: # -*- coding: utf-8 -*- import redis from constant import re...