The building blocks of ConvNets in Keras
In this section, we will be building a simple ConvNet that can be used for classifying the MNIST characters, while at the same time, learning about the different pieces that make up modern ConvNets.
We can directly import the MNIST dataset from Keras by running the following code:
from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data()
Our dataset contains 60,000 28x28-pixel images. MNIST characters are black and white, so the data shape usually does not include channels:
x_train.shape
out: (60000, 28, 28)
We will take a closer look at color channels later, but for now, let's expand our data dimensions to show that we only have a one-color channel. We can achieve this by running the following:
import numpy as np x_train = np.expand_dims(x_train,-1) x_test = np.expand_dims(x_test,-1) x_train.shape
out: (60000, 28, 28, 1)
With the code being run, you can see that we now have a single color channel added.
Conv2D
Now we come...