AlexNet Implementation Using Keras Library

AlexNet Implementation Using Keras Library

1- Introduction:

Alex Krizhevsky, Geoffrey Hinton and Ilya Sutskever done amazing job by developing an amazing neural network architecture called ‘AlexNet’ and won Image Classification Challenge Award (ILSVRC) in 2012. They trained alexnet on 1.2 million high-resolution images into 1000 different classes with 60 million parameters and 650,000 neurons. The training of alexnet was done on two GPUs with split layer concept because GPUs were a little bit slow at that time.

2- AlexNet Architecture:

The AlexNet architecture contain five convolutional layers, some of layers are followed by maximum pooling layers and then three fully-connected layers and finally a 1000-way softmax classifier.

In the paper they published, all the layers are they divided into two layers to train them on separate GPUs. So it is complecated arrangement and hard to understand, we are here to implement AlexNet model in one layer concept.

First Layer:

Input required for AlexNet is a 227x227x3 RGB images which passes through the first convolutional layer with 96 feature maps or filters having size 11×11 and a stride of 4. The image dimensions changes to 55x55x96.

Then the AlexNet applies maximum pooling layer or sub-sampling layer with a filter size 3×3 and a stride of two. The resulting image dimensions will be reduced to 27x27x96.

Second Layer:

Next, there is a second convolutional layer with 256 feature maps having size 5×5 and a stride of 1.

Then there is again a maximum pooling layer with filter size 3×3 and a stride of 2. This layer is same as the second layer except it has 256 feature maps so the output will be reduced to 13x13x256.

Third, Fourth and Fifth Layers:

The third, fourth and fifth layers are convolutional layers with filter size 3×3 and a stride of one. The first two used 384 feature maps where the third used 256 filters.

The three convolutional layers are followed by a maximum pooling layer with filter size 3×3, a stride of 2 and have 256 feature maps.

Sixth Layer:

The convolutional layer output is flatten through a fully connected layer with 9216 feature maps each of size 1×1.

Seventh and Eighth Layers:

Next is again two fully connected layers with 4096 units.

Output Layer:

Finally, there is a softmax output layer ŷ with 1000 possible values.

Summary of AlexNet Architecture

3- Define the AlexNet Model in Keras

Code

import keras

from keras.models import Sequential

from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D

from keras.layers.normalization import BatchNormalization

import numpy as np

np.random.seed(1000)

#Instantiate an empty model

model = Sequential()

 

# 1st Convolutional Layer

model.add(Conv2D(filters=96, input_shape=(224,224,3), kernel_size=(11,11), strides=(4,4), padding=’valid’))

model.add(Activation(‘relu’))

# Max Pooling

model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding=’valid’))

 

# 2nd Convolutional Layer

model.add(Conv2D(filters=256, kernel_size=(11,11), strides=(1,1), padding=’valid’))

model.add(Activation(‘relu’))

# Max Pooling

model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding=’valid’))

 

# 3rd Convolutional Layer

model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding=’valid’))

model.add(Activation(‘relu’))

 

# 4th Convolutional Layer

model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding=’valid’))

model.add(Activation(‘relu’))

 

# 5th Convolutional Layer

model.add(Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding=’valid’))

model.add(Activation(‘relu’))

# Max Pooling

model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding=’valid’))

 

# Passing it to a Fully Connected layer

model.add(Flatten())

# 1st Fully Connected Layer

model.add(Dense(4096, input_shape=(224*224*3,)))

model.add(Activation(‘relu’))

# Add Dropout to prevent overfitting

model.add(Dropout(0.4))

 

# 2nd Fully Connected Layer

model.add(Dense(4096))

model.add(Activation(‘relu’))

# Add Dropout

model.add(Dropout(0.4))

 

# 3rd Fully Connected Layer

model.add(Dense(1000))

model.add(Activation(‘relu’))

# Add Dropout

model.add(Dropout(0.4))

 

# Output Layer

model.add(Dense(17))

model.add(Activation(‘softmax’))

 

model.summary()

 

# Compile the model

model.compile(loss=keras.losses.categorical_crossentropy, optimizer=’adam’, metrics=[“accuracy”])

Enjoyed this article? Stay informed by joining our newsletter!

Comments

You must be logged in to post a comment.

About Author