Module 14 of 25 · TensorFlow & Keras · Intermediate

Working with Image Data

Duration: 7 min

This module delves into the techniques and methodologies for working with image data using TensorFlow and Keras. You will learn how to preprocess image data, build Convolutional Neural Networks (CNNs), utilize Recurrent Neural Networks (RNNs) for sequence data, apply transfer learning, perform hyperparameter tuning, and deploy your models. Understanding these concepts is crucial for developing robust image recognition and classification systems.

Image Preprocessing

Image preprocessing is a critical step in preparing image data for neural network training. This involves resizing images, normalizing pixel values, and augmenting data to improve model generalization. TensorFlow and Keras provide utilities to handle these tasks efficiently.

import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator

# Create an ImageDataGenerator object
datagen = ImageDataGenerator(
    rescale=1./255,  # Normalize pixel values
    shear_range=0.2,  # Apply random shear transformations
    zoom_range=0.2,   # Apply random zoom
    horizontal_flip=True)  # Apply random horizontal flips

# Load images from a directory
generator = datagen.flow_from_directory(
    'path_to_dataset',  # Directory containing images
    target_size=(150, 150),  # All images will be resized to 150x150
    batch_size=32,
    class_mode='binary')  # For binary classification

Try it in Google Colab: Open in Colab

Found 2000 images belonging to 2 classes.

Building Convolutional Neural Networks (CNNs)

CNNs are the go-to architecture for image classification tasks. They automatically detect important features without any feature engineering. TensorFlow and Keras make it straightforward to build and train CNNs.

import tensorflow as tf
from tensorflow.keras import layers, models

# Build a CNN model
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

# Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Train the model
history = model.fit(
    generator,
    steps_per_epoch=100,  # Number of batches per epoch
    epochs=10,
    validation_data=val_generator,  # Validation data generator
    validation_steps=50)  # Number of validation batches

💡 Tip: Always ensure your input shape matches the expected input shape of your CNN model. Mismatches can lead to errors during training.

❓ What is the purpose of normalizing pixel values in image preprocessing?

❓ Which layer is typically used after a Conv2D layer in a CNN to reduce spatial dimensions?

← Previous Continue interactively → Next →

Related Courses