How can Datatset.map be used in Tensorflow to create a dataset of image, label pairs?


The (image, label) pair is created by converting a list of path components, and then encoding the label to an integer format. The ‘map’ method helps in creating a dataset that corresponds to the (image,label) pair.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will be using the flowers dataset, which contains images of several thousands of flowers. It contains 5 sub-directories, and there is one sub-directory for every class.  

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

print("The 'num_parallel_calls' is set so that multiple images are loaded and processed in parallel")
train_ds = train_ds.map(process_path, num_parallel_calls=AUTOTUNE)
val_ds = val_ds.map(process_path, num_parallel_calls=AUTOTUNE)

for image, label in train_ds.take(1):
   print("The shape of image is : ", image.numpy().shape)
   print("The label is : ", label.numpy())

Output

The 'num_parallel_calls' is set so that multiple images are loaded and processed in parallel
The shape of image is :   (180, 180, 3)
The label is :   0

Code credit: https://www.tensorflow.org/tutorials/load_data/images

Explanation

  • Multiple images are loaded and process simultaneously.
  • The 'map' method is used to create a dataset that contains (image, label) pairs.
  • It is iterated over, and the dimensions of the shape, and the label is displayed on the console.

Updated on: 19-Feb-2021

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements