Python Tensorflow图像读取&;显示

Python Tensorflow图像读取&;显示,python,tensorflow,Python,Tensorflow,我有一堆类似于Cifar10格式的图像(二进制文件,size=96*96*3bytes/图像),一个接一个()。我打开的文件有138MB 我试图阅读并检查包含图像的张量的内容,以确保阅读正确,但我有两个问题- FixedLengthRecordReader是否加载整个文件,但每次只提供一个输入?由于读取第一个大小字节应该相对较快。但是,代码运行大约需要两分钟 如何以可显示的格式获取实际图像内容,或在内部显示以验证图像是否可读?我确实执行了sess.run(uint8image),但是结果是空的

我有一堆类似于Cifar10格式的图像(二进制文件,
size=96*96*3
bytes/图像),一个接一个()。我打开的文件有138MB

我试图阅读并检查包含图像的张量的内容,以确保阅读正确,但我有两个问题-

  • FixedLengthRecordReader
    是否加载整个文件,但每次只提供一个输入?由于读取第一个
    大小
    字节应该相对较快。但是,代码运行大约需要两分钟
  • 如何以可显示的格式获取实际图像内容,或在内部显示以验证图像是否可读?我确实执行了sess.run(uint8image),但是结果是空的
  • 代码如下:

    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)
    
    输出:

    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
    
    我在我的CPU上运行这个,如果这增加了什么的话

    编辑:多亏了Rosa,我找到了纯TensorFlow解决方案。显然,在使用
    字符串\u input\u producer
    时,为了查看结果,需要初始化队列运行程序。 需要添加到上述代码中的唯一内容是下面的第二行:

    ...
    with tf.Session() as sess:
        tf.train.start_queue_runners(sess=sess)
    ...
    

    然后,可以使用
    matplotlib.pyplot.imshow(result)
    显示
    结果中的图像。我希望这对某人有帮助。如果您还有任何问题,请随时询问我或查看Rosa答案中的链接。

    在评论中与您交谈后,我相信您可以使用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]
    
    然后可以运行图形:

    init = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init)
    im = sess.run(img_tf)
    
    并验证其是否相同:

    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.您提到:
    因为它应该与阅读并行,所以了解它似乎很有用。
    。对此,我可以说,在数据分析中很少有数据的读取是瓶颈。大部分时间你都会花在训练模型上。

    根据标准,你可以解码JPEG/PNG图像

    应该是这样的:

    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)
    

    您可以找到更多信息,只需给出完整的答案:

    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)
    
    或者,如果你有一个图像目录,你可以通过

    @mttk和@salvador dali:我希望这是你所需要的

    (无法评论,声誉不够,但这里有一个对我有用的修改版本)

    若要@HamedMP有关
    未注册默认会话的错误
    ,您可以使用
    InteractiveSession
    来消除此错误:

    对于@numesanguis与
    Image.show
    的问题,您可以使用常规的PIL
    .show()
    方法,因为
    fromarray
    返回图像对象

    我在下面做了这两件事(注意我使用的是JPEG而不是PNG):


    使用tf.train.match_filenames加载名称\u一旦获得要使用tf.size迭代的文件数 开放式课程并享受;-)


    我使用了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')
    

    该片段与mttk和Rosa Gronchi相同,但不知何故,我无法在运行时显示该图像,因此我将其保存为JPG文件

    您可以使用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()
    

    首先,scipy.misc.imread和PIL不再可用。取而代之的是使用imageio库,但作为依赖项,您需要为此安装枕头

    pip install Pillow imageio
    
    然后使用以下代码加载图像并获取其详细信息

    import imageio
    import tensorflow as tf
    
    path = 'your_path_to_image' # '~/Downloads/image.png'
    
    img = imageio.imread(path)
    print(img.shape) 
    


    两者都很好。

    在我看来,您只读取了一个图像(基于
    [维度(96)、维度(96)、维度(3)]
    ),而不是全部。这就是我想做的,读取一个图像并显示它,但我不确定TensorFlow是否仍加载整个文件,因为读取需要几分钟(这似乎是一个相对简单的操作,不需要那么长时间)您不想在numpy中读取文件作为3d数组并将其馈送到tensorFlow变量中有什么原因吗?正是使用纯tensorFlow。因为它应该并行读取,所以知道它似乎很有用。如果所有操作都失败,我可以在numpy中执行:)同意数据读取不是瓶颈,但是我知道应该有一种方法通过纯TensorFlow实现这一点,所以我想知道我遗漏了什么。除此之外,感谢您的回答,这很有帮助:)Salvador我们想要纯TF解决方案的原因可能是:由于TF编译过程并在自己的环境中运行它们,所以最好执行所有TF操作,以尽量减少python和TF本机计算(C)之间来回的时间。我对苏不感兴趣,但我认为这会使情况有所不同。你同意吗?@HamedMP我也希望看到TF解决方案,我也相信它会更好。问题是我不知道如何在TF中实现这一点,因此我写了一篇我知道的文章。我还相信,与训练一名模特所需的时间相比,这项开销将很小。@GayalKuruppu当然。非常感谢你,我终于找到了解决方案:)sess.run(init_op)之后的所有内容不也应该缩进2个空格吗?我得到一个:ValueError:无法使用eval()计算张量:未注册默认会话。(对于image=my_image.eval())实际上,我遇到了两个错误:image.show(),AttributeError:'module'对象没有属性'show',而I tensorflow/core/kernels/fifo_queue.cc:154]跳过取消的排队attempt@GJStein我想你是对的。我用这段代码加载了大约10000张图像(总大小只有10MB),但每次运行程序时,一些随机图像都会出现错误,如“未找到节点”。此外,程序加载这些图像需要30分钟以上。我想知道为什么。Image.fromarray(np.asarray(Image)).show()是从哪里来的?如果它是PIL的映像,那么它将无法与最新版本的TensorFlow 0.1.15配合使用。因为它使用的是Python 3.6,它不支持PIL…:/虽然这样做有效,但速度有点慢,因为load_img只使用PIL。()Ins
    pip install Pillow imageio
    
    import imageio
    import tensorflow as tf
    
    path = 'your_path_to_image' # '~/Downloads/image.png'
    
    img = imageio.imread(path)
    print(img.shape) 
    
    img_tf = tf.Variable(img)
    print(img_tf.get_shape().as_list())