SoFunction
Updated on 2024-11-14

An introduction to the batch reading of Dataset images in tensorflow and the operation of dimensions in detail

Reading pictures in three dimensions (w, h, c).

import tensorflow as tf
 
import glob
import os
 
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = .decode_image(image_string) # (375, 500, 3)
 
  image_resized = .resize_image_with_crop_or_pad(image_decoded, 200, 200)
  return image_resized
 
 
 
 
with () as sess:
 
  print( ( img ).shape  )

Read images (b, w, h, c) for batch images.

import tensorflow as tf
 
import glob
import os
 
'''
  Dataset Batch Read Images
'''
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = .decode_image(image_string) # (375, 500, 3)
 
  image_decoded = tf.expand_dims(image_decoded, axis=0)
 
  image_resized = .resize_image_with_crop_or_pad(image_decoded, 200, 200)
  return image_resized
 
 
 
img = _parse_function('../pascal/VOCdevkit/VOC2012/JPEGImages/2007_000068.jpg')
 
# image_resized = .resize_image_with_crop_or_pad( tf.truncated_normal((1,220,300,3))*10, 200, 200) This four-dimensional form is fine
 
with () as sess:
 
  print( ( img ).shape  ) # Direct initialization can be converted to four-dimensional reported an error, I do not know why, if anyone wants to understand, please leave a message reported an error!
  #InvalidArgumentError (see above for traceback): Input shape axis 0 must equal 4, got shape [5]

Databae operations:

import tensorflow as tf
 
import glob
import os
 
'''
  Dataset Batch read images:

    Reason:
      1. first define the list of image names, stored in Dataset from_tensor_slices ()
      2. Mapping function, in the function, the list of images to read, and resize, details
        tf.read_file(filename) return is three-dimensional, because this each time out of a picture, put into the queue, do not need to be converted to four-dimensional
        and then resize the picture, and then each batch to access the function, so get_next() returns [batch, w, h, c].
      3. Set up shuffle and batch repeat.

      4. iterator = dataset.make_one_shot_iterator() set iterator

      5. iterator.get_next() get the image for each batch
'''
 
def _parse_function(filename):
  # print(filename)
  image_string = tf.read_file(filename)
  image_decoded = .decode_image(image_string) #(375, 500, 3)
  '''
    Tensor` with type `uint8` with shape `[height, width, num_channels]` for
     BMP, JPEG, and PNG images and shape `[num_frames, height, width, 3]` for
     GIF images.
  '''
 
  # image_resized = .resize_images(label, [200, 200])
  ''' images three dimensional,Any of the 4D's will do.
     images: 4-D Tensor of shape `[batch, height, width, channels]` or
      3-D Tensor of shape `[height, width, channels]`.
    size: A 1-D int32 Tensor of 2 elements: `new_height, new_width`. The
       new size for the images.
  
  '''
  image_resized = .resize_image_with_crop_or_pad(image_decoded, 200, 200)
 
  # return (mage_resized,axis=0)
  return image_resized
 
filenames = ( ('../pascal/VOCdevkit/VOC2012/JPEGImages', "*." + 'jpg') )
 
 
dataset = .from_tensor_slices((filenames))
 
dataset = (_parse_function)
 
dataset = (10).batch(2).repeat(10)
iterator = dataset.make_one_shot_iterator()
 
img = iterator.get_next()
 
with () as sess:
  # print( (img).shape ) #(4, 200, 200, 3)
  for _ in range (10):
    print( (img).shape )

Above this talk about tensorflow in Dataset image batch reading and dimension of the operation of the details is all I share with you, I hope to give you a reference, and I hope you support me more.