0% found this document useful (0 votes)
3 views8 pages

Explanation of CNN

The document explains the process of building and training a Convolutional Neural Network (CNN) using the Keras API in TensorFlow to classify images from the CIFAR-10 dataset. It covers data loading, preprocessing, model architecture including convolutional and dense layers, data augmentation, training, and evaluation steps. The model is designed to improve performance and reduce overfitting through techniques like batch normalization and dropout.

Uploaded by

justpics.tanvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Explanation of CNN

The document explains the process of building and training a Convolutional Neural Network (CNN) using the Keras API in TensorFlow to classify images from the CIFAR-10 dataset. It covers data loading, preprocessing, model architecture including convolutional and dense layers, data augmentation, training, and evaluation steps. The model is designed to improve performance and reduce overfitting through techniques like batch normalization and dropout.

Uploaded by

justpics.tanvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Explanation of the Code:

1. Data Loading and Preprocessing: We first load the CIFAR-10


dataset, normalize the images, and one-hot encode the labels.
2. CNN Architecture: The model consists of a series of convolutional
(Conv2D) and max pooling (MaxPooling2D) layers, interspersed with
batch normalization (BatchNormalization) and dropout layers
(Dropout) to improve convergence and reduce overfitting.
 Convolutional Layers: These layers extract features from
the input images using filters. ReLU activation is used for
introducing non-linearity.
 Batch Normalization: This normalizes the activations from
the previous layer, which can speed up training and improve
the performance of the model.
 Max Pooling: This reduces the spatial dimensions (width and
height) of the input volume for the next convolutional layer.
 Dropout: This randomly sets a fraction rate of input units to 0
at each update during training, which helps to prevent
overfitting.
3. Flattening: After the convolutional blocks, the high-level reasoning
in the neural network is done via dense layers. Before passing to
these, we must flatten the 3D outputs to 1D.
4. Dense Layers: The flattened feature map is fed into dense (fully
connected) layers with ReLU activation. Dropout is also applied here
for regularization.
5. Output Layer: The final dense layer has 10 units (one for each
class) with a softmax activation function, which is standard for
multi-class classification problems.

Python code is for training a Convolutional Neural Network (CNN) using


the Keras API in TensorFlow. It is designed to classify images from the
CIFAR-10 dataset, which consists of 60,000 32x32 color images in 10
classes. The code includes data preprocessing, model definition, data
augmentation, model training, and evaluation. Below, I'll explain each
step:
1. Import necessary modules:

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

This imports TensorFlow and specific modules from Keras that will be used
for creating datasets, layers, models, and preprocessing tools.

2. Load and preprocess the dataset:

(train_images, train_labels), (test_images, test_labels) =


datasets.cifar10.load_data()
This line loads the CIFAR-10 dataset from Keras' built-in dataset collection.
The dataset is split into training and test sets.
3. Normalize pixel values:
train_images, test_images = train_images / 255.0, test_images / 255.0

The pixel values in images are integers in the range [0, 255]. This line
normalizes the pixel values to be in the range [0, 1] by dividing by 255,
which is a common preprocessing step for neural network inputs.
4. Convert class vectors to binary class matrices:

train_labels = tf.keras.utils.to_categorical(train_labels, 10)


test_labels = tf.keras.utils.to_categorical(test_labels, 10)

The CIFAR-10 dataset has integer labels. This step converts the integer
labels into a one-hot encoded format, which is a binary matrix
representation of the labels.
5. Define the CNN architecture:
The following block of code defines a sequential CNN model:
model = models.Sequential()
This creates a Sequential model, which is a linear stack of layers.

6. Add layers to the model:


The model adds several convolutional layers (Conv2D), each followed by
batch normalization (BatchNormalization), and some followed by max
pooling (MaxPooling2D). Dropout layers are added to help prevent
overfitting.
 Convolutional layers are the core building blocks of CNNs and are
responsible for the convolution operations that detect patterns in
images.
 Batch normalization is used to stabilize the learning process and
drastically reduce the number of training epochs required.
 Max pooling layers are used to reduce the spatial dimensions of
the output volume.
 Dropout layers randomly set input units to 0 with a frequency of
the rate at each step during training, which helps prevent
overfitting.
After the convolutional blocks, the output is flattened from a 3D to a 1D
tensor to be fed into a dense layer.
6. Add fully connected layers:

model.add(layers.Dense(128, activation='relu'))
This is a fully connected (dense) layer with 128 neurons and ReLU
activation. It is followed by batch normalization and dropout.
8. Add the output layer:

model.add(layers.Dense(10, activation='softmax'))
This is the output layer with 10 neurons (one for each class in CIFAR-10)
with a softmax activation function for multi-class classification.
9. Compile the model:
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
The model is compiled with the Adam optimizer, categorical crossentropy
loss function, and it will track accuracy as a metric.

10. Set up data augmentation:

