Click here to Skip to main content
15,669,440 members
Articles / Artificial Intelligence / Keras
Article
Posted 22 Jul 2020

Stats

6.6K views
93 downloads
3 bookmarked

Age Estimation With Deep Learning: Building CNN

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Jul 2020CPOL3 min read
In this article we’ll build the network we’ve designed using the Keras framework.
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.

Python
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:

Python
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.

This article is part of the series 'Age Estimation with Deep Learning View All

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader VIPAKS
Russian Federation Russian Federation
EDUCATION:

Master’s degree in Mechanics.

PhD degree in Mathematics and Physics.



PROFESSIONAL EXPERIENCE:

15 years’ experience in developing scientific programs
(C#, C++, Delphi, Java, Fortran).



SCIENTIFIC INTERESTS:

Mathematical modeling, symbolic computer algebra, numerical methods, 3D geometry modeling, artificial intelligence, differential equations, boundary value problems.

Comments and Discussions

 
QuestionAge Estimation Pin
Member 1532482715-Aug-21 23:21
Member 1532482715-Aug-21 23:21 
ImportError: cannot import name 'BatchNormalization' from 'keras.layers.normalization' (C:\Users\IQBAL BAIHAQI\AppData\Roaming\Python\Python39\site-packages\keras\layers\normalization\__init__.py)

what is happen? and the file __init__.py it's nothing there can you help me?
AnswerRe: Age Estimation Pin
Sergey L. Gladkiy15-Aug-21 23:30
mvaSergey L. Gladkiy15-Aug-21 23:30 
QuestionCNN model Pin
Eddy Sels9-Aug-20 0:33
professionalEddy Sels9-Aug-20 0:33 
AnswerRe: CNN model Pin
Eddy Sels10-Aug-20 0:33
professionalEddy Sels10-Aug-20 0:33 
PraiseAge Estimation With Deep Learning: Building CNN Pin
Dan Buskirk22-Jul-20 5:31
Dan Buskirk22-Jul-20 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.