{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# 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":"2022-10-10T18:06:37.312428Z","iopub.execute_input":"2022-10-10T18:06:37.313232Z","iopub.status.idle":"2022-10-10T18:06:37.343783Z","shell.execute_reply.started":"2022-10-10T18:06:37.313144Z","shell.execute_reply":"2022-10-10T18:06:37.341475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow.data import AUTOTUNE\nfrom keras.datasets import mnist","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:37.348995Z","iopub.execute_input":"2022-10-10T18:06:37.349847Z","iopub.status.idle":"2022-10-10T18:06:42.882892Z","shell.execute_reply.started":"2022-10-10T18:06:37.349802Z","shell.execute_reply":"2022-10-10T18:06:42.881859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.layers import Input\nfrom tensorflow.keras.layers import RandomTranslation, RandomZoom, RandomRotation\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:42.884524Z","iopub.execute_input":"2022-10-10T18:06:42.885503Z","iopub.status.idle":"2022-10-10T18:06:43.249033Z","shell.execute_reply.started":"2022-10-10T18:06:42.885461Z","shell.execute_reply":"2022-10-10T18:06:43.246334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model definition and transfer learning","metadata":{}},{"cell_type":"code","source":"model = tf.keras.Sequential()\n\n# Input\nmodel.add(Input((28, 28, 1)))\n\nmodel.add(Conv2D(32, (3, 3), activation = 'relu'))\nmodel.add(Conv2D(32, (3, 3), activation = 'relu'))\nmodel.add(Conv2D(32, (3, 3), activation = 'relu'))\nmodel.add(Conv2D(32, (3, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(64, (3, 3), activation = 'relu'))\nmodel.add(Conv2D(64, (3, 3), activation = 'relu'))\nmodel.add(Conv2D(64, (3, 3), activation = 'relu'))\nmodel.add(MaxPooling2D(2, 2))\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:46.350180Z","iopub.execute_input":"2022-10-10T18:06:46.351109Z","iopub.status.idle":"2022-10-10T18:06:49.457119Z","shell.execute_reply.started":"2022-10-10T18:06:46.351054Z","shell.execute_reply":"2022-10-10T18:06:49.456197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = 'adam', \n              loss = 'categorical_crossentropy', \n              metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:49.458616Z","iopub.execute_input":"2022-10-10T18:06:49.459014Z","iopub.status.idle":"2022-10-10T18:06:49.471908Z","shell.execute_reply.started":"2022-10-10T18:06:49.458977Z","shell.execute_reply":"2022-10-10T18:06:49.470858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data ","metadata":{}},{"cell_type":"code","source":"(training_images, training_labels), (test_images, test_labels) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:49.473603Z","iopub.execute_input":"2022-10-10T18:06:49.474026Z","iopub.status.idle":"2022-10-10T18:06:50.210990Z","shell.execute_reply.started":"2022-10-10T18:06:49.473989Z","shell.execute_reply":"2022-10-10T18:06:50.209547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_full = np.concatenate((training_images, test_images))\ny_train_full = np.concatenate((training_labels, test_labels))","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:50.216120Z","iopub.execute_input":"2022-10-10T18:06:50.218646Z","iopub.status.idle":"2022-10-10T18:06:50.250445Z","shell.execute_reply.started":"2022-10-10T18:06:50.218593Z","shell.execute_reply":"2022-10-10T18:06:50.249595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid = pd.read_csv('../input/digit-recognizer/train.csv').to_numpy()\ntest = pd.read_csv('../input/digit-recognizer/test.csv').to_numpy()\nsample_submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv').to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:50.254914Z","iopub.execute_input":"2022-10-10T18:06:50.255256Z","iopub.status.idle":"2022-10-10T18:06:55.165933Z","shell.execute_reply.started":"2022-10-10T18:06:50.255222Z","shell.execute_reply":"2022-10-10T18:06:55.164092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y = tf.keras.utils.to_categorical(y_train_full)\nvalid_Y = tf.keras.utils.to_categorical(valid[:,0])\n\n# Preprocess Normalisation :\ntrain_X = X_train_full / 255.0\nvalid_X = valid[:,1:] / 255.0\ntest_X = test / 255.0\n\n# Preprocess for tensor inputs :\ntrain_X = train_X.reshape(-1, 28, 28, 1)\nvalid_X = valid_X.reshape(-1, 28, 28, 1)\ntest_X = test_X.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:55.169597Z","iopub.execute_input":"2022-10-10T18:06:55.170043Z","iopub.status.idle":"2022-10-10T18:06:55.564393Z","shell.execute_reply.started":"2022-10-10T18:06:55.170005Z","shell.execute_reply":"2022-10-10T18:06:55.563347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BS = 128 # Batch Size\nEPOCHS = 25\n\nSTEPS = len(train_X) // BS\n\nLABELS = 10","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:55.565670Z","iopub.execute_input":"2022-10-10T18:06:55.566749Z","iopub.status.idle":"2022-10-10T18:06:55.571870Z","shell.execute_reply.started":"2022-10-10T18:06:55.566707Z","shell.execute_reply":"2022-10-10T18:06:55.570651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_DS = tf.data.Dataset.from_tensor_slices((train_X, train_Y))\nvalid_DS = tf.data.Dataset.from_tensor_slices((valid_X, valid_Y))\n\ntrain_DS = train_DS.shuffle(STEPS * BS).batch(BS).repeat().prefetch(AUTOTUNE)\nvalid_DS = valid_DS.batch(BS)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:06:55.573083Z","iopub.execute_input":"2022-10-10T18:06:55.574056Z","iopub.status.idle":"2022-10-10T18:06:57.170828Z","shell.execute_reply.started":"2022-10-10T18:06:55.574023Z","shell.execute_reply":"2022-10-10T18:06:57.169760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:07:40.279684Z","iopub.execute_input":"2022-10-10T18:07:40.280654Z","iopub.status.idle":"2022-10-10T18:07:40.285614Z","shell.execute_reply.started":"2022-10-10T18:07:40.280596Z","shell.execute_reply":"2022-10-10T18:07:40.284621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(patience = 5,\n                                              min_delta = 1e-3,\n                                              restore_best_weights = True)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:07:41.599826Z","iopub.execute_input":"2022-10-10T18:07:41.600190Z","iopub.status.idle":"2022-10-10T18:07:41.605049Z","shell.execute_reply.started":"2022-10-10T18:07:41.600158Z","shell.execute_reply":"2022-10-10T18:07:41.603862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_DS,\n                validation_data = valid_DS,\n                epochs = EPOCHS,\n                steps_per_epoch = STEPS,\n                callbacks = [early_stopping])","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:07:45.120583Z","iopub.execute_input":"2022-10-10T18:07:45.121697Z","iopub.status.idle":"2022-10-10T18:09:14.468066Z","shell.execute_reply.started":"2022-10-10T18:07:45.121626Z","shell.execute_reply":"2022-10-10T18:09:14.467119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_X)\nclasses = pred.argmax(axis=-1)\nsub = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsub.Label = classes\n","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:10:30.278146Z","iopub.execute_input":"2022-10-10T18:10:30.278531Z","iopub.status.idle":"2022-10-10T18:11:10.950435Z","shell.execute_reply.started":"2022-10-10T18:10:30.278496Z","shell.execute_reply":"2022-10-10T18:11:10.949422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T18:11:10.952170Z","iopub.execute_input":"2022-10-10T18:11:10.952537Z","iopub.status.idle":"2022-10-10T18:11:10.984963Z","shell.execute_reply.started":"2022-10-10T18:11:10.952500Z","shell.execute_reply":"2022-10-10T18:11:10.984069Z"},"trusted":true},"execution_count":null,"outputs":[]}]}