datagen = preprocessing.image.ImageDataGenerator(...)
datagen.fit(train_images)
Data augmentation is configured to artificially expand the dataset using
random transformations (like rotations and
shifts). datagen.fit(train_images) computes any necessary statistics for
the transformations. In this case, it's not strictly necessary, as the
transformations do not require it.

11. Train the model:

history = model.fit(...)
This line trains the model with the dataset. Data augmentation is applied
on the fly to the training images. The model trains for a specified number
of epochs and batch size, and it uses the test set for validation after each
epoch.

12. Evaluate the model:


test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
After the model is trained, it is evaluated on the test dataset to determine
the test loss and accuracy.
In Keras, when training a model using the fit method, you can specify the
verbosity of the output with the verbose argument. The verbose argument
allows you to control how much information you want to see during the
training process. It accepts three possible values:
 verbose=0: Silent mode. No output is displayed during training.
 verbose=1: Progress bar. This is the default mode and shows a
progress bar along with one line per epoch for loss and accuracy
metrics.
 verbose=2: One line per epoch. Instead of a progress bar, it will
display one line per epoch, showing the epoch number, training
time, loss, and any other metrics you have chosen to include, such
as accuracy.
So, when you set verbose=2 in the fit or evaluate method, you will see
one line of output for each epoch during training or a single line of output
when evaluating the model, without a progress bar. This can be useful for
reducing the amount of logging information, especially when you are
running training for many epochs or when you use automated scripts and
prefer a cleaner log file.

13. Print test results:


print(f'Test accuracy: {test_acc}, Test loss: {test_loss}')
Finally, the test accuracy and loss are printed out to give you an idea of
how well the model performs on unseen data.

Another representation of the same model:

1. Import TensorFlow and related modules


import tensorflow as tf
from tensorflow.keras import datasets, layers, models, preprocessing
Here, you're importing TensorFlow, which is an open-source library for
machine learning. You're also importing specific modules from keras,
TensorFlow's high-level API for building and training deep learning models.
2. Load and preprocess the dataset
(train_images, train_labels), (test_images, test_labels) =
datasets.cifar10.load_data()
This line loads the CIFAR-10 dataset, which includes 50,000 training
images and 10,000 test images, each labeled with one of 10 classes.
train_images, test_images = train_images / 255.0, test_images / 255.0
Pixel values in the images are integers ranging from 0 to 255. Dividing by
255 normalizes these values to a range of 0 to 1, which helps with the
convergence of the training process.
train_labels = tf.keras.utils.to_categorical(train_labels, 10)
test_labels = tf.keras.utils.to_categorical(test_labels, 10)
This converts the class vectors (integers) to binary class matrices using
one-hot encoding. It's a necessary preprocessing step for multi-class
classification with a softmax output, which requires the labels to be in a
format where each label is a vector of zeros except for the index of the
class, which is set to one.
3. Define the CNN architecture

model = models.Sequential()
This initializes a sequential model, which is a linear stack of layers. You
then add several layers to the model:
 Conv2D layers for convolutional operations that are fundamental to
a CNN. These help the model to learn image features.
 BatchNormalization layers to normalize the activations from the
previous layer, which can stabilize learning.
 MaxPooling2D layers to downsample the spatial dimensions of the
output volumes.
 Dropout layers to reduce overfitting by randomly setting a fraction
of input units to 0 at each update during training time.
The CNN has 3 blocks of convolutional, batch normalization, and max
pooling layers, with increasing numbers of filters (32, 64, 128). After these
blocks, the model includes one fully connected Dense layer, followed by a
final Dense layer that outputs the probability distribution over the 10
classes using a softmax activation function.
4. Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy'])
The model is compiled with the Adam optimizer and uses categorical
crossentropy as the loss function, which is standard for multi-class
classification problems. It will also track accuracy during training and
evaluation.
5. Data augmentation to prevent overfitting
python
Copy
datagen = preprocessing.image.ImageDataGenerator(
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
zoom_range=0.2
)
datagen.fit(train_images)
You create an instance of ImageDataGenerator with certain
transformations to perform data augmentation, which can help prevent
overfitting by providing the model with slightly modified versions of the
training images. datagen.fit(train_images) is not necessary with these
settings but is included to match the general pattern when using data
augmentation (some settings do require this step).
6. Train the model
history = model.fit(datagen.flow(train_images, train_labels,
batch_size=batch_size),
epochs=epochs,
validation_data=(test_images, test_labels),
verbose=2)
The model is trained using the .fit method. datagen.flow is used to feed
the images to the model with the specified batch size and apply the data
augmentation transformations. The model is trained for a given number of
epochs (iterations over the entire dataset), and the test set is used for
validation.
7. Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
After training, the model is evaluated on the test dataset to obtain the
final loss and accuracy.
8. Print the test results

print(f'Test accuracy: {test_acc}, Test loss: {test_loss}')


Finally, the test accuracy and loss are printed out which will give you an
indication of how well the model performs on unseen data.
This code is a complete example of building and training a CNN with
modern best practices like data augmentation and batch normalization to
handle a standard image classification task.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy