本文记录一下TensorFLow的几种图片读取方法,官方文档有较为全面的介绍。
1.使用gfile读图片,decode输出是Tensor,eval后是ndarray
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_raw = tf.gfile.FastGFile('test/a.jpg','rb').read() #bytes img = tf.image.decode_jpeg(image_raw) #Tensor #img2 = tf.image.convert_image_dtype(img, dtype = tf.uint8) with tf.Session() as sess: print(type(image_raw)) # bytes print(type(img)) # Tensor #print(type(img2)) print(type(img.eval())) # ndarray !!! print(img.eval().shape) print(img.eval().dtype) # print(type(img2.eval())) # print(img2.eval().shape) # print(img2.eval().dtype) plt.figure(1) plt.imshow(img.eval()) plt.show()
输出为:
1.3.0
<class 'bytes'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
图片显示(略)
2.使用WholeFileReader输入queue,decode输出是Tensor,eval后是ndarray
import tensorflow as tf import os import matplotlib.pyplot as plt def file_name(file_dir): #来自https://www.jb51.net/article/134543.htm for root, dirs, files in os.walk(file_dir): #模块os中的walk()函数遍历文件夹下所有的文件 print(root) #当前目录路径 print(dirs) #当前路径下所有子目录 print(files) #当前路径下所有非目录子文件 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': L.append(os.path.join(root, file)) return L path = file_name2('test') #以下参考https://www.jb51.net/article/134547.htm (十图详解TensorFlow数据读取机制) #path2 = tf.train.match_filenames_once(path) file_queue = tf.train.string_input_producer(path, shuffle=True, num_epochs=2) #创建输入队列 image_reader = tf.WholeFileReader() key, image = image_reader.read(file_queue) image = tf.image.decode_jpeg(image) with tf.Session() as sess: # coord = tf.train.Coordinator() #协同启动的线程 # threads = tf.train.start_queue_runners(sess=sess, coord=coord) #启动线程运行队列 # coord.request_stop() #停止所有的线程 # coord.join(threads) tf.local_variables_initializer().run() threads = tf.train.start_queue_runners(sess=sess) #print (type(image)) #print (type(image.eval())) #print(image.eval().shape) for _ in path+path: plt.figure plt.imshow(image.eval()) plt.show()
3.使用read_file,decode输出是Tensor,eval后是ndarray
import matplotlib.pyplot as plt import tensorflow as tf import numpy as np print(tf.__version__) image_value = tf.read_file('test/a.jpg') img = tf.image.decode_jpeg(image_value, channels=3) with tf.Session() as sess: print(type(image_value)) # bytes print(type(img)) # Tensor #print(type(img2)) print(type(img.eval())) # ndarray !!! print(img.eval().shape) print(img.eval().dtype) # print(type(img2.eval())) # print(img2.eval().shape) # print(img2.eval().dtype) plt.figure(1) plt.imshow(img.eval()) plt.show()
输出是:
1.3.0
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'numpy.ndarray'>
(666, 1000, 3)
uint8
显示图片(略)
4.TFRecords:
有空再看。
如果图片是根据分类放在不同的文件夹下,那么可以直接使用如下代码:
https://www.jb51.net/article/134532.htm
https://www.jb51.net/article/134539.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