Capstone Project: Image Classification
Duration: 10 min
This module covers the end-to-end process of building an image classification system using TensorFlow and Keras. You will learn how to preprocess data, build and train Convolutional Neural Networks (CNNs), utilize transfer learning, fine-tune hyperparameters, and deploy the model. This project is crucial for understanding real-world applications of deep learning in computer vision tasks.
Data Preprocessing and Loading
Data preprocessing is a critical step in any machine learning project. For image classification, this involves loading images, resizing them to a consistent size, normalizing pixel values, and splitting the dataset into training and validation sets. TensorFlow and Keras provide utilities to streamline these processes.
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# Define data generators
train_datagen = ImageDataGenerator(rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
# Load data
train_generator = train_datagen.flow_from_directory(
'data/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
'data/validation',
target_size=(150, 150),
batch_size=32,
class_mode='binary')Found 2000 images belonging to 2 classes.
Found 800 images belonging to 2 classes.Building and Training a CNN
Convolutional Neural Networks (CNNs) are the state-of-the-art models for image classification tasks. They automatically and adaptively learn spatial hierarchies of features from input images. In this section, we will build a simple CNN using Keras and train it on our preprocessed data.
from tensorflow.keras import layers, models
# Build the 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(
train_generator,
steps_per_epoch=100,
epochs=10,
validation_data=validation_generator,
validation_steps=50)💡 Tip: Ensure your dataset is balanced to avoid bias in the model. Also, monitor the training and validation accuracy to detect overfitting early.
❓ What is the purpose of the ImageDataGenerator in TensorFlow?
❓ Which layer is typically used at the end of a CNN for binary classification?