{"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 tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import InputLayer\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import LeakyReLU\nfrom tensorflow.keras.layers import BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nfrom itertools import cycle\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:20.880732Z","iopub.execute_input":"2021-08-20T07:53:20.881093Z","iopub.status.idle":"2021-08-20T07:53:28.061215Z","shell.execute_reply.started":"2021-08-20T07:53:20.881009Z","shell.execute_reply":"2021-08-20T07:53:28.060408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TrainData = pd.read_csv(r'../input/digit-recognizer/train.csv')\nTrainData\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:28.062578Z","iopub.execute_input":"2021-08-20T07:53:28.062904Z","iopub.status.idle":"2021-08-20T07:53:31.262436Z","shell.execute_reply.started":"2021-08-20T07:53:28.062869Z","shell.execute_reply":"2021-08-20T07:53:31.261626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TestData = pd.read_csv(r'../input/digit-recognizer/test.csv')\nTestData\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:35.638279Z","iopub.execute_input":"2021-08-20T07:53:35.638634Z","iopub.status.idle":"2021-08-20T07:53:37.127948Z","shell.execute_reply.started":"2021-08-20T07:53:35.638604Z","shell.execute_reply":"2021-08-20T07:53:37.126837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.isnan(np.array(TrainData)).any()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:39.315061Z","iopub.execute_input":"2021-08-20T07:53:39.315411Z","iopub.status.idle":"2021-08-20T07:53:39.427149Z","shell.execute_reply.started":"2021-08-20T07:53:39.315368Z","shell.execute_reply":"2021-08-20T07:53:39.426150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.isnan(np.array(TestData)).any()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:40.876537Z","iopub.execute_input":"2021-08-20T07:53:40.876899Z","iopub.status.idle":"2021-08-20T07:53:40.953151Z","shell.execute_reply.started":"2021-08-20T07:53:40.876866Z","shell.execute_reply":"2021-08-20T07:53:40.952196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TrainData = TrainData.copy()\r\nLabel = TrainData.pop('label')\r\nsns.countplot(x=Label)\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:42.274918Z","iopub.execute_input":"2021-08-20T07:53:42.275240Z","iopub.status.idle":"2021-08-20T07:53:42.627219Z","shell.execute_reply.started":"2021-08-20T07:53:42.275209Z","shell.execute_reply":"2021-08-20T07:53:42.626280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples = TrainData.sample(25)\r\nplt.figure(figsize=(20, 20))\r\n\r\nf, ax = plt.subplots(5, 5, figsize=(10, 10))\r\nfor i in range(25):\r\n    ax[i//5, i % 5].imshow(np.array(samples[i:i+1]).reshape(28, 28))\r\n    ax[i//5, i % 5].axis('off')\r\n    ax[i//5, i % 5].set_title(Label[samples.index].values[i], fontsize='12')\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T07:53:44.360832Z","iopub.execute_input":"2021-08-20T07:53:44.361148Z","iopub.status.idle":"2021-08-20T07:53:45.674414Z","shell.execute_reply.started":"2021-08-20T07:53:44.361118Z","shell.execute_reply":"2021-08-20T07:53:45.673609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DS1, DS2, L1, L2 = train_test_split(TrainData, Label, test_size=0.2)\n\nDS1 = tf.reshape(tf.constant(DS1), (-1, 28, 28, 1))/255\nDS2 = tf.reshape(tf.constant(DS2), (-1, 28, 28, 1))/255\nL1 = tf.one_hot(L1, 10)\nL2 = tf.one_hot(L2, 10)\n\nDS = tf.reshape(tf.constant(TrainData), (-1, 28, 28, 1))/255\nL = tf.one_hot(Label, 10)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T08:00:38.750810Z","iopub.execute_input":"2021-08-20T08:00:38.751134Z","iopub.status.idle":"2021-08-20T08:00:39.752889Z","shell.execute_reply.started":"2021-08-20T08:00:38.751104Z","shell.execute_reply":"2021-08-20T08:00:39.752028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MyCallBack(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epoch, log):\n        if log['accuracy'] > 0.9995:\n            self.model.stop_training = True\n\n\nReduceLR = tf.keras.callbacks.ReduceLROnPlateau(\n    monitor='accuracy', factor=0.2, patience=3)\n\nStop = MyCallBack()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T08:01:22.505833Z","iopub.execute_input":"2021-08-20T08:01:22.506150Z","iopub.status.idle":"2021-08-20T08:01:22.511456Z","shell.execute_reply.started":"2021-08-20T08:01:22.506118Z","shell.execute_reply":"2021-08-20T08:01:22.510509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nImageModifier = ImageDataGenerator(\n    rotation_range=10,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n)\n\n\nModel = Sequential([\n\n    InputLayer((28, 28, 1)),\n    Dropout(0.1),\n    Conv2D(128, 3, use_bias=False),\n    LeakyReLU(0.05),\n    BatchNormalization(),\n    MaxPooling2D(2, 2),\n    Conv2D(64, 3, use_bias=False),\n    LeakyReLU(0.05),\n    BatchNormalization(),\n    MaxPooling2D(2, 2),\n    Flatten(),\n    Dense(128, use_bias=False),\n    LeakyReLU(0.05),\n    BatchNormalization(),\n    Dense(10, activation='softmax')\n\n])\n\n\nModel.compile(optimizer='adam',\n              loss='categorical_crossentropy', metrics='accuracy')\n\nModel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T08:01:24.495198Z","iopub.execute_input":"2021-08-20T08:01:24.495529Z","iopub.status.idle":"2021-08-20T08:01:24.591839Z","shell.execute_reply.started":"2021-08-20T08:01:24.495497Z","shell.execute_reply":"2021-08-20T08:01:24.590489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Epochs = 50\nHistory = Model.fit(ImageModifier.flow(DS1,L1), validation_data=(DS2,L2), epochs=Epochs,\n                    callbacks=[ReduceLR, Stop], verbose=0)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T08:03:30.137956Z","iopub.execute_input":"2021-08-20T08:03:30.138292Z","iopub.status.idle":"2021-08-20T08:03:56.405514Z","shell.execute_reply.started":"2021-08-20T08:03:30.138258Z","shell.execute_reply":"2021-08-20T08:03:56.404742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Acc = History.history['accuracy']\r\nLoss = History.history['loss']\r\nValAcc = History.history['val_accuracy']\r\nValLoss = History.history['val_loss']\r\nfig = px.line(x=History.epoch, y=[Acc, ValAcc], title='Accuracy', labels={\r\n              \"x\": \"Epoch\", \"value\": \"Accuracy\", })\r\nfig.update_layout(plot_bgcolor=\"pink\")\r\nnames = cycle(['accuracy', 'val_accuracy'])\r\nfig.for_each_trace(lambda t:  t.update(name=next(names)))\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.046456Z","iopub.status.idle":"2021-08-20T05:03:14.046934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = px.line(x=History.epoch, y=[Loss, ValLoss], title='Loss', labels={\n              \"x\": \"Epoch\", \"value\": \"Loss\", })\nfig.update_layout(plot_bgcolor=\"pink\")\nnames = cycle(['loss', 'val_loss'])\nfig.for_each_trace(lambda t:  t.update(name=next(names)))\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"L2Predict = Model.predict(DS2)\r\nerror = (np.argmax(L2Predict, axis=1)-np.argmax(L2, axis=1) != 0)\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.052049Z","iopub.status.idle":"2021-08-20T05:03:14.052744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ConfusionMatrix = tf.math.confusion_matrix(\r\n    np.argmax(L2, axis=1), np.argmax(L2Predict, axis=1))\r\nsns.heatmap(ConfusionMatrix, annot=True, fmt='g')\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.054039Z","iopub.status.idle":"2021-08-20T05:03:14.054622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\r\nsamples = DS2[error]\r\n\r\nplt.figure(figsize=(20, 20))\r\nf, ax = plt.subplots(1, 5, figsize=(10, 10))\r\nfor i in range(5):\r\n    ax[i].imshow(np.array(samples[i:i+1]).reshape(28, 28))\r\n    ax[i].axis('off')\r\n    ax[i].set_title(str(np.argmax(L2Predict, axis=1)[error][i]) +\r\n                    ' in fact '+str(np.argmax(L2, axis=1)[error][i]), fontsize='12')\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.055829Z","iopub.status.idle":"2021-08-20T05:03:14.056445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get full use of the Training Data\nHistory = Model.fit(ImageModifier.flow(DS,L), epochs=Epochs,\n                    callbacks=[ReduceLR, Stop], verbose=0)\n\nModel.save(r'./model.h5')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TestData = tf.reshape(tf.constant(TestData), (-1, 28, 28, 1))/255\nresult = np.argmax(Model.predict(TestData), axis=1)\nresult\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.057377Z","iopub.status.idle":"2021-08-20T05:03:14.057799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Submission = pd.read_csv(r'../input/digit-recognizer/sample_submission.csv')\r\nSubmission.Label = result\r\n\r\nSubmission.to_csv(r'./sample_submission.csv', index=False)\r\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T05:03:14.058728Z","iopub.status.idle":"2021-08-20T05:03:14.059182Z"},"trusted":true},"execution_count":null,"outputs":[]}]}