{"cells":[{"metadata":{"id":"ANyLV82Jp3lU","colab_type":"text","_uuid":"a3255eebed710ad700f17d209d3019dec0522f3a"},"cell_type":"markdown","source":"#MNIST Digit Classification\n\nIn this document, I am going to be building a deep neural network model to classify digits using Keras Library and Tensor Flow.  \n\n#Importing the Data \n\nFirst let us import the data and examine trends in it. The data consists of images of hand written digits from 0 - 9, thus giving us 10 classes to classify.  Moreover,  every image is 28x28 pixels. \n\nNote that our data will be imported from the keras library datasets.\n"},{"metadata":{"id":"jJ_j_sk-p27L","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"base_uri":"https://localhost:8080/","height":160},"outputId":"0ef8fbd7-a917-4e89-b704-0a5cdab4fcc6","executionInfo":{"status":"ok","timestamp":1532002240895,"user_tz":-120,"elapsed":1618,"user":{"displayName":"Abdullah Emad","photoUrl":"//lh4.googleusercontent.com/-XQERG1X202Q/AAAAAAAAAAI/AAAAAAAAADE/jVWPv5hE4Qk/s50-c-k-no/photo.jpg","userId":"116762722132800298552"}},"trusted":true,"_uuid":"8e86a344cae653318a6cddc0bdecbf14fec37e38","collapsed":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom keras.datasets import mnist\nimport numpy as np\nfrom keras.utils import to_categorical, np_utils\nfrom keras.layers import Dense, Activation, Dropout, Conv2D, MaxPooling2D, Flatten\nfrom keras.models import Sequential\nfrom keras import callbacks\nfrom keras import backend as K\n\nK.set_image_data_format('channels_last')\nnp.random.seed(0)\n\ndef load_data(path='../input/mnist/mnist.npz'):\n    \"\"\"Loads the MNIST dataset.\n    \n    # Returns\n        Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.\n    \"\"\"\n#     path = get_file(path, origin='https://s3.amazonaws.com/img-datasets/mnist.npz')\n    f = np.load('../input/mnist/mnist.npz')\n    x_train = f['x_train']\n    y_train = f['y_train']\n    x_test = f['x_test']\n    y_test = f['y_test']\n    f.close()\n    return (x_train, y_train), (x_test, y_test)\n\n(x_train, y_train), (x_test, y_test) = load_data()\n\nprint(\"Loaded \" + str(len(x_train)) + \" Training examples\")\nprint(\"Loaded \" + str(len(x_train)) + \" Testing examples\")\n\n\n\n\nprint('\\nVisualize the Labels')\ni=0\nfor i in range(10):\n  plt.subplot(1, 10, i+1)\n  plt.axis('off')\n  plt.imshow(x_train[i])","execution_count":null,"outputs":[]},{"metadata":{"id":"strp59nHvGLS","colab_type":"text","_uuid":"a245d99d4b49ab5f86556e880741c7f97458222a"},"cell_type":"markdown","source":"# Creating our Neural Network Model\n\nLet us create a neural network with 1 input layer,  2 hidden layers and an output layer of 10 units corresponding to the 10 digits"},{"metadata":{"id":"3oWzrVbovrgG","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"base_uri":"https://localhost:8080/","height":34},"outputId":"1ea37ceb-c7a2-4b87-b273-a2f482eb2091","executionInfo":{"status":"ok","timestamp":1532002245287,"user_tz":-120,"elapsed":1192,"user":{"displayName":"Abdullah Emad","photoUrl":"//lh4.googleusercontent.com/-XQERG1X202Q/AAAAAAAAAAI/AAAAAAAAADE/jVWPv5hE4Qk/s50-c-k-no/photo.jpg","userId":"116762722132800298552"}},"trusted":true,"_uuid":"ad203a176a24f3ce3a4ecb1857bd347325439de9","collapsed":true},"cell_type":"code","source":"NN = Sequential()\n\n#input layer with 1024 units\nNN.add(Conv2D(128, (3,3) , padding='same', activation=\"relu\", input_shape=(28,28,1)))\n\n# 2 hidden layer\nNN.add(Conv2D(128, (3,3), padding='same', activation=\"relu\"))\nNN.add(MaxPooling2D(pool_size=(2, 2)))\nNN.add(Conv2D(64, (3,3), padding='same', activation='relu'))\nNN.add(MaxPooling2D(pool_size=(2, 2)))\nNN.add(Conv2D(64, (3,3), padding='same', activation='relu'))\nNN.add(MaxPooling2D(pool_size=(2, 2)))\n#output layer\nNN.add(Flatten())\nNN.add(Dense(units=10, activation='softmax'))\n\n\nNN.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])\n\nprint(\"Model has been created!\")\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"id":"GvHjapmVyaZB","colab_type":"text","_uuid":"376b0ad860e58582d4f12bd63d0b9cda623528f8"},"cell_type":"markdown","source":"# Training our Model with the data\n\n\nFirst since our data is a 28x28 image we will need to reshape it into a 1D array of pixels where each example has 28*28 = 784 features and we have 60000 example."},{"metadata":{"id":"58mhCoWDyv6e","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"base_uri":"https://localhost:8080/","height":1138},"outputId":"d39da889-08eb-4d28-fc99-900dcaf17e70","executionInfo":{"status":"ok","timestamp":1532002311979,"user_tz":-120,"elapsed":64148,"user":{"displayName":"Abdullah Emad","photoUrl":"//lh4.googleusercontent.com/-XQERG1X202Q/AAAAAAAAAAI/AAAAAAAAADE/jVWPv5hE4Qk/s50-c-k-no/photo.jpg","userId":"116762722132800298552"}},"trusted":true,"_uuid":"6bd95828d6c6747715f1c2149c7c8164a641bc81","collapsed":true},"cell_type":"code","source":"# reshapes the data into (784X60000) Matrix\nx_train = x_train / 255\nx_test = x_test / 255\nx_train = x_train.reshape(x_train.shape[0],28,28,1)\nx_test = x_test.reshape(x_test.shape[0],28,28,1)\ny_train = np_utils.to_categorical(y_train, num_classes=10)\ny_test = np_utils.to_categorical(y_test, num_classes=10)\n\nprint(x_train.shape)\nprint(y_train.shape)\n\nNN.fit(x_train, y_train, epochs=30, batch_size=200, validation_split=0.2, verbose=1)\n\nprint(\"Trained Our Model with the training set examples\")","execution_count":null,"outputs":[]},{"metadata":{"id":"bv5AzGz95sSK","colab_type":"text","_uuid":"64869263812918aefae35e2d5a13e5ded7d40467"},"cell_type":"markdown","source":"# Performance\n\nLet us now measure how our Model performing"},{"metadata":{"id":"mp2DNvjp53Tc","colab_type":"code","colab":{"autoexec":{"startup":false,"wait_interval":0},"base_uri":"https://localhost:8080/","height":52},"outputId":"ce7e9073-314c-48e2-8485-1a42c63517b6","executionInfo":{"status":"ok","timestamp":1532002229384,"user_tz":-120,"elapsed":1602,"user":{"displayName":"Abdullah Emad","photoUrl":"//lh4.googleusercontent.com/-XQERG1X202Q/AAAAAAAAAAI/AAAAAAAAADE/jVWPv5hE4Qk/s50-c-k-no/photo.jpg","userId":"116762722132800298552"}},"trusted":true,"_uuid":"9de74c9da46b13fe4660a490abd38a7ad29ff504","collapsed":true},"cell_type":"code","source":"print (\"Model Accuracy =  %f\"% (NN.evaluate(x_train, y_train, batch_size=200)[1]*100), \"%\" )","execution_count":null,"outputs":[]}],"metadata":{"colab":{"name":"digits_classification.ipynb","version":"0.3.2","views":{},"default_view":{},"provenance":[],"collapsed_sections":[]},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"accelerator":"GPU","language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}