CNN Implementation in Python
CNN Implementation in Python
import tensorflow as tf
# One-hot encode labels to convert them from class indices to binary vectors
model = models.Sequential()
# Add a 2D convolutional layer with 32 filters, each of size (3, 3), and ReLU activation
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
# Add a third 2D convolutional layer with 64 filters and ReLU activation
# Flatten the 3D output to 1D, preparing it for the fully connected layers
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
# Add the output layer with 10 neurons (for 10 classes) and softmax activation
model.add(layers.Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Validation data is used to monitor the model's performance on unseen data during training
# Evaluate the trained model on the test data and print the accuracy
Explanation:
1. Import Libraries: Import TensorFlow and its submodules, including layers and models, to create a neural
network. Also, import the CIFAR-10 dataset and utility functions for data preprocessing.
2. Load and Preprocess Data: Load the CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10
different classes. Normalize pixel values to the range [0, 1] and one-hot encode class labels.
3. Build CNN Model: Construct a simple CNN using the Sequential API. This model has convolutional layers for
feature extraction and max-pooling layers for downsampling.
4. Compile Model: Specify the model's optimizer, loss function, and evaluation metric. The model is compiled to
prepare it for training.
5. Train Model: Train the model on the training data for 10 epochs. During training, the model learns to map
input images to their corresponding classes.
6. Evaluate Model: Evaluate the trained model on the test data to assess its performance on unseen images. The
test accuracy is printed, indicating the proportion of correctly classified images.
This code serves as an introductory example of building and training a simple CNN for image classification. It
covers key concepts such as convolutional layers, max pooling, flattening, and dense layers.
Link: https://www.kaggle.com/datasets/samuelcortinhas/cats-and-dogs-image-classification
Python Code
Step 1: Import necessary libraries
import tensorflow as tf
# Data augmentation is a technique to artificially increase the diversity of the training dataset,
# helping the model generalize better to unseen data.
train_datagen = ImageDataGenerator(
# For the validation set, we only want to rescale the pixel values, not apply augmentation.
test_datagen = ImageDataGenerator(rescale=1./255)
# It simplifies the process of organizing and loading data for training and validation.
train_generator = train_datagen.flow_from_directory(
validation_generator = test_datagen.flow_from_directory(
target_size=(150, 150),
batch_size=32,
class_mode='binary'
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Flatten())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
# Compile the model with binary crossentropy loss for binary classification,
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# Train the model using the 'fit' method with the training generator.
# Evaluate the model on the validation data and print the validation accuracy.
# The 'evaluate' method uses the validation generator to generate batches of images.
Explanation:
- The `ImageDataGenerator` is used to apply data augmentation during training. It rescales pixel values to the range [0,
1], shears the images, applies random zoom transformations, and flips images horizontally.
- Data augmentation helps the model generalize better by exposing it to a wider variety of training examples.
2. flow_from_directory:
- `flow_from_directory` simplifies the process of loading images from directories. It automatically assigns labels based
on the subdirectory names (in this case, 'cat' and 'dog').
- It generates batches of augmented images on-the-fly, making it efficient for training on large datasets.
3. Model Building:
- A simple CNN model is constructed with convolutional layers for feature extraction, max-pooling layers for down-
sampling, and dense layers for classification.
4. Model Compilation:
- The model is compiled with binary crossentropy loss, suitable for binary classification (cat or dog), and the Adam
optimizer.
5. Model Training:
- The model is trained using the `fit` method, utilizing the training generator. The validation generator is used to
monitor the model's performance on unseen data during training.
6. Model Evaluation:
- The trained model is evaluated on the validation data using the `evaluate` method, and the validation accuracy is
printed.
Data Augmentation: `ImageDataGenerator` allows us to apply real-time data augmentation during training, enhancing
the model's ability to generalize to unseen data.
Organized Directory Structure: `flow_from_directory` simplifies the loading of images when they are organized into
subdirectories, each representing a different class. This structure is common in many image classification datasets.
Why Data Generators and `flow_from_directory` not used in First Example (CIFAR-10):
Standardized Dataset: CIFAR-10 is a standardized dataset that comes in a specific format. It doesn't require extensive
data augmentation, and the data is organized in a way that can be directly loaded using `cifar10.load_data()` without the
need for `flow_from_directory`.
Smaller Dataset: CIFAR-10 is a relatively small dataset compared to some others, and the initial example aimed for
simplicity without the need for additional data augmentation techniques.