[GENERAL] Creating Convolutional Neural Network From Scratch

Using CNN on Graviti Data Platform for Image Classification Model

creating convolutional neural network from scratch

This article was originally published by Himanshu Sharma on towords data science.


Image classification basically helps us in classifying images into different labels. It is like bucketing different images into the bucket they belong to. For, e.g. a model trained to identify the image of a cat and a dog will help in segregating different images of cats and dogs respectively. There are multiple deep learning frameworks like Tensorflow, Keras, Theano, etc that can be used to create image classification models. Today we will create an image classification model from scratch using Keras and Tensorflow.

Creating image-related modeling can be done using CNN. Convolutional Neural Networks are mainly used for image-related modeling. It is one of the easiest ways to perform image classification, image detection, image segmentation, etc. It contains different types of convolutional layers that help in filtering out the most important features of the image using kernels of different sizes. Some of the most important layers are:

  1. Conv2D
    It is used to create a Convolutional kernel that is convolved with the input layer to produce the output tensor.

  2. MaxPooling2D
    It is a downsampling technique that takes the maximum value by pool size.

  3. Flatten
    It flattens the input and creates an1-D output.

There are multiple hyper-parameters that can be used accordingly to improve the model performance. These hyper-parameters may include a number of neurons, kernel size, pool size, activation function, etc.

In this article, we will create a network using CNN from scratch. I will show you how you can load data from online sources, preprocess that data and make it ready for modeling, and finally design the model architecture.

Let’s get started…

Installing required libraries

We will start by installing all required libraries. We will install Keras, TensorFlow and will also install TensorBay (SDK on Graviti data platform) that we will use for loading the dataset. The command given below will do that.

Importing required libraries

In this step, we will import all the required libraries and functions to load the data, preprocess it and create the model.

Loading the Dataset

This is the initial step, where we will load the data from TensorBay. In order to download a dataset from TensorBay, we need to create an account and fork a dataset on which we want to work, it contains a large variety of image datasets. For loading the dataset in our Jupyter notebook we need to have the access key to TensorBay that we can download from developer tools in our account.

For this article, I am using the 17 Category Flower, as you can see I have loaded it above using TensorBay.

gas = GAS("<Your Key>")
dataset = Dataset("Flower17", gas)
segment = dataset[1]

Preprocessing the Dataset

In this step, we will preprocess the data and make it ready for modeling. We will start by creating the image and labels list and loading data into it.

Next, let us visualize the number of classes that we have in this dataset.

Data Label
Data Labels(Source: By Author)

Next, we will split the dataset, normalize it and also binarize the labels.

Label Binarized
Label Binarized(Source: By Author)

Creating the Model

This is the final step, where we will create the model architecture, compile the model and train it. Before creating the model architecture, let’s split the training data into train and validation.

Model Architecture
Model Architecture(Source: By Author)
Training
Training(Source: By Author)

As you can see above we have created and trained the model using some hyper-parameters, model accuracy is not that good but you can always tune the hyper-parameters to increase the performance. We can also save the model by using the command given below.

Accuracy
Accuracy(Source: By Author)
Loss
Loss(Source: By Author)

In the end, let us also create some predictions using the model.

Prediction(Source: By Author)

Go ahead, try this with different datasets and create CNN models easily by following this article. If you want to learn more about Graviti data platform, check out another tutorial: A quick guide to OCR with Transformer.