dl-workshop-2022

Reading images with Python (Tensorflow)

The goal here is to load all the images in the ‘train’ folder and ‘validation’ folder into image data generators and visualize some of the images and their labels. Please re-order the blocks of code below, copy-paste the code blocks in appropriate order in a new Colab notebook, run them, and analyze the outputs.

Block A: Visualize some images in the training set

import matplotlib.pyplot as plt
images, labels = train_ds[0] # # Take one batch full of images
plt.imshow(images[0])
plt.title(labels[0])
plt.axis("off")

Block B: Load some images in the validation set

valid_ds = datagen.flow_from_directory(
  './face-expression-kaggle/images/validation/',
  target_size=(48, 48),
  shuffle=True,
  batch_size=32)

Block C: Load training data folder using a Keras utility

from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)
train_ds = datagen.flow_from_directory(
  './face-expression-kaggle/images/train/',
  target_size=(48, 48),
  shuffle=True,
  batch_size=32)

Block D: Upload the dataset

Upload the (.zip file) to Google Colab from the folder icon in the left toolbar.

Block E: Verify that unzipping worked:

import pathlib
data_dir = pathlib.Path('./face-expression-kaggle/')
image_count = len(list(data_dir.glob('*/*/*/*.jpg')))
print(image_count)

Block F: Visualize some images in the validation set

import matplotlib.pyplot as plt
images, labels = valid_ds[0] # # Take one batch full of images
plt.imshow(images[0])
plt.title(labels[0])
plt.axis("off")

Block G: Unzip the dataset

! unzip face-expression-kaggle.zip