Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to read image data in TensorFlow

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what are the ways to read image data in TensorFlow". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the ways to read image data in TensorFlow?"

Working with a single picture

After we have finished training the model, we often have to test it with pictures, and sometimes we don't need to test a lot of images, maybe a few or even one. There is no need to use a queuing mechanism in this case.

Import tensorflow as tfimport matplotlib.pyplot as pltdef read_image (file_name): img = tf.read_file (filename=file_name) # default read format is uint8print ("img type is", type (img)) Img = tf.image.decode_jpeg (img,channels=0) # channels is 1 to get a grayscale image If 0, read return imgdef main (): with tf.device ("/ cpu:0"): img_path='./1.jpg'img=read_image (img_path) with tf.Session () as sess:image_numpy=sess.run (img) print (image_numpy) print (image_numpy.dtype) print (image_numpy.shape) plt.imshow (image_numpy) plt.show () if _ name__== "_ main__": main ()

Img_path is the address where the file is located, including the name of the file, either relative or absolute

The output is as follows:

The type of img is [[196219209] [196219209] [196219209]. [[71 106 42] [59 89 39] [34 63 19]. [21 52 46] [15 45 43] [22 50 53]] uint8 (675, 1200, 3)

A function that is similar to tf.read_file is tf.gfile.FastGFile tf.gfile.GFile, except that you specify whether the read method is'r'or 'rb'.

A large number of images need to be read for training

In this case, you need to use the Tensorflow queue mechanism. The first step is to get the path to each image, put them all into a list, then create a queue with string_input_producer, and then read it with tf.WholeFileReader. For more information, please see the following example:

Def get_image_batch (data_file,batch_size): data_names= [os.path.join (data_file,k) for k in os.listdir (data_file)] # this num_epochs function is local Variable in the whole Graph, so local variables are added when sess.run global variables. Filenames_queue=tf.train.string_input_producer (data_names,num_epochs=50,shuffle=True,capacity=512) reader=tf.WholeFileReader () _, img_bytes=reader.read (filenames_queue) image=tf.image.decode_png (img_bytes,channels=1) # reads what format, decode what format # decodes into a single channel, and the resulting shape is [?, 1], that is, Graph does not know the size of the image. You need set_shapeimage.set_shape ([180, 180, 180, 1]) # set to the size of the originally known image. Or the following code can be preprocessed directly through tf.image.resize_imagesimage=tf.image.convert_image_dtype (image,tf.float32) # and can be replaced with the preprocessing method you want to use # image=tf.divide (image,255.0) return tf.train.batch ([image], batch_size)

The date_file here refers to the path where the folder is located, excluding the file name. The first sentence is to traverse the file name in the specified directory and store it in a list. Of course, there are many ways to do this, such as glob.glob, or the whole code of tf.train.match_filename_once is as follows:

Import tensorflow as tf import os def read_image (data_file,batch_size): data_names= [os.path.join (data_file,k) for k in os.listdir (data_file)] filenames_queue=tf.train.string_input_producer (data_names,num_epochs=5,shuffle=True,capacity=30) reader=tf.WholeFileReader () _, img_bytes=reader.read (filenames_queue) image=tf.image.decode_jpeg (img_bytes,channels=1) image=tf.image.resize_images (image (180180) image=tf.image.convert_image_dtype (image,tf.float32) return tf.train.batch ([image], batch_size) def main (): img_path=r'F:\ dataSet\ WIDER\ WIDER_train\ images\ 6 a local dataset directory in Murray Funeral' # There are enough images img=read_image (img_path,batch_size=10) image=img [0] # take out the first data of each batch print (image) init= [tf.global _ variables_initializer (), tf.local_variables_initializer ()] with tf.Session () as sess: sess.run (init) coord = tf.train.Coordinator () threads = tf.train.start_queue_runners (sess=sess Coord=coord) try: while not coord.should_stop (): # omit the use of image If you just execute the following code, the image is always the same image. We need # sess.run to implement the iteration of image. Thank monk1992 for correcting print (image.shape) except tf.errors.OutOfRangeError: print ('read done') finally: coord.request_stop () coord.join (threads) if _ _ name__== "_ _ main__": main ()

It can be said that this code is very neat. Notice that there is initialization of the local variable in init, and because the queue is used, of course, you have to tell the computer when the queue starts. Tf.train.Coordinator and tf.train.start_queue_runners are the two classes that manage the queue, as shown in the program.

The output is as follows:

(180, 180, 1) (180, 180, 1) (180, 180, 1) (180, 180, 1) (180, 180, 1)

A function similar to tf.train.string_input_producer is tf.train.slice_input_producer. The form of the first parameter of tf.train.slice_input_producer and tf.train.string_input_producer is different. Wait until you have time to do a blog comparing the two.

Decoding TFRecorder to get image data

In fact, this piece is similar to the previous way, more important is how to generate TFRecorder files, this part I will add to another blog post.

Still use tf.train.string_input_producer.

Import tensorflow as tfimport matplotlib.pyplot as pltimport osimport cv2import numpy as npimport globdef read_image (data_file,batch_size): files_path=glob.glob (data_file) queue=tf.train.string_input_producer (files_path,num_epochs=None) reader = tf.TFRecordReader () print (queue) _, serialized_example = reader.read (queue) features= tf.parse_single_example (serialized_example,features= {'image_raw': tf.FixedLenFeature ([], tf.string),' label_raw': tf.FixedLenFeature ([], tf.string)) }) image = tf.decode_raw (features ['image_raw'], tf.uint8) image = tf.cast (image, tf.float32) image.set_shape ((12' 12'3)) label = tf.decode_raw (features ['label_raw'], tf.float32) label.set_shape ((2)) # omitted from preprocessing You can add return tf.train.batch ([image,label], batch_size=batch_size,num_threads=4,capacity=5*batch_size) def main () as needed: img_path=r'F:\ python\ MTCNN_by_myself\ prepare_data\ pnet*.tfrecords' # several local tf files img,label=read_image (img_path,batch_size=10) image=img [0] init= [tf.global _ variables_initializer () Tf.local_variables_initializer ()] with tf.Session () as sess:sess.run (init) coord= tf.train.Coordinator () threads = tf.train.start_queue_runners (sess=sess,coord=coord) try:while not coord.should_stop (): # omit the use of image If you just execute the following code, the image is always the same image. We need # sess.run to implement the iteration of image. Thank monk1992 for correcting print (image.shape) except tf.errors.OutOfRangeError:print ('read done') finally:coord.request_stop () coord.join (threads) if _ _ name__== "_ _ main__": main ()

In the read_image function, you first use the glob function to get a list of tfrecord files, then parse according to how the TFRecord files are saved, and then set_shape

It is necessary to remind you of the way parse works. We see that tf.decode_raw is used here, because to do TFRecord is to string the image data, the data is serial, and the spatial result is lost. When you take the data of image and label from features, you have to decode it with tf.decode_raw. Of course, the result is serial, so set_shape becomes a serial, and then reshape. This approach depends on the way you code TFRecord. Give me another example:

Reader=tf.TFRecordReader () _, serialized_example=reader.read (file_name_queue) features= tf.parse_single_example (serialized_example, features= {'data': tf.FixedLenFeature ([256256], tf.float32),' label': tf.FixedLenFeature ([], tf.int64), 'id': tf.FixedLenFeature ([], tf.int64)}) img = features [' data'] label = features ['label'] id = features [' id']

There is no need for any decoding at this time. Because the way to do TFRecord is to append the image data directly.

At this point, I believe you have a deeper understanding of "what is the way to read image data in TensorFlow". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report