Four ways for TensorFLow to read images from files

Source: Internet
Author: User

Four ways for TensorFLow to read images from files

This article records several image reading methods of TensorFLow. The official documentation provides a comprehensive introduction.

1. Read the image using gfile. The decode output is Tensor, And the eval is ndarray.

import matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npprint(tf.__version__)image_raw = tf.gfile.FastGFile('test/a.jpg','rb').read()  #bytesimg = 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()

Output:

1.3.0
<Class 'bytes '>
<Class 'tensorflow. python. framework. ops. Tensor '>
<Class 'numpy. ndarray'>
(666,100 0, 3)
Uint8
Image Display (omitted)

2. Use WholeFileReader to input queue, decode output is Tensor, and eval is ndarray

Import tensorflow as tfimport osimport matplotlib. pyplot as pltdef file_name (file_dir): # From the http://www.bkjia.com/article/134543.htm for root, dirs, files in OS. walk (file_dir): # The walk () function in the module OS traverses all files in the folder print (root) # current directory path print (dirs) # print (files) of all subdirectories in the current path # def file_name2 (file_dir) of all non-directory sub-files in the current path: # specific types of files 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') # The following reference http://www.bkjia.com/article/134547.htm (TensorFlow data read mechanism in ten pictures) # path2 = tf. train. match_filenames_once (path) file_queue = tf. train. string_input_producer (path, shuffle = True, num_epochs = 2) # create Input Queue 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 () # The thread to start collaboratively # threads = tf. train. start_queue_runners (sess = sess, coord = coord) # Start the thread to run the queue # coord. request_stop () # Stop all threads # coord. join (threads) tf. local_variables_initializer (). run () threads = tf. train. start_queue_runners (sess = sess) # print (type (image. eval () # print (image. eval (). shape) for _ in path + path: plt. figure plt. imshow (image. eval () plt. show ()

3. When read_file is used, the decode output is Tensor and the eval is ndarray.

import matplotlib.pyplot as pltimport tensorflow as tfimport numpy as npprint(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()

The output is:

1.3.0
<Class 'tensorflow. python. framework. ops. Tensor '>
<Class 'tensorflow. python. framework. ops. Tensor '>
<Class 'numpy. ndarray'>
(666,100 0, 3)
Uint8
Show image (omitted)

4. TFRecords:

If you are free, try again.

If the image is placed in different folders by category, you can directly use the following code:
Http://www.bkjia.com/article/134532.htm
Http://www.bkjia.com/article/134539.htm

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.