学会读取图片并转化为张量
import tensorflow as tf import os #读取狗图片并转换成张量 def picread(filelist): file_queue=tf.train.string_input_producer(filelist) #构造阅读器读取图片内容,默认读取一张图片 reader=tf.WholeFileReader() key,value=reader.read(file_queue) print(value) #解码 image=tf.image.decode_jpeg(value) print(image) #处理图片的大小统一 image_resize=tf.image.resize_images(image,[200,200]) print(image_resize) #注意一定要把样本的形状固定,批处理要求所有数据形状必须定义 image_resize.set_shape([200,200,3]) #进行批处理 image_batch=tf.train.batch([image_resize],batch_size=10,num_threads=1,capacity=10) print(image_batch) return image_batch if __name__=="__main__": file_name=os.listdir("./data/dog/") file_list=[os.path.join("./data/dog/",file)for file in file_name] image_batch=picread(file_list) 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)