How can Tensorflow be used to configure the flower dataset for performance?



The flower dataset would have given a certain percentage of accuracy when a model is created. If it is required to configure the model for performance, a function is defined that performs the buffer prefetch for the second time, and then it is shuffled. This function is called on the training dataset to improve the performance of the model.

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("A function is defined that configures the dataset for perfromance")
def configure_for_performance(ds):
   ds = ds.cache()
   ds = ds.shuffle(buffer_size=1000)
   ds = ds.batch(batch_size)
   ds = ds.prefetch(buffer_size=AUTOTUNE)
   return ds

print("The function is called on training dataset")
train_ds = configure_for_performance(train_ds)
print("The function is called on validation dataset")
val_ds = configure_for_performance(val_ds)

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

Output

A function is defined that configures the dataset for perfromance
The function is called on training dataset
The function is called on validation dataset

Explanation

  • A model needs to be trained with the dataset.
  • The model is first well shuffled, then batched, and then these batches are made available.
  • These features are added using the 'tf.data' API.

Advertisements