{"cells":[{"metadata":{"id":"5xeMXu0yp1TW","colab_type":"text"},"cell_type":"markdown","source":"# MNIST dataset classification using CNN\n\n## Importing libraries\nImporting the various libraries required for the classification.\n\n**__ future __** : The future package will provide support for running your code on Python 2.6, 2.7, and 3.3+ mostly unchanged.\n\nRest of the imports are pretty much straight forward.\n\n%%capture is a built-in magic command that hides the output of the packages installing"},{"metadata":{"id":"qQMToRD-koBO","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"%%capture\nfrom __future__ import absolute_import, division, print_function, unicode_literals\n\n!pip install tensorflow-gpu==2.0.0-beta1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers, datasets, models\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"id":"y7olqvMMr4bV","colab_type":"text"},"cell_type":"markdown","source":"## Download and prepare the MNIST dataset"},{"metadata":{"id":"_TNfcZVKoGOM","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"e9b11eb1-9d6a-4621-af79-ee8158348e5d","trusted":true},"cell_type":"code","source":"# Loading the data and splting it into train and test images\n# (train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()\ntrain_images = pd.read_csv(\"../input/train.csv\")\ntest_images = test = pd.read_csv(\"../input/test.csv\")\n\ntrain_labels = train_images['label']\ntrain_images = train_images.drop(labels = [\"label\"],axis = 1)\n\n# Reshape it into the following shapes\n# The images are of (28,28,1) shapes : (width, height, channels)\n# The channel size is 1 since the images are grayscale. If it were RGB it would have channel size of 3\nprint('Before: ', train_images.shape, test_images.shape)\n\ntrain_images = train_images.values.reshape((-1,28,28,1))\ntest_images = test_images.values.reshape((-1,28,28,1))\n\nprint('After: ', train_images.shape, test_images.shape)\n\n# Normalize the images so that it's pixel values remain between 0 and 1\n# This helps some models perform better\ntrain_images, test_images = train_images/255, test_images/255\n\n# creating validation data\ntrain_images, train_images_val, train_labels, train_labels_val = train_test_split(train_images, train_labels, test_size=0.1)\nprint(train_images.shape, train_images_val.shape, train_labels.shape, train_labels_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"id":"ZkP0JqoevRVF","colab_type":"text"},"cell_type":"markdown","source":"## Defaining the model\nThis model consists of :\n\n*  **Convolutional** and **MaxPooling layers**.\nThe first layer takes a special argument input_shape to know to shape in which the input will be provided.\nThe subsecutent layers do not need this argument as those recieve its input from the previous layers so keras handles it. \n* After twolayers of Con2D and MaxPolling2D we use a **Flatten** layer to flatten the last Conv layer into a long vector\n* A **Dense** layer is used that is ***fully connected*** to each of the inputs from the Flatten layer\n* Finally a **Dense** layer of **10 units** (for the 0-9 digits in the MNIST dataset) is used with a **softmax activation**\n\n\n\n\n\n"},{"metadata":{"id":"tCaooC2-vad9","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"model = models.Sequential([\n    layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(pool_size=(2,2)),\n    layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"id":"hg_QCoOtxkf3","colab_type":"text"},"cell_type":"markdown","source":"Display the model summary/architecture.\nThis will also show you the number of parameters in use"},{"metadata":{"id":"ixE9nyK9xvGX","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":420},"outputId":"baa030cb-d760-4939-a6cf-33fe848a9ac1","trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"id":"XNqBnhm-1nHK","colab_type":"text"},"cell_type":"markdown","source":"## Compile the model\n\n\n*   **optimizer** : The function used for the minimization of the loss\n*   **loss** : The end goal to minimize\n*   **metrics** : List of metrics to be evaluated by the model during training and testing\n\n"},{"metadata":{"id":"S_iXy_Ex1tBI","colab_type":"code","colab":{},"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"id":"SQpYFYcd3M54","colab_type":"text"},"cell_type":"markdown","source":"## Train the model\nHere we input the training date and it's labels to train the model. Epochs is the number of training interations the model will do with the entire dataset."},{"metadata":{"id":"nQ1C6sUD3Q9h","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":218},"outputId":"205d4921-c2d8-4880-bb25-1ddf7fc03e14","trusted":true},"cell_type":"code","source":"model.fit(train_images, train_labels, epochs=7,validation_data=(train_images_val, train_labels_val))","execution_count":null,"outputs":[]},{"metadata":{"id":"2rH5RRCb37q8","colab_type":"text"},"cell_type":"markdown","source":"## Now we evaluate the model on unseen data"},{"metadata":{"id":"VHIb_Dby4BRp","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":50},"outputId":"5b6f5a89-8a06-4c67-a367-31e3d425fe80","trusted":true},"cell_type":"code","source":"# test_loss, test_accuracy = model.evaluate(test_images, test_labels)\n# print('Test accuracy : ', round(test_accuracy*100, 2), '%')\n\n# predict the results\nresults = model.predict(test_images)\nresults = np.argmax(results, axis = 1)\nresults = pd.Series(results, name='Label')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# prepare results for submissions\nsubmission = pd.concat([pd.Series(range(1,28001), name='ImageId'), results], axis = 1)\n\nsubmission.to_csv(\"cnn_mnist_datagen.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"id":"HkmV8yWk5HDV","colab_type":"text"},"cell_type":"markdown","source":"***Congratulations we have an accuracy of 99% above on unseen data of the MNIST dataset with our model***\n\n(Results may vary with different runs but should generally be above 98%)"}],"metadata":{"colab":{"name":"MNIST_using_CNN.ipynb","version":"0.3.2","provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":1}