python-tensorflow批量读取图像
发布日期:2021-11-05 07:52:19 浏览次数:31 分类:技术文章

本文共 1500 字,大约阅读时间需要 5 分钟。

其实最根本的就是

1、利用

filenames_queue=tf.train.string_input_producer(data_names,num_epochs=5,shuffle=False,capacity=512)

2、读取了文件名,放在了文件名队列中,filenames_queue就是一个FIFOQueue,

3、然后出队列利用reader=tf.WholeFileReader()去读取文件,转换等等

4、再获取batch

这些都在构建文件名队列,构建文件队列的时候讲过了,唯一多的就是多了从文件名队列中获取文件名,到放入文件队列过程多了读取图像

import osimport tensorflow as tfimport matplotlib.pylab as pltdata_file = '.\\testdata'data_names=[os.path.join(data_file,k) for k in os.listdir(data_file)]#这个num_epochs函数在整个Graph是local Variable,所以在sess.run全局变量的时候也要加上局部变量。  filenames_queue=tf.train.string_input_producer(data_names,num_epochs=5,shuffle=False,capacity=512)##文件读取与转换reader=tf.WholeFileReader()_,img_bytes=reader.read(filenames_queue)image=tf.image.decode_png(img_bytes,channels=1)    #读取的是什么格式,就decode什么格式,此时还不知道图像大小#解码成单通道的,并且获得的结果的shape是[?, ?,1],也就是Graph不知道图像的大小,需要set_shapeimage.set_shape([800,800,1])   #set到原本已知图像的大小。或者直接通过tf.image.resize_images,tf.reshape()image=tf.image.convert_image_dtype(image,tf.uint8)##获取batch图像image_batch = tf.train.batch([image],batch_size=4,num_threads=4,capacity=32)with tf.Session() as sess:    init=[tf.global_variables_initializer(),tf.local_variables_initializer()]    sess.run(init)    coord = tf.train.Coordinator()    threads = tf.train.start_queue_runners(sess=sess,coord=coord)    for _ in range(5):        # while not coord.should_stop():        img = sess.run(image_batch)        plt.imshow(img[0,:,:,0],cmap='gray')        plt.show()    coord.request_stop()    coord.join(threads)

输出了5张图像 

数据如下放在文件夹中

留一张图测试用

转载地址:https://blog.csdn.net/xingghaoyuxitong/article/details/114681254 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python-tensorflow 多线程+Event进入enqueue
下一篇:python-tensorflow Coordinate和QueueRunner配合管理队列-注入数据

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月02日 23时58分49秒