{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction to CNN using Tensorflow 2.0 - MNIST (98%)\nAuthor: Anand\n\n\nWell this is my first kaggle notebook.So which better dataset to start my keggle journey.\nWish me luck kagglers :)","metadata":{}},{"cell_type":"code","source":"#Lets import the needed Libraries\n#Kaggle comes with the latest tensorflow 2.0 version so i don't have to install it :p\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nimport itertools\ntf.__version__","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now lets load the dataset\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now we have loaded the data now lets see its content\ntrain.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now we can see that the first column labels is the target and rest are the features\nX_train = train.iloc[:,1:]\ny_train = train.iloc[:,0]\n#Same for the test set\nX_test = test.iloc[:,:]\ntrain.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now lets just visualize the MNIST class count tp check imbalance\ng = sns.countplot(y_train)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We can see that all the classes are around similar range.That's what we needed \n#Now lets the normalize the data to make the pixels within (0 - 1) range\nX_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#In the above section[24] we could see that dataset shape (42000, 785),\n#but since we are using Convolution our model expects an input in the shape (height x width x depth)\n#Since we are using a black and white image the depth/colour channel should be 1.\n#So lets reshape the inputs\nX_train = X_train.values.reshape(-1,28,28,1)\nX_test = X_test.values.reshape(-1,28,28,1)\nX_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now lets encode the labels since that what our loss function expects\ny_train = tf.keras.utils.to_categorical(y_train)\ny_train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now lets split our dataset for train and validation.90% for training and 10% for validation\nrandom_seed = 42\nX_train,X_val,y_train,y_val = train_test_split(X_train,y_train,test_size = 0.1)\ninput_shape = (28,28,1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Now let us take a look at sample MNIST data\nplt.imshow(X_train[0].reshape(28,28),cmap=plt.set_cmap('gray'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## CNN\n#### Now is the interesting part of building a model.I am just building a very simple Model with two convolution layers.Instead of going with keras API here i am going with latest tensorflow 2.0 framework (which is almost similar to keras :p ). So lets build the model","metadata":{}},{"cell_type":"code","source":"#Building the model\nmodel = tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Conv2D(32,kernel_size = (3,3),activation='relu',input_shape=input_shape))\nmodel.add(tf.keras.layers.Conv2D(64,kernel_size = (3,3),activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.2))\nmodel.add(tf.keras.layers.MaxPool2D(pool_size=(2,2)))\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(128,activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(10,activation='softmax'))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Lets checkout the model summary\nmodel.summary()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Looks okay :) \n#Now lets compile the model\nmodel.compile(optimizer='adam',metrics=['acc'],loss='categorical_crossentropy')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nbatch_size = 32\nhistory = model.fit(X_train,y_train,\n                   epochs=epochs,\n                   validation_data=(X_val,y_val),\n                   batch_size=batch_size,\n                   verbose=1).history","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### That's a 98.8%+ (99%) accuracy on the validation data.Well i guess that's great for my first try without doing much. :)","metadata":{}},{"cell_type":"code","source":"#Now we can plot how our accuracy and loss went\nloss = history['loss']\nval_loss = history['val_loss']\nepochs = range(1, len(loss)+ 1)\nprint(loss)\nprint(epochs)\nline1 = plt.plot(epochs,loss,label=\"Validation/Test loss\")\nline2 = plt.plot(epochs,val_loss,label=\"Training loss\")\nplt.setp(line1,linewidth=2.0,marker='+',markersize=\"10.0\")\nplt.setp(line2,linewidth=2.0,marker='4',markersize=\"10.0\")\nplt.title(\"Model Loss\")\nplt.ylabel(\"Epocs\")\nplt.xlabel(\"Loss\")\nplt.grid(True)\nplt.legend()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nacc = history['acc']\nval_acc = history['val_acc']\nline1 = plt.plot(epochs,acc,label=\"Validation/Test Accuracy\")\nline2 = plt.plot(epochs,val_acc,label=\"Training Accuracy\")\nplt.setp(line1,linewidth=2.0,marker='+',markersize=\"10.0\")\nplt.setp(line2,linewidth=2.0,marker='4',markersize=\"10.0\")\nplt.title(\"Model Accuracy\")\nplt.ylabel(\"Epocs\")\nplt.xlabel(\"Accuracy\")\nplt.grid(True)\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check model score\nscore = model.evaluate(X_val,y_val)\nprint(f\"loss :{score[0]}\")\nprint(f\"Accuracy :{score[1]}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Well 98% + looks great :] ","metadata":{}},{"cell_type":"markdown","source":"### Lets check the prediction accuracy on test data\n\nWell this might seem a bit unconventional for kaggle where kagglers mostly stops at the point where they get a great accuracy score on the validation & test data.Well here i am trying to pick a random number from the MNIST test set and predict its result.","metadata":{}},{"cell_type":"code","source":"#lets plot a random image from the test set\nrand = np.random.randint(0,len(X_test))\nplt.imshow(X_test[rand].reshape(28,28))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 7 thats my favourite number :)\n#Now lets see how the model predict\nresult = model.predict_classes(X_test[rand].reshape(-1,28,28,1))[0]\nprint(f\"Predicted Result : {result}\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Wow :)\n\n#### Well that's it then, i hope i have done good for my first kaggle notebook.If i have done any mistake please comment.\n#### if you find this notebook useful please like :)\n","metadata":{}}]}