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.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
Once the flower dataset has been downloaded using the ‘get_file’ method, it will be loaded into the environment to work with it. The loader parameters are explicitly mentioned and the loaded data is split into training and validation set.
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("Loading parameters for the loader") batch_size = 32 img_height = 180 img_width = 180 print("Preprocessing the image dataset using Keras") print("Splitting dataset into training and validation set ") train_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=batch_size) print("Splitting dataset into training and validation set ") val_ds = tf.keras.preprocessing.image_dataset_from_directory( data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size) print("Printing the class names present in sub-directories") class_names = train_ds.class_names print(class_names)
Code credit: https://www.tensorflow.org/tutorials/load_data/images
Output
Loading parameters for the loader Preprocessing the image dataset using Keras Splitting dataset into training and validation set Found 3670 files belonging to 5 classes. Using 2936 files for training. Splitting dataset into training and validation set Found 3670 files belonging to 5 classes. Using 734 files for validation. Printing the class names present in sub-directories ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
Explanation
- The parameters are defined.
- The dataset is split into training set and validation set.
- The class names into which every image is classified is displayed on the console.