{"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-03-26T16:19:39.690910Z","iopub.execute_input":"2022-03-26T16:19:39.691686Z","iopub.status.idle":"2022-03-26T16:19:39.724439Z","shell.execute_reply.started":"2022-03-26T16:19:39.691585Z","shell.execute_reply":"2022-03-26T16:19:39.723657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:39.957508Z","iopub.execute_input":"2022-03-26T16:19:39.958327Z","iopub.status.idle":"2022-03-26T16:19:46.156047Z","shell.execute_reply.started":"2022-03-26T16:19:39.958275Z","shell.execute_reply":"2022-03-26T16:19:46.155285Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:46.157825Z","iopub.execute_input":"2022-03-26T16:19:46.158080Z","iopub.status.idle":"2022-03-26T16:19:51.169631Z","shell.execute_reply.started":"2022-03-26T16:19:46.158029Z","shell.execute_reply":"2022-03-26T16:19:51.167524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.176244Z","iopub.execute_input":"2022-03-26T16:19:51.176744Z","iopub.status.idle":"2022-03-26T16:19:51.217108Z","shell.execute_reply.started":"2022-03-26T16:19:51.176701Z","shell.execute_reply":"2022-03-26T16:19:51.216098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.219113Z","iopub.execute_input":"2022-03-26T16:19:51.219373Z","iopub.status.idle":"2022-03-26T16:19:51.248559Z","shell.execute_reply.started":"2022-03-26T16:19:51.219339Z","shell.execute_reply":"2022-03-26T16:19:51.247686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.array(train.iloc[:,1:])\nlabels = np.array(train.iloc[:,0])\ntest_data = np.array(test)\nprint(type(train), type(data), type(labels), type(test_data))\nprint(train.shape, data.shape, labels.shape, test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.252940Z","iopub.execute_input":"2022-03-26T16:19:51.255588Z","iopub.status.idle":"2022-03-26T16:19:51.437998Z","shell.execute_reply.started":"2022-03-26T16:19:51.255545Z","shell.execute_reply":"2022-03-26T16:19:51.437277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(data, labels, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.441616Z","iopub.execute_input":"2022-03-26T16:19:51.442236Z","iopub.status.idle":"2022-03-26T16:19:51.874423Z","shell.execute_reply.started":"2022-03-26T16:19:51.442195Z","shell.execute_reply":"2022-03-26T16:19:51.873682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d = data.shape[0]\nt = x_train.shape[0]\nv = x_val.shape[0]\n\nm = data.shape[1]\n\nprint(\"Number of data samples: \", d, \" trainaing samples\", t, \" valuation samples\", v)\nprint (\"Number of features: \", m)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.876118Z","iopub.execute_input":"2022-03-26T16:19:51.876414Z","iopub.status.idle":"2022-03-26T16:19:51.883846Z","shell.execute_reply.started":"2022-03-26T16:19:51.876373Z","shell.execute_reply":"2022-03-26T16:19:51.883122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Display some samples*\n-","metadata":{}},{"cell_type":"code","source":"index = np.random.randint(0, d, 25)\nplt.figure(figsize=(14,7))\nfor i, idx in enumerate(index):\n    plt.subplot(5, 5, i+1)\n    plt.subplots_adjust(hspace=0.5)\n    plt.imshow(np.resize(data[idx], (28,28,1)))\n    plt.title(labels[idx])","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:51.885304Z","iopub.execute_input":"2022-03-26T16:19:51.885816Z","iopub.status.idle":"2022-03-26T16:19:53.846682Z","shell.execute_reply.started":"2022-03-26T16:19:51.885779Z","shell.execute_reply":"2022-03-26T16:19:53.845941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Data in each category*\n-","metadata":{}},{"cell_type":"code","source":"x_labels = sorted(set(labels))\ny_labels = [labels.tolist().count(i) for i in x_labels]\n\nplt.figure(figsize = (15,5))\nplt.xticks([i for i in x_labels])\nplt.bar(x_labels, y_labels)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:53.847800Z","iopub.execute_input":"2022-03-26T16:19:53.849222Z","iopub.status.idle":"2022-03-26T16:19:54.142538Z","shell.execute_reply.started":"2022-03-26T16:19:53.849181Z","shell.execute_reply":"2022-03-26T16:19:54.141874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Resize the image*\n-","metadata":{}},{"cell_type":"code","source":"print(\"The dimension of train data is:\", data.shape)\nprint(\"The dimension of test data is:\", test_data.shape)\n\ndata_re = np.reshape(data, (data.shape[0], 28, 28, 1))\ntest_data_re = np.reshape(test_data, (test_data.shape[0], 28, 28, 1))\n\n\nprint(\"The dimension of train data is:\", data_re.shape)\nprint(\"The dimension of test data is:\", test_data_re.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:54.145389Z","iopub.execute_input":"2022-03-26T16:19:54.145617Z","iopub.status.idle":"2022-03-26T16:19:54.152608Z","shell.execute_reply.started":"2022-03-26T16:19:54.145586Z","shell.execute_reply":"2022-03-26T16:19:54.151794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*One-hot encoding*\n-","metadata":{}},{"cell_type":"code","source":"labels_cat = to_categorical(labels)\nprint(labels_cat.shape)\nprint(labels[1], \"in now \", labels_cat[1])","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:54.154183Z","iopub.execute_input":"2022-03-26T16:19:54.154654Z","iopub.status.idle":"2022-03-26T16:19:54.164009Z","shell.execute_reply.started":"2022-03-26T16:19:54.154616Z","shell.execute_reply":"2022-03-26T16:19:54.162820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Normalising the data*\n-","metadata":{}},{"cell_type":"code","source":"data_norm = data_re/255\ntest_data_norm = test_data_re/255\n\n\nprint(data_re.shape, data_norm.shape, test_data_norm.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:54.165042Z","iopub.execute_input":"2022-03-26T16:19:54.165593Z","iopub.status.idle":"2022-03-26T16:19:54.339557Z","shell.execute_reply.started":"2022-03-26T16:19:54.165552Z","shell.execute_reply":"2022-03-26T16:19:54.338769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(data_norm, labels_cat, test_size=0.1, random_state=42)\nprint(data_norm.shape, X_train.shape, X_val.shape)\nprint(labels_cat.shape, Y_train.shape, Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:54.340700Z","iopub.execute_input":"2022-03-26T16:19:54.341034Z","iopub.status.idle":"2022-03-26T16:19:54.726789Z","shell.execute_reply.started":"2022-03-26T16:19:54.340995Z","shell.execute_reply":"2022-03-26T16:19:54.726004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Data Augmentation*\n-","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nimport tensorflow as tf\nfrom tensorflow.keras import layers\n#REsize and rescale is already done in\n#resize_and_rescale = tf.keras.Sequential([\n    #layers.Resizing(128,128)\n    #layers.Rescaling(1./255)\n#])\n\ndata_augmentation = Sequential([\n    #layers.RandomFlip(\"horizontal_and_vertical\"),\n    layers.RandomRotation(0.05),\n    layers.RandomZoom((-0.3, 0.3),(-0.3,0.3))\n])","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:54.728268Z","iopub.execute_input":"2022-03-26T16:19:54.728685Z","iopub.status.idle":"2022-03-26T16:19:57.142877Z","shell.execute_reply.started":"2022-03-26T16:19:54.728635Z","shell.execute_reply":"2022-03-26T16:19:57.142125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nfor i in range(9):\n    plt.subplot(3, 3, i+1)\n    plt.imshow(data_augmentation(np.resize(data[6], (28,28,1))))\n   ","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:57.144225Z","iopub.execute_input":"2022-03-26T16:19:57.144622Z","iopub.status.idle":"2022-03-26T16:19:58.744161Z","shell.execute_reply.started":"2022-03-26T16:19:57.144584Z","shell.execute_reply":"2022-03-26T16:19:58.743445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Model Training*\n-","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import *\n\n\nmodel = Sequential()\n\n#Data augmentation layer\n#model.add(InputLayer(input_shape=(28, 28, 1)))\nmodel.add(RandomRotation(0.05, input_shape=(28,28,1)))\nmodel.add(RandomZoom((-0.3, 0.3),(-0.3,0.3)))\n\nmodel.add(Conv2D(filters=32, kernel_size=(3,3), padding='same', activation='relu'))#,input_shape=(28, 28, 1)))\nmodel.add(MaxPool2D(pool_size = (2,2)))\nmodel.add(Dropout(rate=0.2))\n\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(filters=64, kernel_size=(3,3), padding='same', activation='relu'))\nmodel.add(MaxPool2D(pool_size = (2,2)))\nmodel.add(Dropout(rate=0.2))\n\nmodel.add(Flatten())\n\nmodel.add(BatchNormalization())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:58.748104Z","iopub.execute_input":"2022-03-26T16:19:58.751195Z","iopub.status.idle":"2022-03-26T16:19:59.012157Z","shell.execute_reply.started":"2022-03-26T16:19:58.751154Z","shell.execute_reply":"2022-03-26T16:19:59.011416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#input_shape=(42000,28,28,1)\n#model.build(input_shape)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:59.013410Z","iopub.execute_input":"2022-03-26T16:19:59.013639Z","iopub.status.idle":"2022-03-26T16:19:59.027409Z","shell.execute_reply.started":"2022-03-26T16:19:59.013608Z","shell.execute_reply":"2022-03-26T16:19:59.021814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:19:59.028769Z","iopub.execute_input":"2022-03-26T16:19:59.029017Z","iopub.status.idle":"2022-03-26T16:19:59.042262Z","shell.execute_reply.started":"2022-03-26T16:19:59.028985Z","shell.execute_reply":"2022-03-26T16:19:59.041515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train, Y_train, epochs=100, batch_size =100, validation_data = (X_val, Y_val))","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:23:22.121538Z","iopub.execute_input":"2022-03-26T16:23:22.121807Z","iopub.status.idle":"2022-03-26T16:23:26.112324Z","shell.execute_reply.started":"2022-03-26T16:23:22.121776Z","shell.execute_reply":"2022-03-26T16:23:26.111609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Plotting the  loss and accuracy*\n-","metadata":{}},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='acc')\nplt.plot(history.history['val_accuracy'], label='val_acc')\nplt.legend()\nplt.plot()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:22:00.787232Z","iopub.execute_input":"2022-03-26T16:22:00.787502Z","iopub.status.idle":"2022-03-26T16:22:00.999504Z","shell.execute_reply.started":"2022-03-26T16:22:00.787472Z","shell.execute_reply":"2022-03-26T16:22:00.998775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*Predecting and submitting on test set*\n-","metadata":{}},{"cell_type":"code","source":"predect_x = model.predict(test_data_norm)\npred_class_x = np.argmax(predect_x, axis = 1)\nsubmissions=pd.DataFrame({\"ImageId\": list(range(1,len(pred_class_x)+1)),\"Label\": pred_class_x})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T16:23:37.090377Z","iopub.execute_input":"2022-03-26T16:23:37.090741Z","iopub.status.idle":"2022-03-26T16:23:38.496327Z","shell.execute_reply.started":"2022-03-26T16:23:37.090615Z","shell.execute_reply":"2022-03-26T16:23:38.495525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}