0% found this document useful (0 votes)
74 views7 pages

CNN Implementation in Python

This document describes two examples of multiclass image classification using convolutional neural networks. The first example uses a simple CNN to classify images from the CIFAR-10 dataset into 10 classes. It loads and preprocesses the data, builds a CNN model, compiles and trains it, and evaluates performance. The second example performs binary classification of cat and dog images. It uses data augmentation, loads images with flow_from_directory, builds and trains a CNN, and evaluates it on a validation set.

Uploaded by

Muhammad Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
74 views7 pages

CNN Implementation in Python

This document describes two examples of multiclass image classification using convolutional neural networks. The first example uses a simple CNN to classify images from the CIFAR-10 dataset into 10 classes. It loads and preprocesses the data, builds a CNN model, compiles and trains it, and evaluates performance. The second example performs binary classification of cat and dog images. It uses data augmentation, loads images with flow_from_directory, builds and trains a CNN, and evaluates it on a validation set.

Uploaded by

Muhammad Usman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

1.

Multiclass Image Classification on CIFAR-10 Dataset:


 Python CNN Code:

 Step 1: Import necessary libraries

import tensorflow as tf

from tensorflow.keras import layers, models

from tensorflow.keras.datasets import cifar10

from tensorflow.keras.utils import to_categorical

 Step 2: Load and preprocess the CIFAR-10 dataset

(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

# Normalize pixel values to be between 0 and 1

train_images, test_images = train_images / 255.0, test_images / 255.0

# One-hot encode labels to convert them from class indices to binary vectors

train_labels, test_labels = to_categorical(train_labels), to_categorical(test_labels)

 Step 3: Build a Simple CNN Model Architecture

model = models.Sequential()

# Add a 2D convolutional layer with 32 filters, each of size (3, 3), and ReLU activation

model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))

# Add a max pooling layer to downsample the spatial dimensions

model.add(layers.MaxPooling2D((2, 2)))

# Add another 2D convolutional layer with 64 filters and ReLU activation

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Add another max pooling layer

model.add(layers.MaxPooling2D((2, 2)))
# Add a third 2D convolutional layer with 64 filters and ReLU activation

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

# Flatten the 3D output to 1D, preparing it for the fully connected layers

model.add(layers.Flatten())

# Add a fully connected layer with 64 neurons and ReLU activation

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'))

 Step 4: Compile, Train and Evaluate the model

# Specify the optimizer, loss function, and evaluation metric

model.compile(optimizer='adam',

loss='categorical_crossentropy',

metrics=['accuracy'])

# Train the model on the training data for 10 epochs

# Validation data is used to monitor the model's performance on unseen data during training

model.fit(train_images, train_labels, epochs=10, validation_data=(test_images, test_labels))

# Evaluate the trained model on the test data and print the accuracy

test_loss, test_acc = model.evaluate(test_images, test_labels)

print(f'Test accuracy: {test_acc}')

 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.

2. Binary Image Classification b/w Cats and Dogs


Below is a simple example of a Convolutional Neural Network (CNN) in Python using TensorFlow and Keras to
classify images as either cats or dogs. This example uses the "Dogs vs. Cats" dataset from Kaggle. Ensure you
have the dataset downloaded and organized into separate folders for training and testing before running the
code. Let’s say, we use the following dataset from Kaggle.com

Link: https://www.kaggle.com/datasets/samuelcortinhas/cats-and-dogs-image-classification

 Python Code
 Step 1: Import necessary libraries

import tensorflow as tf

from tensorflow.keras import layers, models

from tensorflow.keras.preprocessing.image import ImageDataGenerator

 Step 2: Set up data augmentation and load data

# Data augmentation is a technique to artificially increase the diversity of the training dataset,
# helping the model generalize better to unseen data.

# It involves randomly applying transformations to the input images.

train_datagen = ImageDataGenerator(

rescale=1./255, # Normalize pixel values to be in the range [0,1]

shear_range=0.2, # Apply random shear transformations

zoom_range=0.2, # Apply random zoom transformations

horizontal_flip=True # Flip images horizontally

# For the validation set, we only want to rescale the pixel values, not apply augmentation.

test_datagen = ImageDataGenerator(rescale=1./255)

# The 'flow_from_directory' method is used to load images from directories,

# where each subdirectory represents a different class.

# It simplifies the process of organizing and loading data for training and validation.

train_generator = train_datagen.flow_from_directory(

'train', # Path to the training data directory

target_size=(150, 150), # Resize images to a consistent size

batch_size=32, # Number of samples per batch

class_mode='binary' # Binary classification: cat or dog

validation_generator = test_datagen.flow_from_directory(

'validation', # Path to the validation data directory

target_size=(150, 150),

batch_size=32,

class_mode='binary'

 Step 3: Build a simple CNN Model Architecture

# The model consists of convolutional layers for feature extraction,

# max-pooling layers for down-sampling, and dense layers for classification.


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(128, activation='relu'))

model.add(layers.Dense(1, activation='sigmoid'))

 Step 4: Compile, Train and Evaluate the model

# Compile the model with binary crossentropy loss for binary classification,

# and use the Adam optimizer for training.

model.compile(optimizer='adam',

loss='binary_crossentropy',

metrics=['accuracy'])

# Train the model using the 'fit' method with the training generator.

# The generator provides batches of augmented images during training.

# The validation generator is used to monitor performance on unseen data.

model.fit(train_generator, epochs=10, validation_data=validation_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.

test_loss, test_acc = model.evaluate(validation_generator)

print(f'Validation accuracy: {test_acc}')

 Explanation:

1. Data Augmentation and ImageDataGenerator:

- 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.

Why Data Generators and `flow_from_directory` in this example:


Large Dataset: Data generators are particularly useful when dealing with large datasets that may not fit into memory.

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.

You might also like