本文记录一下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()
输出为:

本文详细介绍了TensorFlow中读取图片的四种方法:通过gfile、WholeFileReader、read_file以及TFRecords。每种方法的输出形式和使用场景都有所不同,适合不同程度的数据预处理需求。对于分类存放的图片,可利用特定的代码结构进行读取。建议参考相关链接进一步了解。
最低0.47元/天 解锁文章
6505

被折叠的 条评论
为什么被折叠?



