Convolutinal Neural Network is also known as CNN, and it has become popular in recent times because of its usefulness. It consists of multilayer perceptrons for performing computational works. CNN is known to use very less pre-processing compared to other image classifying algorithms. It learns from filters, whereas the traditional ones uses hand-engineered algorithms. It is widely accepted that for image processing purposes CNN is the best option in python for data science.
It is a dataset of handwritten images as shown below.
It is possible to achieve 99.6% accuracy by the use of CNN along with functional model. The reason we use functional model is because it maintains easiness while connecting the layers.
- We should include the libraries first.
- Creation of test data and training data.
When we proceed further, we will use img_rows and img_cols as image dimensions. In case of mnist dataset, it is 28 and 28. It is also there to check the data format i.e., ‘channels_first’ or ‘channels_last’. In CNN, it is possible to normalize data and reduce the large terms of the calculation to smaller terms beforehand. Here we will normalize the x_train and x_test data by dividing it with 255.
Checking the data-format:
- Description of output classes:
As the output of the model can have any number from digits 0 to 9 so, 10 classes are there in the output. For making output for 10 classes we will use keras.utils.to_categorical function that will provide us with 10 columns. Out of the 10 columns one value will be 1, and the rest will be 0. The column with 1 will denote the class of the digit.
Now as the dataset is ready, we will proceed toward the CNN model.
- Calling of compile and fit function:
- Evaluate function:
The model.evaluate will provide the score for the test data. The model will now predict the class of the data, and the predicted class will be matched to y_test label for giving accuracy.