{"cells":[{"metadata":{},"cell_type":"markdown","source":"MNIST Digit Recognition"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"from __future__ import absolute_import, division, print_function, unicode_literals\nimport numpy as np \nimport pandas as pd \nimport tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Convolution2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dense\nfrom keras.callbacks import callbacks\nfrom keras.utils import to_categorical\nfrom tensorflow.keras.callbacks import ModelCheckpoint\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Load the data\ndf_train = pd.read_csv('../input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('../input/digit-recognizer/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Visualizing the shape of train data\ndf_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Visualizing the shape of test data\ndf_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"height = 28\nwidth = 28\nclasses =10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalization of data\nx_train = df_train.drop(['label'], axis = 1).to_numpy().reshape(42000, 28, 28, 1).astype('float32')\nx_train /= 255.\ny_train = tf.keras.utils.to_categorical(df_train['label'], classes)\n\nx_test = df_test.to_numpy().reshape(28000, 28, 28, 1).astype('float32')\nx_test /= 255.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\nprint(x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Visualization of image\ng = plt.imshow(x_train[6,:,:,0])\nprint(\"Output = \" +str(y_train[6,:]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"g = plt.imshow(x_test[7,:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Create the Convolution Model \nmodel =  models.Sequential()\nmodel.add(layers.Conv2D(filters = 32, kernel_size =(3, 3), activation = 'relu', input_shape = (28, 28, 1), padding = 'same'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.3))\nmodel.add(layers.Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu', input_shape = (28, 28, 1), padding = 'same'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.3))                            \nmodel.add(layers.Conv2D(filters = 64, kernel_size =(3, 3), activation = 'relu', input_shape = (28, 28, 1), padding = 'same'))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation = 'relu'))\nmodel.add(layers.Dropout(0.3))\nmodel.add(layers.Dense(10, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compile the Model\nmodel.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class EarlyStoppingAtMinLoss(tf.keras.callbacks.Callback):\n  \"\"\"Stop training when the loss is at its min, i.e. the loss stops decreasing.\n\n  Arguments:\n      patience: Number of epochs to wait after min has been hit. After this\n      number of no improvement, training stops.\n  \"\"\"\n\n  def __init__(self, patience=0):\n    super(EarlyStoppingAtMinLoss, self).__init__()\n\n    self.patience = patience\n\n    # best_weights to store the weights at which the minimum loss occurs.\n    self.best_weights = None\n\n  def on_train_begin(self, logs=None):\n    # The number of epoch it has waited when loss is no longer minimum.\n    self.wait = 0\n    # The epoch the training stops at.\n    self.stopped_epoch = 0\n    # Initialize the best as infinity.\n    self.best = np.Inf\n\n  def on_epoch_end(self, epoch, logs=None):\n    current = logs.get('loss')\n    if np.less(current, self.best):\n      self.best = current\n      self.wait = 0\n      # Record the best weights if current results is better (less).\n      self.best_weights = self.model.get_weights()\n    else:\n      self.wait += 1\n      if self.wait >= self.patience:\n        self.stopped_epoch = epoch\n        self.model.stop_training = True\n        print('Restoring model weights from the end of the best epoch.')\n        self.model.set_weights(self.best_weights)\n\n  def on_train_end(self, logs=None):\n    if self.stopped_epoch > 0:\n      print('Epoch %05d: early stopping' % (self.stopped_epoch + 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train the Model\nhistory = model.fit(x_train, y_train, epochs=15, validation_split = 0.2, callbacks = [EarlyStoppingAtMinLoss()])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Visualizing the Accuracy of Model\nplt.plot(history.history['accuracy'], label='acc')\nplt.plot(history.history['val_accuracy'], label = 'val_acc')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.ylim([0.5, 1])\nplt.legend(loc='lower right')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper right')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Prediction on Test data\ny_predict = model.predict_classes(x_test, verbose=0)\nm = plt.imshow(x_test[9,:,:,0])\nprint(\"Output = \" +str(y_predict[9]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submissions=pd.DataFrame({\"ImageId\": list(range(1,len(y_predict)+1)),\n                         \"Label\": y_predict})\nsubmissions.to_csv(\"sub.csv\", index=False, header=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.6"}},"nbformat":4,"nbformat_minor":1}