Here we'll: Define the instantiation class, use multiple add method calls to stack the layers in the required order, and instantiate the model.
In this series of articles, we’ll show you how to use a Deep Neural Network (DNN) to estimate a person’s age from an image.
In the last article, we designed the CNN architecture for age estimation. In this – the fourth article of the series – we’ll build the network we’ve designed using the Keras framework. The Keras library helps you create CNNs with minimal code writing.
Define Instantiation Class
In the Python code below, we introduced a class with one static method for network instantiation.
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Dropout
from keras.layers.core import Flatten
from keras.layers.core import Dense
class ClassLeNet:
@staticmethod
def create(width, height, kernels, hidden, classes):
net = Sequential()
net.add(Conv2D(kernels, (5, 5), padding="same", input_shape=(height, width, 1)))
net.add(Activation("relu"))
net.add(BatchNormalization())
net.add(Conv2D(kernels, (5, 5), padding="same"))
net.add(Activation("relu"))
net.add(BatchNormalization())
net.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
net.add(Conv2D(kernels, (3, 3), padding="same"))
net.add(Activation("relu"))
net.add(BatchNormalization())
net.add(Conv2D(kernels, (3, 3), padding="same"))
net.add(Activation("relu"))
net.add(BatchNormalization())
net.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
net.add(Flatten())
net.add(Dense(hidden))
net.add(Activation("relu"))
net.add(Dropout(0.5))
net.add(Dense(hidden))
net.add(Activation("relu"))
net.add(Dense(classes))
net.add(Activation("softmax"))
return net
Import Classes and Packages
The above code imports the required Python packages and classes. Some remarks on the class names:
- The
BatchNormalization
class is an implementation of the normalization layer - The Dense class implements a fully-connected layer
- The
Flatten
class is a special layer to convert data between the multidimensional and 1D layers
We believe that the rest of the package and class names are self-explanatory.
Define the Create Method
In the class we’ve defined, the create
method has five parameters:
width
and height
– the horizontal and vertical sizes of the input image data kernels
– the number of learnable kernels in the convolutional layers hidden
– the number of neurons in the hidden fully-connected layers classes
– the number of classes to train the CNN on
As our network is a feedforward CNN, we instantiate the Sequential
model class.
Add Layers
We use multiple add
method calls to stack the layers in the required order. All constructors of the Conv2D
convolutional layers are supplied with the following parameters:
- The number of kernels
- The size of the kernels (5 x 5 or 3 x 3)
- Padding= "same," for zero padding
The first convolutional layer is created with one additional parameter, input_shape
, which specifies dimensions of the input images.
The activation layers use the ReLU activation function, which is specified by the relu
parameter value. The last activation layer is an exception: it uses the SOFTMAX function to provide the output as the probabilities per class (age group).
The pooling layers are initialized with two parameters: pool_size
of 2 x 2 and strides
of 2 x 2. These parameter values make every pooling layer reduce the width and height by a factor of two.
There are three fully-connected (Dense
) layers at the end part of the stack. Two hidden layers are instantiated with the number of neurons equal to the hidden
parameter value. The last output layer has the number of neurons equal to the class number.
There is a dropout layer between the two fully-connected layers, with the probability of 0.5. Note the Flatten
layer between the convolutional and fully-connected parts of the network. It is necessary because the convolutional output has three dimensions (width, height, and the number of kernels) while the fully connected input is one-dimensional. So, the convolved data must be converted to a 1D array before it can be used as the input for the dense layers.
Instantiate the Model
With the class for building our CNN model implemented, we can instantiate the model as follows:
from keras.optimizers import SGD
kernels = 16
hidden = 256
imgsize = 128
classes = 10
net = ClassLeNet.create(imgsize, imgsize, kernels, hidden, classes)
opt = SGD(lr=0.01)
net.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
First, we assign values to the model parameters:
- The image size is 128
- The number of learnable kernels in the convolutional layers is 16
- The number of neurons in the hidden FC layers is 256
- The number of classes (age groups) is 10
We then call the create
method to instantiate the network model with the above parameter values. Also, we need to choose the optimization method to be used for training the model. The Stochastic Gradient Descent (SGD) optimizer is instantiated with the learning rate value lr
=0.01.
Finally, the model is compiled with the above optimizer, using accuracy as the metrics, as well as cross-entropy as the loss function (a common choice for classification problems).
Next Step
In this article, we implemented and instantiated out CNN using the Keras framework. Our CNN instance is now ready for training.
The next article will show you how to feed the dataset images to the model and optimize it for the age estimation.