Jovian
⭐️
Sign In
In [ ]:
from datetime import datetime
import keras
from keras import backend as K
K.set_image_dim_ordering('th')
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D
import numpy as np


def alexnet(input_data_shape=(224, 224, 3, ), number_of_classes=10):
    model = Sequential()

    # 1st Convolutional Layer
    model.add(Conv2D(filters=96, input_shape=input_data_shape, kernel_size=(5, 5), strides=(4, 4), padding='same', activation='relu'))
    # Max Pooling
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # 2nd Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
    # Max Pooling
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # 3rd Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
    # Max Pooling
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
    # 4th Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
    # Max Pooling
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
    # 5th Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(5, 5), strides=(1, 1), padding='same', activation='relu'))
    # Max Pooling
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Flatten the feature maps to pass them to Fully Connected Layers
    model.add(Flatten())

    # Fully Connected Layers
    model.add(Dense(4096, activation='relu'))
    model.add(Dense(4096, activation='relu'))
    model.add(Dense(number_of_classes, activation='softmax'))

    model.summary()
    return model
In [ ]:
def vgg_16(input_data_shape=(224, 224, 3,), number_of_classes=10):
    model = Sequential()
    # Block 1
    model.add(Conv2D(filters=64, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=64, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Block 2
    model.add(Conv2D(filters=128, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=128, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Block 3
    model.add(Conv2D(filters=256, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=256, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=256, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Block 4
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Block 5
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(Conv2D(filters=512, input_shape=input_data_shape, kernel_size=(5, 5), padding= 'same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))

    # Flatten the feature maps to pass them to Fully Connected Layers
    model.add(Flatten())

    # fully connected layers
    model.add(Dense(4096, activation='relu'))
    model.add(Dense(4096, activation='relu'))
    model.add(Dense(number_of_classes, activation='softmax'))

    # Create model.
    model.summary()

    return model

Set the inference hyper-parameters:

In [ ]:
batch_size = 128
num_classes = 10
num_of_training_iteration = 100

Set the input image shape/size

In [ ]:
input_data_shape = (224, 224, 3)

Build AlexNet & VGG-16 networks (default input image shape is 224x224x3)

In [ ]:
alexnet_model = alexnet(input_data_shape=input_data_shape)
vgg16_model = vgg_16(input_data_shape=input_data_shape)
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_86 (Conv2D) (None, 96, 56, 1) 537696 _________________________________________________________________ max_pooling2d_60 (MaxPooling (None, 96, 28, 1) 0 _________________________________________________________________ conv2d_87 (Conv2D) (None, 256, 28, 1) 614656 _________________________________________________________________ max_pooling2d_61 (MaxPooling (None, 256, 14, 1) 0 _________________________________________________________________ conv2d_88 (Conv2D) (None, 384, 14, 1) 2457984 _________________________________________________________________ max_pooling2d_62 (MaxPooling (None, 384, 7, 1) 0 _________________________________________________________________ conv2d_89 (Conv2D) (None, 384, 7, 1) 3686784 _________________________________________________________________ max_pooling2d_63 (MaxPooling (None, 384, 4, 1) 0 _________________________________________________________________ conv2d_90 (Conv2D) (None, 256, 4, 1) 2457856 _________________________________________________________________ max_pooling2d_64 (MaxPooling (None, 256, 2, 1) 0 _________________________________________________________________ flatten_10 (Flatten) (None, 512) 0 _________________________________________________________________ dense_28 (Dense) (None, 4096) 2101248 _________________________________________________________________ dense_29 (Dense) (None, 4096) 16781312 _________________________________________________________________ dense_30 (Dense) (None, 10) 40970 ================================================================= Total params: 28,678,506 Trainable params: 28,678,506 Non-trainable params: 0 _________________________________________________________________ _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_91 (Conv2D) (None, 64, 224, 3) 358464 _________________________________________________________________ conv2d_92 (Conv2D) (None, 64, 224, 3) 102464 _________________________________________________________________ max_pooling2d_65 (MaxPooling (None, 64, 112, 2) 0 _________________________________________________________________ conv2d_93 (Conv2D) (None, 128, 112, 2) 204928 _________________________________________________________________ conv2d_94 (Conv2D) (None, 128, 112, 2) 409728 _________________________________________________________________ max_pooling2d_66 (MaxPooling (None, 128, 56, 1) 0 _________________________________________________________________ conv2d_95 (Conv2D) (None, 256, 56, 1) 819456 _________________________________________________________________ conv2d_96 (Conv2D) (None, 256, 56, 1) 1638656 _________________________________________________________________ conv2d_97 (Conv2D) (None, 256, 56, 1) 1638656 _________________________________________________________________ max_pooling2d_67 (MaxPooling (None, 256, 28, 1) 0 _________________________________________________________________ conv2d_98 (Conv2D) (None, 512, 28, 1) 3277312 _________________________________________________________________ conv2d_99 (Conv2D) (None, 512, 28, 1) 6554112 _________________________________________________________________ conv2d_100 (Conv2D) (None, 512, 28, 1) 6554112 _________________________________________________________________ max_pooling2d_68 (MaxPooling (None, 512, 14, 1) 0 _________________________________________________________________ conv2d_101 (Conv2D) (None, 512, 14, 1) 6554112 _________________________________________________________________ conv2d_102 (Conv2D) (None, 512, 14, 1) 6554112 _________________________________________________________________ conv2d_103 (Conv2D) (None, 512, 14, 1) 6554112 _________________________________________________________________ max_pooling2d_69 (MaxPooling (None, 512, 7, 1) 0 _________________________________________________________________ flatten_11 (Flatten) (None, 3584) 0 _________________________________________________________________ dense_31 (Dense) (None, 4096) 14684160 _________________________________________________________________ dense_32 (Dense) (None, 4096) 16781312 _________________________________________________________________ dense_33 (Dense) (None, 10) 40970 ================================================================= Total params: 72,726,666 Trainable params: 72,726,666 Non-trainable params: 0 _________________________________________________________________

Train the networks on MNIST dataset:

In [ ]:
alexnet_model.compile(loss=keras.losses.categorical_crossentropy, optimizer='adam', metrics=["accuracy"])
vgg16_model.compile(loss=keras.losses.categorical_crossentropy, optimizer='adam', metrics=["accuracy"])

alexnet_inference_time = []
vgg16_inference_time = []

## dummy tensor to check the inference time of each network
x_test= np.random.rand(batch_size, input_data_shape[0],input_data_shape[1],input_data_shape[2])

for _ in range(num_of_training_iteration):
    alexnet_inference_start = datetime.now()
    alexnet_inference = alexnet_model.predict_classes(x_test)
    alexnet_inference_finish = datetime.now()
    alexnet_inference_time.append(alexnet_inference_finish - alexnet_inference_start)
    
for _ in range(num_of_training_iteration):
    vgg16_inference_start = datetime.now()
    vgg16_inference = vgg16_model.predict_classes(x_test)
    vgg16_inference_finish = datetime.now()
    vgg16_inference_time.append(vgg16_inference_finish - vgg16_inference_start)
    
print("Average Inference Time for AlexNet: {}".format(np.mean(alexnet_inference_time)))
print("Average Inference Time for VGG-16: {}".format(np.mean(vgg16_inference_time)))
Average Inference Time for AlexNet: 0:00:00.205323 Average Inference Time for VGG-16: 0:00:00.414448
In [ ]:
import jovian
jovian.commit()
[jovian] Saving notebook..
In [ ]: