10. Convolutional Neural Network

arrow_back Back to Experiments

10. Convolutional Neural Network

Aim

    To write a Python program for CNN using Keras with TensorFlow backend.

Understand the Convolutional Neural Network Before You Begin

Overview: A Convolutional Neural Network (CNN) is a type of deep learning algorithm primarily used for processing and analyzing visual data such as images and videos. CNNs automatically learn important patterns and features from input data using convolutional layers, which apply small filters to detect local patterns like edges, textures, and shapes. These learned features help the model recognize complex objects in images.

A typical CNN architecture consists of several layers including convolution layers, activation functions, pooling layers, and fully connected layers. Convolution layers extract spatial features, pooling layers reduce the dimensionality of the feature maps, and fully connected layers perform the final classification. CNNs are widely used in applications such as image recognition, object detection, medical image analysis, and facial recognition.

Further Understanding: Convolutional Neural Network (CNN)

Algorithm

  1. Import Libraries and Modules:Import necessary libraries and modules from Keras, including layers for building the neural network, and from sklearn for data handling.
  2. Set Configuration: Define hyperparameters such as batch size, number of epochs, and number of classes.
  3. Load and Inspect Data:Load the Fashion MNIST dataset and inspect its shape to understand the dimensions of training and testing data.
  4. Preprocess Data:
    • Reshape image data to add a channel dimension, making it suitable for convolutional layers.
    • Convert pixel values to float type and normalize them to the range 0-1.
    • Convert labels from categorical integers to one-hot encoded vectors.
  5. Split Training Data:Split the training data into training and validation sets using sklearn's train_test_split.
  6. Build CNN Model:
    • Initialize a sequential model.
    • Add convolutional layers with specified filters, kernel size, activation function, and padding.
    • Include LeakyReLU activation functions for non-linearity, and max pooling layers for downsampling.
    • Flatten the output to make it suitable for the dense layer.
    • Add fully connected layers with specified neurons and activation functions for classification.
  7. Compile the Model:Configure the learning process by specifying the optimizer, loss function, and metrics for evaluation.
  8. Model Summary:Display the structure of the model, showing all layers along with their shapes and number of parameters.
  9. Train the Model:Fit the model on the training data with specified batch size and epochs, validating it on a separate set of data.
  10. Evaluate the Model:Test the model on unseen test data to evaluate its performance and print out the loss and accuracy metrics.
  11. Print Model Outputs:Output key results including test loss and accuracy to understand how well the model performed.

About the Dataset

The Fashion-MNIST dataset contains 28x28 grayscale images of 70,000 fashion items from ten categories, with 7,000 images per category, from Zalando's article images. The training set contains 60,000 pictures, while the test set contains 10,000. Fashion-MNIST is a dataset that is identical to the MNIST dataset, which you might already be familiar with and use to identify handwritten digits. That is, the image measurements, preparation, and test splits are all identical to those found in the MNIST dataset..

Simulation

Image Classification using CNN on Handwritten Digits..

Pre-Lab Questions

  1. What are the functions of different components of CNN used in the program?
  2. What is need for reshaping the training and testing data?

Post-Lab Questions

  1. Modify the code of the CNN for a different dataset and comment on the results?
  2. Using code examples, explain how the output of the CNN classifier changes as the amount of training data increases or decreases.
  3. To check the output of the provided code, expand the dataset subroutine to include more training data and also decrease the training data?

Result

The CNN model was successfully implemented and trained on the Fashion-MNIST dataset using Keras. It achieved a test accuracy of ________% with a test loss of _________.