Python Tensorflow 图像读取和显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33648322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow image reading & display
提问by mttk
I've got a bunch of images in a format similar to Cifar10 (binary file, size = 96*96*3
bytes per image), one image after another (STL-10 dataset). The file I'm opening has 138MB.
我有一堆类似于 Cifar10 格式的图像(二进制文件,size = 96*96*3
每个图像的字节数),一个接一个的图像(STL-10 数据集)。我打开的文件有 138MB。
I tried to read & check the contents of the Tensors containing the images to be sure that the reading is done right, however I have two questions -
我试图阅读并检查包含图像的张量的内容以确保阅读正确,但是我有两个问题 -
- Does the
FixedLengthRecordReader
load the whole file, however just provide inputs one at a time? Since reading the firstsize
bytes should be relatively fast. However, the code takes about two minutes to run. - How to get the actual image contents in a displayable format, or display them internally to validate that the images are read well? I did
sess.run(uint8image)
, however the result is empty.
- 是否
FixedLengthRecordReader
加载整个文件,但一次只提供一个输入?由于读取第一个size
字节应该相对较快。但是,代码运行大约需要两分钟。 - 如何以可显示的格式获取实际图像内容,或在内部显示它们以验证图像是否被正确读取?我做了
sess.run(uint8image)
,但结果是空的。
The code is below:
代码如下:
import tensorflow as tf
def read_stl10(filename_queue):
class STL10Record(object):
pass
result = STL10Record()
result.height = 96
result.width = 96
result.depth = 3
image_bytes = result.height * result.width * result.depth
record_bytes = image_bytes
reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
result.key, value = reader.read(filename_queue)
print value
record_bytes = tf.decode_raw(value, tf.uint8)
depth_major = tf.reshape(tf.slice(record_bytes, [0], [image_bytes]),
[result.depth, result.height, result.width])
result.uint8image = tf.transpose(depth_major, [1, 2, 0])
return result
# probably a hack since I should've provided a string tensor
filename_queue = tf.train.string_input_producer(['./data/train_X'])
image = read_stl10(filename_queue)
print image.uint8image
with tf.Session() as sess:
result = sess.run(image.uint8image)
print result, type(result)
Output:
输出:
Tensor("ReaderRead:1", shape=TensorShape([]), dtype=string)
Tensor("transpose:0", shape=TensorShape([Dimension(96), Dimension(96), Dimension(3)]), dtype=uint8)
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
[empty line for last print]
Process finished with exit code 137
I'm running this on my CPU, if that adds anything.
我正在我的 CPU 上运行它,如果有的话。
EDIT: I found the pure TensorFlow solution thanks to Rosa. Apparently, when using the string_input_producer
, in order to see the results, you need to initialize the queue runners.
The only required thing to add to the code above is the second line from below:
编辑:感谢 Rosa,我找到了纯 TensorFlow 解决方案。显然,在使用 时string_input_producer
,为了查看结果,您需要初始化队列运行器。唯一需要添加到上面代码的是下面的第二行:
...
with tf.Session() as sess:
tf.train.start_queue_runners(sess=sess)
...
Afterwards, the image in the result
can be displayed with matplotlib.pyplot.imshow(result)
. I hope this helps someone. If you have any further questions, feel free to ask me or check the link in Rosa's answer.
之后,result
可以显示 中的图像matplotlib.pyplot.imshow(result)
。我希望这可以帮助别人。如果您有任何其他问题,请随时问我或查看 Rosa 回答中的链接。
采纳答案by Hamed MP
Just to give a complete answer:
只是给出一个完整的答案:
filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) # list of files to read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_img = tf.image.decode_png(value) # use png or jpg decoder based on your files.
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1): #length of your filename list
image = my_img.eval() #here is your image Tensor :)
print(image.shape)
Image.fromarray(np.asarray(image)).show()
coord.request_stop()
coord.join(threads)
Or if you have a directory of images you can add them all via this Github source file
或者如果你有一个图像目录,你可以通过这个 Github 源文件添加它们
@mttk and @salvador-dali: I hope it is what you need
@mttk 和 @salvador-dali:我希望这是你所需要的
回答by Salvador Dali
After speaking with you in the comments, I believe that you can just do this using numpy/scipy. The ideas is to read the image in the numpy
3d-array and feed it into the variable.
在评论中与您交谈后,我相信您可以使用 numpy/scipy 来做到这一点。想法是读取numpy
3d 数组中的图像并将其输入变量。
from scipy import misc
import tensorflow as tf
img = misc.imread('01.png')
print img.shape # (32, 32, 3)
img_tf = tf.Variable(img)
print img_tf.get_shape().as_list() # [32, 32, 3]
Then you can run your graph:
然后你可以运行你的图表:
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
im = sess.run(img_tf)
and verify that it is the same:
并验证它是否相同:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(1,2,1)
plt.imshow(im)
fig.add_subplot(1,2,2)
plt.imshow(img)
plt.show()
P.S.you mentioned: Since it's supposed to parallelize reading, it seems useful to know.
. To which I can say that rarely in data-analysis reading of the data is the bottleneck. Most of your time you will spend training your model.
PS你提到的:Since it's supposed to parallelize reading, it seems useful to know.
。对此我可以说,很少在数据分析中读取数据是瓶颈。您大部分时间都将花费在训练模型上。
回答by Rosa Gronchi
According to the documentationyou can decode JPEG/PNG images.
根据文档,您可以解码 JPEG/PNG 图像。
It should be something like this:
它应该是这样的:
import tensorflow as tf
filenames = ['/image_dir/img.jpg']
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
images = tf.image.decode_jpeg(value, channels=3)
You can find a bit more info here
你可以在这里找到更多信息
回答by curiouscat
(Can't comment, not enough reputation, but here is a modified version that worked for me)
(无法评论,没有足够的声誉,但这是一个对我有用的修改版本)
To @HamedMP error about the No default session is registered
you can use InteractiveSession
to get rid of this error:
https://www.tensorflow.org/versions/r0.8/api_docs/python/client.html#InteractiveSession
到@HamedMP 错误No default session is registered
你可以InteractiveSession
用来摆脱这个错误:https:
//www.tensorflow.org/versions/r0.8/api_docs/python/client.html#InteractiveSession
And to @NumesSanguis issue with Image.show
, you can use the regular PIL .show()
method because fromarray
returns an image object.
对于@NumesSanguis 问题Image.show
,您可以使用常规 PIL.show()
方法,因为fromarray
返回一个图像对象。
I do both below (note I'm using JPEG instead of PNG):
我在下面都做(注意我使用的是 JPEG 而不是 PNG):
import tensorflow as tf
import numpy as np
from PIL import Image
filename_queue = tf.train.string_input_producer(['my_img.jpg']) # list of files to read
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_img = tf.image.decode_jpeg(value) # use png or jpg decoder based on your files.
init_op = tf.initialize_all_variables()
sess = tf.InteractiveSession()
with sess.as_default():
sess.run(init_op)
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1): #length of your filename list
image = my_img.eval() #here is your image Tensor :)
Image.fromarray(np.asarray(image)).show()
coord.request_stop()
coord.join(threads)
回答by Ivan Jacobs
Load names with tf.train.match_filenames_once get the number of files to iterate over with tf.size open session and enjoy ;-)
使用 tf.train.match_filenames_once 加载名称获取文件数量以使用 tf.size 打开会话进行迭代并享受 ;-)
import tensorflow as tf
import numpy as np
import matplotlib;
from PIL import Image
matplotlib.use('Agg')
import matplotlib.pyplot as plt
filenames = tf.train.match_filenames_once('./images/*.jpg')
count_num_files = tf.size(filenames)
filename_queue = tf.train.string_input_producer(filenames)
reader=tf.WholeFileReader()
key,value=reader.read(filename_queue)
img = tf.image.decode_jpeg(value)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
num_files = sess.run(count_num_files)
for i in range(num_files):
image=img.eval()
print(image.shape)
Image.fromarray(np.asarray(image)).save('te.jpeg')
回答by Cloud Cho
I used CIFAR10 format instead of STL10 and code came out like
我使用 CIFAR10 格式而不是 STL10 并且代码出来了
filename_queue = tf.train.string_input_producer(filenames)
read_input = read_cifar10(filename_queue)
with tf.Session() as sess:
tf.train.start_queue_runners(sess=sess)
result = sess.run(read_input.uint8image)
img = Image.fromarray(result, "RGB")
img.save('my.jpg')
The snippet is identical with mttk and Rosa Gronchi, but Somehow I wasn't able to show the image during run-time, so I saved as the JPG file.
该片段与 mttk 和 Rosa Gronchi 相同,但不知何故我无法在运行时显示图像,所以我保存为 JPG 文件。
回答by Alex-zhai
You can use tf.keras API.
您可以使用 tf.keras API。
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, array_to_img
tf.enable_eager_execution()
img = load_img("example.png")
img = tf.convert_to_tensor(np.asarray(img))
image = tf.image.resize_images(img, (800, 800))
to_img = array_to_img(image)
to_img.show()
回答by Gayal Kuruppu
First of all scipy.misc.imread and PIL are no longer available. Instead use imageio library but you need to install Pillow for that as a dependancy
首先 scipy.misc.imread 和 PIL不再可用。而是使用 imageio 库,但您需要为此安装 Pillow 作为依赖项
pip install Pillow imageio
Then use the following code to load the image and get the details about it.
然后使用以下代码加载图像并获取有关它的详细信息。
import imageio
import tensorflow as tf
path = 'your_path_to_image' # '~/Downloads/image.png'
img = imageio.imread(path)
print(img.shape)
or
或者
img_tf = tf.Variable(img)
print(img_tf.get_shape().as_list())
both work fine.
两者都工作正常。