{"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":"import pandas as pd\nimport numpy as np\nimport os\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import BatchNormalization\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils.np_utils import to_categorical\n\nfrom tensorflow.keras.layers import Dense, Input, Flatten, Conv2D, MaxPool2D\nfrom tensorflow.keras.models import Model\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-28T04:22:16.059719Z","iopub.execute_input":"2023-02-28T04:22:16.060006Z","iopub.status.idle":"2023-02-28T04:22:26.105926Z","shell.execute_reply.started":"2023-02-28T04:22:16.059977Z","shell.execute_reply":"2023-02-28T04:22:26.104695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir('../input/digit-recognizer'))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:30.201830Z","iopub.execute_input":"2023-02-28T04:22:30.205592Z","iopub.status.idle":"2023-02-28T04:22:30.217790Z","shell.execute_reply.started":"2023-02-28T04:22:30.205539Z","shell.execute_reply":"2023-02-28T04:22:30.216528Z"},"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')\nsub = pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:31.635012Z","iopub.execute_input":"2023-02-28T04:22:31.635970Z","iopub.status.idle":"2023-02-28T04:22:36.960965Z","shell.execute_reply.started":"2023-02-28T04:22:31.635928Z","shell.execute_reply":"2023-02-28T04:22:36.959898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Training data size is {train.shape}\\nTesting data size is {test.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:41.464914Z","iopub.execute_input":"2023-02-28T04:22:41.465645Z","iopub.status.idle":"2023-02-28T04:22:41.471465Z","shell.execute_reply.started":"2023-02-28T04:22:41.465606Z","shell.execute_reply":"2023-02-28T04:22:41.470159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(['label'], axis = 1).values\ny = train['label'].values","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:42.716035Z","iopub.execute_input":"2023-02-28T04:22:42.716406Z","iopub.status.idle":"2023-02-28T04:22:42.812336Z","shell.execute_reply.started":"2023-02-28T04:22:42.716374Z","shell.execute_reply":"2023-02-28T04:22:42.811260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:44.025410Z","iopub.execute_input":"2023-02-28T04:22:44.026099Z","iopub.status.idle":"2023-02-28T04:22:44.134137Z","shell.execute_reply.started":"2023-02-28T04:22:44.026059Z","shell.execute_reply":"2023-02-28T04:22:44.133035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:45.175118Z","iopub.execute_input":"2023-02-28T04:22:45.175838Z","iopub.status.idle":"2023-02-28T04:22:45.181501Z","shell.execute_reply.started":"2023-02-28T04:22:45.175801Z","shell.execute_reply":"2023-02-28T04:22:45.180360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y)\n\nprint(f\"Label size {y.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:46.892012Z","iopub.execute_input":"2023-02-28T04:22:46.892926Z","iopub.status.idle":"2023-02-28T04:22:46.902260Z","shell.execute_reply.started":"2023-02-28T04:22:46.892877Z","shell.execute_reply":"2023-02-28T04:22:46.901203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:49.148465Z","iopub.execute_input":"2023-02-28T04:22:49.149719Z","iopub.status.idle":"2023-02-28T04:22:49.533732Z","shell.execute_reply.started":"2023-02-28T04:22:49.149678Z","shell.execute_reply":"2023-02-28T04:22:49.532663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, X_test.shape, y_train.shape, y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:50.682637Z","iopub.execute_input":"2023-02-28T04:22:50.683014Z","iopub.status.idle":"2023-02-28T04:22:50.693037Z","shell.execute_reply.started":"2023-02-28T04:22:50.682981Z","shell.execute_reply":"2023-02-28T04:22:50.690011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train__ = X_train.reshape(X_train.shape[0], 28, 28)\n\nfig, axis = plt.subplots(1, 4, figsize=(20, 10))\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(X_train__[i], cmap='binary')\n    digit = y_train[i].argmax()\n    ax.set(title = f\"Real Number is {digit}\");","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:52.220346Z","iopub.execute_input":"2023-02-28T04:22:52.221041Z","iopub.status.idle":"2023-02-28T04:22:52.858260Z","shell.execute_reply.started":"2023-02-28T04:22:52.221004Z","shell.execute_reply":"2023-02-28T04:22:52.857070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class LeNet5(tf.keras.Model):\n    \n  def __init__(self,  num_classes = 10):\n    super(LeNet5, self).__init__()\n    self.conv1 = Conv2D(filters = 6, kernel_size= (5,5), padding=\"same\", activation=\"relu\")\n    self.max_pool2x2 = MaxPool2D(pool_size = (2,2))\n    self.conv2 = Conv2D(filters=16 , kernel_size = (5,5), padding = \"same\", activation = \"relu\")\n    self.conv3 = Conv2D(filters = 120 , kernel_size = (5,5), padding = \"same\", activation = \"relu\")\n    self.flatten = Flatten()\n    self.fc2 = Dense(units = 84, activation = \"relu\")\n    self.fc3=Dense(units = num_classes, activation = \"softmax\")\n \n  def call(self, input_tensor):\n    \n    x = self.conv1(input_tensor)\n    x = self.max_pool2x2(x)\n    x = self.conv2(x)\n    x = self.max_pool2x2(x)\n    x = self.conv3(x)\n    x = self.flatten(x)\n    x = self.fc2(x)\n    x = self.fc3(x)\n    \n    return x  \n\ninput_layer = Input(shape  =(28, 28, 1))\nx = LeNet5()(input_layer)\nmodel = Model(inputs=input_layer, outputs = x)\n\nprint(model.summary(expand_nested = True))","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:22:59.078722Z","iopub.execute_input":"2023-02-28T04:22:59.079124Z","iopub.status.idle":"2023-02-28T04:23:02.956318Z","shell.execute_reply.started":"2023-02-28T04:22:59.079092Z","shell.execute_reply":"2023-02-28T04:23:02.955344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = tf.keras.losses.CategoricalCrossentropy(),\n    metrics = tf.keras.metrics.CategoricalAccuracy(),\n    optimizer = tf.keras.optimizers.Adam()\n)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:25:17.618980Z","iopub.execute_input":"2023-02-28T04:25:17.619981Z","iopub.status.idle":"2023-02-28T04:25:17.636458Z","shell.execute_reply.started":"2023-02-28T04:25:17.619943Z","shell.execute_reply":"2023-02-28T04:25:17.635465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    featurewise_center = False,  # set input mean to 0 over the dataset\n    samplewise_center = False,  # set each sample mean to 0\n    featurewise_std_normalization = False,  # divide inputs by std of the dataset\n    samplewise_std_normalization = False,  # divide each input by its std\n    zca_whitening = False,  # apply ZCA whitening\n    rotation_range = 15,  # randomly rotate images in the range (degrees, 0 to 180)\n    zoom_range = 0.01, # Randomly zoom image \n    width_shift_range = 0.1,  # randomly shift images horizontally (fraction of total width)\n    height_shift_range = 0.1,  # randomly shift images vertically (fraction of total height)\n    horizontal_flip = False,  # randomly flip images\n    vertical_flip = False\n)  # randomly flip images\n\n\n#datagen.fit(X_train)\ntrain_gen = datagen.flow(X_train, y_train, batch_size = 128)\ntest_gen = datagen.flow(X_test, y_test, batch_size = 128)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:25:18.836087Z","iopub.execute_input":"2023-02-28T04:25:18.837325Z","iopub.status.idle":"2023-02-28T04:25:18.896015Z","shell.execute_reply.started":"2023-02-28T04:25:18.837273Z","shell.execute_reply":"2023-02-28T04:25:18.894813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5\nbatch_size = 128\ntrain_steps = X_train.shape[0] // batch_size\nvalid_steps = X_test.shape[0] // batch_size\n\nes = tf.keras.callbacks.EarlyStopping(\n        monitor = \"val_acc\", # metrics to monitor\n        patience = 2, # how many epochs before stop\n        verbose = 1,\n        mode = \"max\", # we need the maximum accuracy.\n        restore_best_weights = True, # \n     )","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:25:20.291381Z","iopub.execute_input":"2023-02-28T04:25:20.292109Z","iopub.status.idle":"2023-02-28T04:25:20.298422Z","shell.execute_reply.started":"2023-02-28T04:25:20.292072Z","shell.execute_reply":"2023-02-28T04:25:20.296979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_gen, \n                    epochs = epochs, \n                    steps_per_epoch = train_steps,\n                    validation_data = test_gen,\n                    validation_steps = valid_steps, \n                    callbacks = [es])","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:25:22.851302Z","iopub.execute_input":"2023-02-28T04:25:22.852499Z","iopub.status.idle":"2023-02-28T04:26:47.816372Z","shell.execute_reply.started":"2023-02-28T04:25:22.852452Z","shell.execute_reply":"2023-02-28T04:26:47.815307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the loss and accuracy curves for training and validation \nfig, ax = plt.subplots(1,1, figsize=(20, 5))\nax.plot(history.history['loss'], color='b', label=\"Training loss\")\nax.plot(history.history['val_loss'], color='r', label=\"validation loss\",axes =ax)\nlegend = ax.legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:26:50.098030Z","iopub.execute_input":"2023-02-28T04:26:50.098727Z","iopub.status.idle":"2023-02-28T04:26:50.385138Z","shell.execute_reply.started":"2023-02-28T04:26:50.098690Z","shell.execute_reply":"2023-02-28T04:26:50.384109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,1, figsize=(20, 5))\nax.plot(history.history['categorical_accuracy'], color='b', label=\"Training accuracy\")\nax.plot(history.history['val_categorical_accuracy'], color='r',label=\"Validation accuracy\")\nlegend = ax.legend(loc='best', shadow=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:26:54.705912Z","iopub.execute_input":"2023-02-28T04:26:54.706583Z","iopub.status.idle":"2023-02-28T04:26:55.016049Z","shell.execute_reply.started":"2023-02-28T04:26:54.706539Z","shell.execute_reply":"2023-02-28T04:26:55.014826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['val_loss'], color='b', label=\"validation loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:26:58.392894Z","iopub.execute_input":"2023-02-28T04:26:58.393400Z","iopub.status.idle":"2023-02-28T04:26:58.621060Z","shell.execute_reply.started":"2023-02-28T04:26:58.393361Z","shell.execute_reply":"2023-02-28T04:26:58.620042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(10, 10)) # Set Figure\n\ny_pred = model.predict(X_test) # Predict class probabilities as 2 => [0.1, 0, 0.9, 0, 0, 0, 0, 0, 0, 0]\n\nY_pred = np.argmax(y_pred, 1) # Decode Predicted labels\nY_test = np.argmax(y_test, 1) # Decode labels\n\nmat = confusion_matrix(Y_test, Y_pred) # Confusion matrix\n\n# Plot Confusion matrix\nsns.heatmap(mat.T, square=True, annot=True, cbar=False, cmap=plt.cm.Blues, fmt='.0f')\nplt.xlabel('Predicted Values')\nplt.ylabel('True Values');\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:27:09.044184Z","iopub.execute_input":"2023-02-28T04:27:09.045195Z","iopub.status.idle":"2023-02-28T04:27:10.366260Z","shell.execute_reply.started":"2023-02-28T04:27:09.045125Z","shell.execute_reply":"2023-02-28T04:27:10.365124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\nX_test__ = X_test.reshape(X_test.shape[0], 28, 28)\n\nfig, axis = plt.subplots(4, 4, figsize=(12, 14))\nfor i, ax in enumerate(axis.flat):\n    ax.imshow(X_test__[i], cmap='binary')\n    ax.set(title = f\"Real Number is {y_test[i].argmax()}\\nPredict Number is {y_pred[i].argmax()}\");","metadata":{"execution":{"iopub.status.busy":"2023-02-28T04:27:13.343164Z","iopub.execute_input":"2023-02-28T04:27:13.344348Z","iopub.status.idle":"2023-02-28T04:27:16.018357Z","shell.execute_reply.started":"2023-02-28T04:27:13.344298Z","shell.execute_reply":"2023-02-28T04:27:16.017165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}