Skip to content

Instantly share code, notes, and snippets.

@ShaonBhattaShuvo
Last active November 25, 2022 19:22
Show Gist options
  • Save ShaonBhattaShuvo/cf36e7e9b9275d315912555d4f963ce5 to your computer and use it in GitHub Desktop.
Save ShaonBhattaShuvo/cf36e7e9b9275d315912555d4f963ce5 to your computer and use it in GitHub Desktop.
cnn-implementation.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOCaV5pipkgh3i+MM7KDVG/",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ShaonBhattaShuvo/cf36e7e9b9275d315912555d4f963ce5/cnn-implementation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"\"\"\"\n",
"@author: Shaon Bhatta Shuvo\n",
"\"\"\"\n",
"#Importing Libraries"
],
"metadata": {
"id": "e0oXBqyqUSdt"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import tensorflow.keras as tfk\n",
"from sklearn import metrics \n",
"from keras.models import Sequential\n",
"from keras.layers import Conv2D, MaxPooling2D,Flatten,Dense,Dropout,GlobalAveragePooling2D "
],
"metadata": {
"id": "RcsrJeLkUSGw"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#loading the datset from keras "
],
"metadata": {
"id": "Yql3XcawaK0N"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yN2WQCtJT7__"
},
"outputs": [],
"source": [
"mnist = tfk.datasets.mnist\n",
"(X_train, y_train), (X_test, y_test) = mnist.load_data() #X indiciates images and y indicates lables"
]
},
{
"cell_type": "markdown",
"source": [
"#Basic dataset info\n"
],
"metadata": {
"id": "bQAJ6Jx4aeyo"
}
},
{
"cell_type": "code",
"source": [
"print(\"Number of distict labels or classes: \", np.unique(y_test))\n",
"print(\"Number of train images: \",len(y_train))\n",
"print(\"Number of test images: \",len(y_test))"
],
"metadata": {
"id": "YVNWEV2NaeJG"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#some sample dataset images\n"
],
"metadata": {
"id": "stO80U2HUPMa"
}
},
{
"cell_type": "code",
"source": [
"print(\"Dataset Samples: \")\n",
"class_names = ['Class 0','Class 1','Class 2','Class 3','Class 4','Class 5','Class 6','Class 7','Class 8','Class 9']\n",
"plt.figure(figsize=(10,10))\n",
"for i in range(25):\n",
" plt.subplot(5,5,i+1)\n",
" plt.xticks([])\n",
" plt.yticks([])\n",
" plt.grid(False)\n",
" plt.imshow(X_train[i],)\n",
" plt.xlabel(class_names[y_train[i]])\n",
"plt.show()\n"
],
"metadata": {
"id": "K4vFdGKAbDj3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Data Preprocessing\n",
"setting the input shape based on image data, if you work with color image then put 3 instead of 1. \n"
],
"metadata": {
"id": "pRCmugXHh-sR"
}
},
{
"cell_type": "code",
"source": [
"img_rows, img_cols = 28, 28\n",
"import keras.backend as K\n",
"if K.image_data_format() == 'channels_first':\n",
" X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)\n",
" X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)\n",
" input_shape = (1, img_rows, img_cols)\n",
"else:\n",
" X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)\n",
" X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)\n",
" input_shape = (img_rows, img_cols, 1)"
],
"metadata": {
"id": "X16I8-ZViEkJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Data Rescaling: \n",
"All the images are consist of RGB values with pixel range from 0 to 255. However, we need to rescale \n",
"these values between 0 to 1, because values in range 0 to 255 would be too high for our model to process."
],
"metadata": {
"id": "N-z7yhVjbN7U"
}
},
{
"cell_type": "code",
"source": [
"X_train = X_train.astype('float32')/255\n",
"X_test = X_test.astype('float32')/255\n",
"\n",
"y_train = y_train.astype('int32')\n",
"y_test = y_test.astype('int32')"
],
"metadata": {
"id": "E8h8EaJQbYxQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Reserve 10,000 samples for Validation Set\n"
],
"metadata": {
"id": "fb2RoV6ndk37"
}
},
{
"cell_type": "code",
"source": [
"X_val = X_train[-10000:]\n",
"y_val = y_train[-10000:]\n",
"X_train = X_train[:-10000]\n",
"y_train = y_train[:-10000]"
],
"metadata": {
"id": "MNsmD8Pldn6x"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Dataset info"
],
"metadata": {
"id": "yfAx7lkgmxyc"
}
},
{
"cell_type": "code",
"source": [
"print('X_train shape:', X_train.shape)\n",
"print(X_train.shape[0], 'train samples')\n",
"print(X_test.shape[0], 'test samples')\n",
"print(X_val.shape[0], 'validation samples')"
],
"metadata": {
"id": "P7wO7Pkvm3Nz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Building a CNN model "
],
"metadata": {
"id": "DdIsEO5Bg87F"
}
},
{
"cell_type": "code",
"source": [
"model = Sequential()\n",
"model.add(Conv2D(64, kernel_size=(3, 3),input_shape=input_shape,activation='relu')) #First Convolutional Layer\n",
"#model.add(Conv2D(32, (3, 3), activation='relu')) #Second Convolutional Layer\n",
"model.add(MaxPooling2D(pool_size=(2, 2))) #Max Pooling Layer\n",
"#model.add(Dropout(0.25)) #Adding Dropout Regularization \n",
"model.add(Flatten()) #Flattening the matrix into vector \n",
"model.add(Dense(50, activation='relu')) #Adding NN layer (Fully Connected Layer)\n",
"model.add(Dense(100, activation='relu')) #Adding One more hidden NN layer\n",
"#model.add(Dropout(0.5)) #Adding Dropout Regularization to the Fully Connected Layer \n",
"model.add(Dense(10, activation='softmax')) #Output Layer"
],
"metadata": {
"id": "HVcN6WIkhDwT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Understanding the fileters"
],
"metadata": {
"id": "Ci-esbsVJoJf"
}
},
{
"cell_type": "code",
"source": [
"#Let's take first hidden layer as the layer of interest.\n",
"layer = model.layers #Conv layers at 0\n",
"print(layer)\n",
"filters, biases = model.layers[0].get_weights()\n",
"print(layer[0].name, filters.shape)"
],
"metadata": {
"id": "_LugvdnKJnO5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Ploting the filters"
],
"metadata": {
"id": "1bri_YsGLL6w"
}
},
{
"cell_type": "code",
"source": [
"fig1=plt.figure(figsize=(8, 12))\n",
"columns = 8\n",
"rows = 8\n",
"n_filters = columns * rows\n",
"for i in range(1, n_filters +1):\n",
" f = filters[:, :, :, i-1]\n",
" fig1 =plt.subplot(rows, columns, i)\n",
" fig1.set_xticks([]) #Turn off axis\n",
" fig1.set_yticks([])\n",
" plt.imshow(f[:, :, 0], cmap='gray') #Show only the filters from 0th channel (R)\n",
" #ix += 1\n",
"plt.show()"
],
"metadata": {
"id": "1gOyR-s2LML8"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Compiling the model"
],
"metadata": {
"id": "87BJYac9iL-J"
}
},
{
"cell_type": "code",
"source": [
"model.compile(optimizer='adam', \n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['sparse_categorical_accuracy'])\n"
],
"metadata": {
"id": "AatkEcs1iMaJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Model's Summary"
],
"metadata": {
"id": "ZCUlQMxVidr5"
}
},
{
"cell_type": "code",
"source": [
"model.summary()"
],
"metadata": {
"id": "HQ5r9D1jicw6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Training the model \n"
],
"metadata": {
"id": "qHv8InrNjG-b"
}
},
{
"cell_type": "code",
"source": [
"training = model.fit(X_train,y_train, epochs = 25, batch_size =5000, validation_data =(X_val,y_val))"
],
"metadata": {
"id": "GVFXFvSBjFCh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Visulaizing the Training and Validation Sets Loss and Accuracy\n"
],
"metadata": {
"id": "Y_YBsslQjWd4"
}
},
{
"cell_type": "code",
"source": [
"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))\n",
"#Plot training and validation accuracy values\n",
"#axes[0].set_ylim(0,1) #if we want to limit axis in certain range\n",
"axes[0].plot(training.history['sparse_categorical_accuracy'], label='Train')\n",
"axes[0].plot(training.history['val_sparse_categorical_accuracy'], label='Validation')\n",
"axes[0].set_title('Model Accuracy')\n",
"axes[0].set_xlabel('Epoch')\n",
"axes[0].set_ylabel('Accuracy')\n",
"axes[0].legend()\n",
"#Plot training and validation loss values\n",
"#axes[1].set_ylim(0,1)\n",
"axes[1].plot(training.history['loss'], label='Train')\n",
"axes[1].plot(training.history['val_loss'], label='Validation')\n",
"axes[1].set_title('Model Loss')\n",
"axes[1].set_xlabel('Epoch')\n",
"axes[1].set_ylabel('Loss')\n",
"axes[1].legend()\n",
"plt.tight_layout()\n",
"plt.show()"
],
"metadata": {
"id": "kdTyp7nAjgwg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Evaluating the performance on the Test set "
],
"metadata": {
"id": "8S20eSqljmbN"
}
},
{
"cell_type": "code",
"source": [
"test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=2)\n",
"print('Test loss: {0:.2f}, Test Accuracy: {1:.2f}%'.format(test_loss, test_accuracy*100)) \n"
],
"metadata": {
"id": "9ApTTubwjnVp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Checking with prediction value\n",
"\n",
"\n",
"\n"
],
"metadata": {
"id": "vPfVU5H1TFU-"
}
},
{
"cell_type": "code",
"source": [
"from numpy.core.fromnumeric import argmax\n",
"prediction = model.predict(X_test)\n",
"print(prediction)\n",
"#print(prediction[0])\n",
"#print(argmax(prediction[0]))\n",
"#print(y_test[0])"
],
"metadata": {
"id": "xS8jktFMTGWt"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Flattening dataset to implement NN model. "
],
"metadata": {
"id": "o6e0KGViqTt3"
}
},
{
"cell_type": "code",
"source": [
"X_train = X_train.reshape(50000, 784)\n",
"X_val = X_val.reshape(10000, 784)\n",
"X_test = X_test.reshape(10000, 784)"
],
"metadata": {
"id": "w40-Xxdkmmft"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Dataset Info"
],
"metadata": {
"id": "Pt6Oy6GVwaAH"
}
},
{
"cell_type": "code",
"source": [
"print('X_train shape:', X_train.shape)\n",
"print(X_train.shape[0], 'train samples')\n",
"print(X_test.shape[0], 'test samples')\n",
"print(X_val.shape[0], 'validation samples')"
],
"metadata": {
"id": "FCa9lSQFwdui"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Building the model (NN)"
],
"metadata": {
"id": "7vt0X19BwLDh"
}
},
{
"cell_type": "code",
"source": [
"model = tfk.Sequential()\n",
"model.add(tfk.layers.Dense(50,input_shape=(784,), activation='relu')) #First Hidden Layer\n",
"model.add(tfk.layers.Dense(100, activation='relu')) #Second Hidden Layer\n",
"model.add(tfk.layers.Dense(10, activation='softmax')) #Output Layer"
],
"metadata": {
"id": "RRmx1APtqcGA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Compiling the model"
],
"metadata": {
"id": "6cnjeoXPrC7T"
}
},
{
"cell_type": "code",
"source": [
"model.compile(optimizer='adam', \n",
" loss='sparse_categorical_crossentropy',\n",
" metrics=['sparse_categorical_accuracy'])\n"
],
"metadata": {
"id": "eA4J5K2urFje"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Model's Summary"
],
"metadata": {
"id": "1tGOE-f8rPAl"
}
},
{
"cell_type": "code",
"source": [
"model.summary()"
],
"metadata": {
"id": "RSgedO7greIH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Training the model \n"
],
"metadata": {
"id": "tUsy4ySXrY2H"
}
},
{
"cell_type": "code",
"source": [
"training = model.fit(X_train,y_train, epochs = 25, batch_size =5000, validation_data =(X_val,y_val))"
],
"metadata": {
"id": "XIwXuFkGr0Bi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Visulaizing the Training and Validation Sets Loss and Accuracy\n"
],
"metadata": {
"id": "0TtdLBkYsQCv"
}
},
{
"cell_type": "code",
"source": [
"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(8,4))\n",
"#Plot training and validation accuracy values\n",
"#axes[0].set_ylim(0,1) #if we want to limit axis in certain range\n",
"axes[0].plot(training.history['sparse_categorical_accuracy'], label='Train')\n",
"axes[0].plot(training.history['val_sparse_categorical_accuracy'], label='Validation')\n",
"axes[0].set_title('Model Accuracy')\n",
"axes[0].set_xlabel('Epoch')\n",
"axes[0].set_ylabel('Accuracy')\n",
"axes[0].legend()\n",
"#Plot training and validation loss values\n",
"#axes[1].set_ylim(0,1)\n",
"axes[1].plot(training.history['loss'], label='Train')\n",
"axes[1].plot(training.history['val_loss'], label='Validation')\n",
"axes[1].set_title('Model Loss')\n",
"axes[1].set_xlabel('Epoch')\n",
"axes[1].set_ylabel('Loss')\n",
"axes[1].legend()\n",
"plt.tight_layout()\n",
"plt.show()"
],
"metadata": {
"id": "uYASI4y6sXUf"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"#Evaluating the performance on the Test set"
],
"metadata": {
"id": "8vukVZGSsDzh"
}
},
{
"cell_type": "code",
"source": [
"test_loss, test_accuracy = model.evaluate(X_test, y_test, verbose=2)\n",
"print('Test loss: {0:.2f}, Test Accuracy: {1:.2f}%'.format(test_loss, test_accuracy*100)) \n"
],
"metadata": {
"id": "Sc09yURTtM1m"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment