{"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":"code","source":"# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-25T14:43:41.101402Z","iopub.execute_input":"2021-06-25T14:43:41.101869Z","iopub.status.idle":"2021-06-25T14:43:41.109155Z","shell.execute_reply.started":"2021-06-25T14:43:41.101825Z","shell.execute_reply":"2021-06-25T14:43:41.108206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:41.111128Z","iopub.execute_input":"2021-06-25T14:43:41.111732Z","iopub.status.idle":"2021-06-25T14:43:46.639483Z","shell.execute_reply.started":"2021-06-25T14:43:41.111696Z","shell.execute_reply":"2021-06-25T14:43:46.638545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\nprint(train.head())\nprint(20*'---')\nprint(test.head())\n","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:46.642993Z","iopub.execute_input":"2021-06-25T14:43:46.643248Z","iopub.status.idle":"2021-06-25T14:43:51.276154Z","shell.execute_reply.started":"2021-06-25T14:43:46.643222Z","shell.execute_reply":"2021-06-25T14:43:51.275173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set up training and testing data\ntraining_images = (train.iloc[:,1:].values).astype('float32') # all pixel values\ntraining_labels = train.iloc[:,0].values.astype('int32') # only labels\n\ntesting_images = test.values.astype('float32')\n\ntraining_images = training_images.reshape(training_images.shape[0], 28, 28,1)\nX_test = testing_images.reshape(testing_images.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:51.279581Z","iopub.execute_input":"2021-06-25T14:43:51.279887Z","iopub.status.idle":"2021-06-25T14:43:51.385806Z","shell.execute_reply.started":"2021-06-25T14:43:51.279850Z","shell.execute_reply":"2021-06-25T14:43:51.384898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Total Training images\", training_images.shape)\nprint(\"Total Training labels\",training_labels.shape)\nprint(\"Total Testing images\",X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:51.387065Z","iopub.execute_input":"2021-06-25T14:43:51.387576Z","iopub.status.idle":"2021-06-25T14:43:51.393970Z","shell.execute_reply.started":"2021-06-25T14:43:51.387536Z","shell.execute_reply":"2021-06-25T14:43:51.393016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display some pictures of the dataset\nfig, axes = plt.subplots(nrows=4, ncols=6, figsize=(8, 8),\n                        subplot_kw={'xticks': [], 'yticks': []})\n\nfor i, ax in enumerate(axes.flat):\n    img = training_images[i].reshape(28,28)\n    ax.imshow(img, cmap = 'gray')\n    title = training_labels[i]\n    ax.set_title(title, fontsize = 15)\nplt.tight_layout(pad=0.5)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:51.395278Z","iopub.execute_input":"2021-06-25T14:43:51.395665Z","iopub.status.idle":"2021-06-25T14:43:52.725454Z","shell.execute_reply.started":"2021-06-25T14:43:51.395628Z","shell.execute_reply":"2021-06-25T14:43:52.724148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the distribution of each letter\n\nvc = pd.Series(training_labels).value_counts()\nplt.figure(figsize=(20,5))\nsns.barplot(x = sorted(vc.index), y = vc, palette = \"rocket\")\nplt.title(\"Number of pictures of each category\", fontsize = 15)\nplt.xticks(fontsize = 15)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:52.730213Z","iopub.execute_input":"2021-06-25T14:43:52.730826Z","iopub.status.idle":"2021-06-25T14:43:52.967672Z","shell.execute_reply.started":"2021-06-25T14:43:52.730737Z","shell.execute_reply":"2021-06-25T14:43:52.966852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepares data for training\nX = training_images\ny = training_labels\n\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.20, random_state=1)\n\nprint(X_train.shape)\nprint(X_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:52.968960Z","iopub.execute_input":"2021-06-25T14:43:52.969296Z","iopub.status.idle":"2021-06-25T14:43:53.281690Z","shell.execute_reply.started":"2021-06-25T14:43:52.969250Z","shell.execute_reply":"2021-06-25T14:43:53.280694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create an ImageDataGenerator and do Image Augmentation\n\ntrain_datagen = ImageDataGenerator(rescale = 1.0/255.0,\n                                   height_shift_range=0.1,\n                                   width_shift_range=0.1,\n                                   zoom_range=0.1,\n                                   shear_range=0.1,\n                                   rotation_range=10,\n                                   fill_mode='nearest',\n                                   horizontal_flip=False)\n\n#Image Augmentation is not done on the testing data\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0/255)\n\ntrain_datagenerator = train_datagen.flow(X_train, y_train, batch_size = 128)\n\nvalidation_datagenerator = validation_datagen.flow(X_val,y_val, batch_size=128)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:53.284879Z","iopub.execute_input":"2021-06-25T14:43:53.285236Z","iopub.status.idle":"2021-06-25T14:43:53.292897Z","shell.execute_reply.started":"2021-06-25T14:43:53.285197Z","shell.execute_reply":"2021-06-25T14:43:53.291910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a Callback class that stops training once accuracy reaches 99.0%\n\nclass myCallback(tf.keras.callbacks.Callback):\n  def on_epoch_end(self, epoch, logs={}):\n    if(logs.get('accuracy')>0.99):\n      print(\"\\nReached 99.0% accuracy so cancelling training!\")\n      self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:53.294560Z","iopub.execute_input":"2021-06-25T14:43:53.294883Z","iopub.status.idle":"2021-06-25T14:43:53.301665Z","shell.execute_reply.started":"2021-06-25T14:43:53.294851Z","shell.execute_reply":"2021-06-25T14:43:53.300774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([tf.keras.layers.InputLayer(input_shape=[28,28,1]),\n                                    tf.keras.layers.experimental.preprocessing.RandomContrast(0.2),\n                                    tf.keras.layers.Conv2D(32, (3,3), activation='relu'),\n                                    tf.keras.layers.MaxPool2D(2,2),\n                                    tf.keras.layers.Conv2D(256, (3,3), activation='relu'),\n                                    tf.keras.layers.MaxPool2D(2,2),\n                                    tf.keras.layers.Conv2D(512, (3,3), activation='relu'),\n                                    tf.keras.layers.MaxPool2D(2,2),\n                                    tf.keras.layers.Flatten(),\n                                    tf.keras.layers.Dense(512, activation = 'relu'),\n                                    tf.keras.layers.Dropout(0.2),\n                                    tf.keras.layers.Dense(512, activation = 'relu'),\n                                    tf.keras.layers.Dropout(0.2),\n                                    tf.keras.layers.Dense(10, activation = 'softmax')])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:53.303336Z","iopub.execute_input":"2021-06-25T14:43:53.304020Z","iopub.status.idle":"2021-06-25T14:43:55.314281Z","shell.execute_reply.started":"2021-06-25T14:43:53.303980Z","shell.execute_reply":"2021-06-25T14:43:55.313452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:55.315597Z","iopub.execute_input":"2021-06-25T14:43:55.315947Z","iopub.status.idle":"2021-06-25T14:43:55.329234Z","shell.execute_reply.started":"2021-06-25T14:43:55.315913Z","shell.execute_reply":"2021-06-25T14:43:55.326324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling the Model. \nmodel.compile(loss = 'sparse_categorical_crossentropy',\n             optimizer = tf.keras.optimizers.Adam(),\n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:55.330850Z","iopub.execute_input":"2021-06-25T14:43:55.331378Z","iopub.status.idle":"2021-06-25T14:43:55.346209Z","shell.execute_reply.started":"2021-06-25T14:43:55.331342Z","shell.execute_reply":"2021-06-25T14:43:55.345299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', \n                                            patience = 2, \n                                            verbose=1,factor=0.25, \n                                            min_lr=0.0001)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:55.349432Z","iopub.execute_input":"2021-06-25T14:43:55.349673Z","iopub.status.idle":"2021-06-25T14:43:55.355000Z","shell.execute_reply.started":"2021-06-25T14:43:55.349651Z","shell.execute_reply":"2021-06-25T14:43:55.354051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the Model\ncallbacks = myCallback()\nhistory = model.fit(train_datagenerator,\n                    validation_data = validation_datagenerator,\n                    steps_per_epoch = len(X_train)//128,\n                    epochs = 50,\n                    validation_steps = len(X_val)//128,\n                    callbacks = [callbacks, learning_rate_reduction])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T14:43:55.356248Z","iopub.execute_input":"2021-06-25T14:43:55.356696Z","iopub.status.idle":"2021-06-25T15:09:45.467166Z","shell.execute_reply.started":"2021-06-25T14:43:55.356660Z","shell.execute_reply":"2021-06-25T15:09:45.466282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the chart for accuracy and loss on both training and validation\n\nimport matplotlib.pyplot as plt\nfig.set_size_inches(16,9)\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:45.468882Z","iopub.execute_input":"2021-06-25T15:09:45.469276Z","iopub.status.idle":"2021-06-25T15:09:45.761470Z","shell.execute_reply.started":"2021-06-25T15:09:45.469233Z","shell.execute_reply":"2021-06-25T15:09:45.760736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix,accuracy_score, classification_report\n# Predict the label of the test_images\npred = model.predict(X_val)\npred = np.argmax(pred,axis=1)\n\n# Get the accuracy score\nacc = accuracy_score(y_val,pred)\n\n# Display the results\nprint(f'## {acc*100:.2f}% accuracy on the test set')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:45.762644Z","iopub.execute_input":"2021-06-25T15:09:45.763150Z","iopub.status.idle":"2021-06-25T15:09:47.450035Z","shell.execute_reply.started":"2021-06-25T15:09:45.763109Z","shell.execute_reply":"2021-06-25T15:09:47.448331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(y_val, pred)) ","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:47.451449Z","iopub.execute_input":"2021-06-25T15:09:47.451799Z","iopub.status.idle":"2021-06-25T15:09:47.476271Z","shell.execute_reply.started":"2021-06-25T15:09:47.451762Z","shell.execute_reply":"2021-06-25T15:09:47.475376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display a confusion matrix\ncf_matrix = confusion_matrix(y_val, pred, normalize='true')\nplt.figure(figsize = (20,15))\nsns.heatmap(cf_matrix, annot=True, xticklabels = sorted(set(y_val)), yticklabels = sorted(set(y_val)),cbar=False)\nplt.title('Normalized Confusion Matrix\\n', fontsize = 23)\nplt.xlabel(\"Predicted Classes\",fontsize=15)\nplt.ylabel(\"True Classes\",fontsize=15)\nplt.xticks(fontsize=15)\nplt.yticks(fontsize=15,rotation=0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:47.477422Z","iopub.execute_input":"2021-06-25T15:09:47.477772Z","iopub.status.idle":"2021-06-25T15:09:48.027392Z","shell.execute_reply.started":"2021-06-25T15:09:47.477736Z","shell.execute_reply":"2021-06-25T15:09:48.026606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"correct = np.nonzero(pred == y_val)[0]\nplt.figure(figsize=(8, 8))\ni = 0\nfor c in correct[:9]:\n    plt.subplot(3,3,i+1)\n    plt.imshow(X_val[c].reshape(28,28), cmap=\"gray\", interpolation='none')\n    plt.title(\"Predicted:{}, Actual:{}\".format(pred[c], y_val[c]))\n    plt.tight_layout()\n    i += 1","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:48.028669Z","iopub.execute_input":"2021-06-25T15:09:48.029236Z","iopub.status.idle":"2021-06-25T15:09:49.489698Z","shell.execute_reply.started":"2021-06-25T15:09:48.029184Z","shell.execute_reply":"2021-06-25T15:09:49.488923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Final submission\n\npredictions = model.predict(X_test, verbose=0)\npred = predictions.argmax(axis=1)\nprint(pred)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:49.491089Z","iopub.execute_input":"2021-06-25T15:09:49.491498Z","iopub.status.idle":"2021-06-25T15:09:54.521522Z","shell.execute_reply.started":"2021-06-25T15:09:49.491462Z","shell.execute_reply":"2021-06-25T15:09:54.520524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({\"ImageId\": list(range(1,len(pred)+1)),\n                            \"Label\": pred})\n\nsubmission.to_csv(\"submission.csv\", index=False, header=True)\n\nprint(\"Finished submission file to csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-25T15:09:54.525353Z","iopub.execute_input":"2021-06-25T15:09:54.525730Z","iopub.status.idle":"2021-06-25T15:09:54.878951Z","shell.execute_reply.started":"2021-06-25T15:09:54.525663Z","shell.execute_reply":"2021-06-25T15:09:54.878047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}