|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## MNIST Handwritten Numbers detection via CNN using Keras,TensorFlow 2.0" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "#Import Modules\n", |
| 17 | + "import tensorflow as tf\n", |
| 18 | + "import cv2 as cv\n", |
| 19 | + "import numpy as np\n", |
| 20 | + "import matplotlib.pyplot as plt\n", |
| 21 | + "import copy\n", |
| 22 | + "\n", |
| 23 | + "#Dataset.\n", |
| 24 | + "from tensorflow.keras.datasets import mnist\n", |
| 25 | + "#One-hot encoding.\n", |
| 26 | + "from tensorflow.keras.utils import to_categorical\n", |
| 27 | + "#Feedforward sequential network with no feedback loop.\n", |
| 28 | + "from tensorflow.keras.models import Sequential\n", |
| 29 | + "#Layers.\n", |
| 30 | + "from tensorflow.keras.layers import Dense, Dropout, Flatten\n", |
| 31 | + "from tensorflow.keras.layers import Conv2D, MaxPooling2D\n", |
| 32 | + "#Optimizer.\n", |
| 33 | + "from tensorflow.keras.optimizers import SGD\n", |
| 34 | + "from tensorflow.keras import backend as K\n", |
| 35 | + "#Model loader.\n", |
| 36 | + "from tensorflow.keras.models import load_model\n", |
| 37 | + "\n" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "#load the MNIST handwritten number dataset.\n", |
| 47 | + "(x_train, y_train), (x_test, y_test) = mnist.load_data()\n", |
| 48 | + "print(x_train.shape)\n", |
| 49 | + "print(x_test.shape)\n", |
| 50 | + "print(x_train[0].shape)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": null, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [], |
| 58 | + "source": [ |
| 59 | + "#Display random samples from training set.\n", |
| 60 | + "\n", |
| 61 | + "#pyplot subplot nrows,ncols,index->3,3,1\n", |
| 62 | + "subplot_num=331\n", |
| 63 | + "for index in range(0,5):\n", |
| 64 | + " random_num=np.random.randint(0,len(x_train))\n", |
| 65 | + " image=x_train[random_num]\n", |
| 66 | + " plt.subplot(subplot_num)\n", |
| 67 | + " plt.imshow(image,cmap='gray')\n", |
| 68 | + " subplot_num=subplot_num+1" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "#Preprocess the data.\n", |
| 78 | + "#Convert the image shape for Keras.\n", |
| 79 | + "#Convert labels to one-hot encoded representation.\n", |
| 80 | + "#Normalize the data.\n", |
| 81 | + "\n", |
| 82 | + "#Convert for Keras.\n", |
| 83 | + "img_rows=x_train[0].shape[0]\n", |
| 84 | + "img_cols=x_train[0].shape[1]\n", |
| 85 | + "input_img_shape=(img_rows,img_cols,1)\n", |
| 86 | + "\n", |
| 87 | + "#Reshape.\n", |
| 88 | + "x_train=x_train.reshape(x_train.shape[0],img_rows,img_cols,1)\n", |
| 89 | + "x_test=x_test.reshape(x_test.shape[0],img_rows,img_cols,1)\n", |
| 90 | + "\n", |
| 91 | + "#One Hot encoding.\n", |
| 92 | + "print('Before one-hot encoding',y_train.shape[0])\n", |
| 93 | + "\n", |
| 94 | + "y_train=to_categorical(y_train)\n", |
| 95 | + "y_test=to_categorical(y_test)\n", |
| 96 | + "\n", |
| 97 | + "print(\"After one-hot encoding\",y_train.shape[0],y_train.shape[1])\n", |
| 98 | + "num_classes=y_train.shape[1]\n", |
| 99 | + "num_pixels=x_train.shape[0]*x_train.shape[1]\n", |
| 100 | + "\n", |
| 101 | + "#Normalize the data.\n", |
| 102 | + "print(x_test.dtype)\n", |
| 103 | + "print(x_train.dtype)\n", |
| 104 | + "x_train=x_train.astype('float32')\n", |
| 105 | + "x_test=x_test.astype('float32')\n", |
| 106 | + "\n", |
| 107 | + "x_train=x_train/255.0\n", |
| 108 | + "x_test=x_test/255.0\n", |
| 109 | + "\n" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "#Build the model.\n", |
| 119 | + "\n", |
| 120 | + "model=Sequential()\n", |
| 121 | + "\n", |
| 122 | + "#First layer.\n", |
| 123 | + "#padding=0, stride=1\n", |
| 124 | + "#(nxn)*(f*f) =[((n+2p-f)/s) +1, ((n+2p-f)/s) +1]\n", |
| 125 | + "# (28x28x1)*(3x3x32)= (26x26x32)\n", |
| 126 | + "model.add(Conv2D(32, kernel_size=(3,3), activation='relu',input_shape=input_img_shape))\n", |
| 127 | + "\n", |
| 128 | + "#Second layer.(24x24x64)\n", |
| 129 | + "model.add(Conv2D(64,(3,3),activation='relu'))\n", |
| 130 | + "\n", |
| 131 | + "#Max-Pooling.(12x12x64)\n", |
| 132 | + "model.add(MaxPooling2D(pool_size=(2,2)))\n", |
| 133 | + "\n", |
| 134 | + "#Dropout\n", |
| 135 | + "model.add(Dropout(0.25))\n", |
| 136 | + "\n", |
| 137 | + "#Flatten 9216x1\n", |
| 138 | + "model.add(Flatten())\n", |
| 139 | + "\n", |
| 140 | + "#Dense Layer (1x128)\n", |
| 141 | + "model.add(Dense(128,activation='relu'))\n", |
| 142 | + "\n", |
| 143 | + "#Dropout.\n", |
| 144 | + "model.add(Dropout(0.5))\n", |
| 145 | + "\n", |
| 146 | + "#Final layer \n", |
| 147 | + "model.add(Dense(num_classes,activation='softmax'))\n", |
| 148 | + "\n", |
| 149 | + "#Compile the model.(SGD, LR=0.01, loss=CCE)\n", |
| 150 | + "model.compile(loss='categorical_crossentropy', optimizer=SGD(0.01), metrics=['accuracy'])\n", |
| 151 | + "\n", |
| 152 | + "print(model.summary())" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "#Training the model\n", |
| 162 | + "batch=5\n", |
| 163 | + "epochs=10\n", |
| 164 | + "\n", |
| 165 | + "history=model.fit(x_train,y_train,batch_size=batch,epochs=epochs,verbose=1,validation_data=(x_test,y_test))\n" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": null, |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [], |
| 173 | + "source": [ |
| 174 | + "#Evaluate the model\n", |
| 175 | + "score=model.evaluate(x_test,y_test,verbose=1)\n", |
| 176 | + "\n", |
| 177 | + "print('Test accuracy',score[1])\n", |
| 178 | + "print('Test loss',score[0])" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": null, |
| 184 | + "metadata": {}, |
| 185 | + "outputs": [], |
| 186 | + "source": [ |
| 187 | + "#Plot loss.\n", |
| 188 | + "\n", |
| 189 | + "history_dict=history.history\n", |
| 190 | + "\n", |
| 191 | + "loss_values=history_dict['loss']\n", |
| 192 | + "#validation loss\n", |
| 193 | + "val_loss=history_dict['val_loss']\n", |
| 194 | + "\n", |
| 195 | + "epochs=range(1,len(loss_values)+1)\n", |
| 196 | + "\n", |
| 197 | + "line1=plt.plot(epochs,val_loss,label='Validation loss')\n", |
| 198 | + "line2=plt.plot(epochs,loss_values,label='Training loss')\n", |
| 199 | + "plt.setp(line1,linewidth=2.0,marker='+', markersize=10.0)\n", |
| 200 | + "plt.setp(line2,linewidth=2.0,marker='3',markersize=10.0)\n", |
| 201 | + "plt.xlabel('Epochs')\n", |
| 202 | + "plt.ylabel('Loss')\n", |
| 203 | + "plt.grid(True)\n", |
| 204 | + "plt.legend()\n", |
| 205 | + "plt.show()" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": null, |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "#Plot accuracy\n", |
| 215 | + "history_dict = history.history\n", |
| 216 | + "\n", |
| 217 | + "acc_values = history_dict['accuracy']\n", |
| 218 | + "val_acc_values = history_dict['val_accuracy']\n", |
| 219 | + "epochs = range(1, len(loss_values) + 1)\n", |
| 220 | + "\n", |
| 221 | + "line1 = plt.plot(epochs, val_acc_values, label='Validation/Test Accuracy')\n", |
| 222 | + "line2 = plt.plot(epochs, acc_values, label='Training Accuracy')\n", |
| 223 | + "plt.setp(line1, linewidth=2.0, marker = '+', markersize=10.0)\n", |
| 224 | + "plt.setp(line2, linewidth=2.0, marker = '4', markersize=10.0)\n", |
| 225 | + "plt.xlabel('Epochs') \n", |
| 226 | + "plt.ylabel('Accuracy')\n", |
| 227 | + "plt.grid(True)\n", |
| 228 | + "plt.legend()\n", |
| 229 | + "plt.show()" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": null, |
| 235 | + "metadata": {}, |
| 236 | + "outputs": [], |
| 237 | + "source": [ |
| 238 | + "#Save the model.\n", |
| 239 | + "model.save('mnist_number_trained_model.h5')\n", |
| 240 | + "\n" |
| 241 | + ] |
| 242 | + }, |
| 243 | + { |
| 244 | + "cell_type": "code", |
| 245 | + "execution_count": null, |
| 246 | + "metadata": {}, |
| 247 | + "outputs": [], |
| 248 | + "source": [ |
| 249 | + "#load the saved model\n", |
| 250 | + "classifier=load_model('mnist_number_trained_model.h5')" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "code", |
| 255 | + "execution_count": null, |
| 256 | + "metadata": {}, |
| 257 | + "outputs": [], |
| 258 | + "source": [ |
| 259 | + "#Test on test data.\n", |
| 260 | + "test_image=x_test[3]\n", |
| 261 | + "print(test_image.shape)\n", |
| 262 | + "#Reshape for Keras.\n", |
| 263 | + "test_image=test_image.reshape(1,28,28,1)\n", |
| 264 | + "\n", |
| 265 | + "#res=classifier.predict_classes(test_image,1,verbose=0)\n", |
| 266 | + "#res=str(res[0])\n", |
| 267 | + "#print(res)\n", |
| 268 | + "\n", |
| 269 | + "res=classifier.predict(test_image)\n", |
| 270 | + "print(res)\n", |
| 271 | + "res=np.argmax(res)\n", |
| 272 | + "print(res)\n", |
| 273 | + "\n", |
| 274 | + "test_image=test_image.reshape(28,28)\n", |
| 275 | + "plt.imshow(test_image,cmap='gray')\n", |
| 276 | + "print('Prediction',res)" |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "code", |
| 281 | + "execution_count": null, |
| 282 | + "metadata": {}, |
| 283 | + "outputs": [], |
| 284 | + "source": [ |
| 285 | + "cap = cv.VideoCapture(0)\n", |
| 286 | + "\n", |
| 287 | + "while True:\n", |
| 288 | + "\n", |
| 289 | + " ret, frame = cap.read()\n", |
| 290 | + " \n", |
| 291 | + " #define region of interest\n", |
| 292 | + " roi = frame[100:400, 320:620]\n", |
| 293 | + " cv.imshow('roi', roi)\n", |
| 294 | + " roi = cv.cvtColor(roi, cv.COLOR_BGR2GRAY)\n", |
| 295 | + " roi = cv.resize(roi, (28, 28), interpolation = cv.INTER_AREA)\n", |
| 296 | + " \n", |
| 297 | + " cv.imshow('roi sacled and gray', roi)\n", |
| 298 | + " copy = frame.copy()\n", |
| 299 | + " cv.rectangle(copy, (320, 100), (620, 400), (255,0,0), 5)\n", |
| 300 | + " \n", |
| 301 | + " roi = roi.reshape(1,28,28,1) \n", |
| 302 | + "\n", |
| 303 | + " result = classifier.predict(roi)\n", |
| 304 | + " result =np.argmax(result)\n", |
| 305 | + " #print(result)\n", |
| 306 | + " result=str(result)[0]\n", |
| 307 | + " cv.putText(copy, result, (300 , 100), cv.FONT_HERSHEY_COMPLEX, 2, (0, 255, 0), 2)\n", |
| 308 | + " cv.imshow('frame', copy) \n", |
| 309 | + " \n", |
| 310 | + " if cv.waitKey(1) == 13: #13 is the Enter Key\n", |
| 311 | + " break\n", |
| 312 | + " \n", |
| 313 | + "cap.release()\n", |
| 314 | + "cv.destroyAllWindows() " |
| 315 | + ] |
| 316 | + }, |
| 317 | + { |
| 318 | + "cell_type": "code", |
| 319 | + "execution_count": null, |
| 320 | + "metadata": {}, |
| 321 | + "outputs": [], |
| 322 | + "source": [] |
| 323 | + } |
| 324 | + ], |
| 325 | + "metadata": { |
| 326 | + "kernelspec": { |
| 327 | + "display_name": "Python 3", |
| 328 | + "language": "python", |
| 329 | + "name": "python3" |
| 330 | + }, |
| 331 | + "language_info": { |
| 332 | + "codemirror_mode": { |
| 333 | + "name": "ipython", |
| 334 | + "version": 3 |
| 335 | + }, |
| 336 | + "file_extension": ".py", |
| 337 | + "mimetype": "text/x-python", |
| 338 | + "name": "python", |
| 339 | + "nbconvert_exporter": "python", |
| 340 | + "pygments_lexer": "ipython3", |
| 341 | + "version": "3.6.12" |
| 342 | + } |
| 343 | + }, |
| 344 | + "nbformat": 4, |
| 345 | + "nbformat_minor": 4 |
| 346 | +} |
0 commit comments