Search code examples
pythontensorflowimage-resizing

How to read images with varying resolutions to create a Dataset in TensorFlow


I am trying to learn how to build a neural network to detect pedestrians in images. I downloaded the PETA dataset and I want to read the images into a Tensorflow dataset. I actually succeeded it by using this code:

filenames = glob.glob("C://PETA/3DPeS/archive/*.bmp")

dataset = tf.data.Dataset.from_tensor_slices((filenames))

def _parsefunc(filename):
    img_st = tf.read_file(filename)
    img_dec = tf.image.decode_bmp(img_st,channels=3)
    img = tf.cast(img_dec,tf.float32)

return img

dataset = dataset.map(_parsefunc)

iterator = dataset.make_one_shot_iterator()

But not all the images have the same resolutions and as long as I can see we need to specify a certain size to define our neural network layers.

So how can I resize the images to get a clean TensorFlow dataset?

Thanks.


Solution

  • In your _parsefunc you can do the resize of the input images to the size of your input neural network layer, using tf.image.resize_images().

    def _parsefunc(filename):
       img_st = tf.read_file(filename)
       img_dec = tf.image.decode_bmp(img_st,channels=3)
       img = tf.cast(img_dec,tf.float32)
       img = tf.image.resize_images(img, [width, height])