Convolutional Neural Network:    CNN

We will first build a CNN basic Model from Scratch  with three convolutional layers, and max pooling to automatically extract features from our images and reduce the resolution of the output convolutional feature map. We assume that you know enough about CNN, so we will not cover theoretical details. Feel free to check out my book or any other resources on the web explaining convolutional neural networks! Now let us use Keras to build our CNN model architecture.

CNN basic Model from Scratch

from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.models import Sequential
from keras import optimizers

CNN basic Model from Scratch

model = Sequential()

model.add(Conv2D(16, kernel_size=(3, 3), activation=’relu’,
input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, kernel_size=(3, 3), activation=’relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))

CNN basic Model from Scratch

model.add(Conv2D(128, kernel_size=(3, 3), activation=’relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))

CNN basic Model from Scratch

model.add(Flatten())
model.add(Dense(512, activation=’relu’))
model.add(Dense(1, activation=’sigmoid’))

CNN basic Model from Scratch

model.compile(loss=’binary_crossentropy’,
optimizer=optimizers.RMSprop(),
metrics=[‘accuracy’])                                                                                                                                               model.summary()

CNN basic Model from Scratch

 

The above output shows us our summary of the basic model of CNN. As we mentioned before, we use three convolutional layers for feature extraction. The flattening layer is used to flatten 128 of the 17 x 17 feature maps we output from the third convolutional layer. This will be fed into our dense layer to get the final prediction of whether the image should be a dog (1) or a cat (0). This is the entire content of the model training process, so let’s use the following snippet using the fitting function (…) to train our model.
The following terms are very important to train our model:
1) batch size represents the total number of images passed to the model in each iteration.
2) Updates the unit weight in the layer after each iteration.
3) The total number of iterations is always equal to the total number of training samples divided by batch size
4) An epoch is when the complete data set passes through the network once, that is, all iterations are completed on a batch basis of data.
5) We use a batch size of 30 and our training data has a total of 3000 samples, which means there will be a total of 100 iterations per epoch. We trained the model for a total of 30 epochs and
therefore validated it against our validation set of 1000 images.

history = model.fit(x=train_imgs_scaled, y=train_labels_enc,
validation_data=(validation_imgs_scaled, validation_labels_enc),
batch_size=batch_size,
epochs=epochs,
verbose=1)

CNN Basic

Based on the training and validation precision values, our model appears to be a bit overfitted. We can use the following code snippets to plot the precision and errors of our model to get a better perspective.

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 4))
t = f.suptitle(‘Basic CNN Performance’, fontsize=12)
f.subplots_adjust(top=0.85, wspace=0.3)

CNN basic Model from Scratch

epoch_list = list(range(1,31))
ax1.plot(epoch_list, history.history[‘acc’], label=’Train Accuracy’)
ax1.plot(epoch_list, history.history[‘val_acc’], label=’Validation Accuracy’)
ax1.set_xticks(np.arange(0, 31, 5))
ax1.set_ylabel(‘Accuracy Value’)
ax1.set_xlabel(‘Epoch’)
ax1.set_title(‘Accuracy’)
l1 = ax1.legend(loc=”best”)

CNN basic Model from Scratch

ax2.plot(epoch_list, history.history[‘loss’], label=’Train Loss’)
ax2.plot(epoch_list, history.history[‘val_loss’], label=’Validation Loss’)
ax2.set_xticks(np.arange(0, 31, 5))
ax2.set_ylabel(‘Loss Value’)
ax2.set_xlabel(‘Epoch’)
ax2.set_title(‘Loss’)
l2 = ax2.legend(loc=”best”)

CNN basic Model from Scratch

Further more you can used regularization  technique  as well as  Image augmentation  and many more  Click here

Read more:: Machine learning algorithmExplainable AI, Block chain

Show CommentsClose Comments

1 Comment

  • Karine
    Posted August 25, 2021 at 11:58 am 0Likes

    Why people still use to read news papers when in this technological globe the
    whole thing is available on web?

Leave a comment