Tensorflow image reading & display

前端 未结 8 1138
轮回少年
轮回少年 2020-11-30 21:41

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\'

相关标签:
8条回答
  • 2020-11-30 22:18

    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.

    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.

    0 讨论(0)
  • 2020-11-30 22:18

    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

    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.

    0 讨论(0)
提交回复
热议问题