{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-20T13:43:25.862402Z","iopub.execute_input":"2022-07-20T13:43:25.862811Z","iopub.status.idle":"2022-07-20T13:43:25.874108Z","shell.execute_reply.started":"2022-07-20T13:43:25.862776Z","shell.execute_reply":"2022-07-20T13:43:25.872906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load data\ntrain=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:25.876351Z","iopub.execute_input":"2022-07-20T13:43:25.877167Z","iopub.status.idle":"2022-07-20T13:43:31.189452Z","shell.execute_reply.started":"2022-07-20T13:43:25.877107Z","shell.execute_reply":"2022-07-20T13:43:31.188190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.191399Z","iopub.execute_input":"2022-07-20T13:43:31.191716Z","iopub.status.idle":"2022-07-20T13:43:31.201505Z","shell.execute_reply.started":"2022-07-20T13:43:31.191687Z","shell.execute_reply":"2022-07-20T13:43:31.200496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.203045Z","iopub.execute_input":"2022-07-20T13:43:31.203359Z","iopub.status.idle":"2022-07-20T13:43:31.213560Z","shell.execute_reply.started":"2022-07-20T13:43:31.203332Z","shell.execute_reply":"2022-07-20T13:43:31.212675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train.iloc[:,1:]\nY_train = train.iloc[:,0]\nX_train","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.215923Z","iopub.execute_input":"2022-07-20T13:43:31.216712Z","iopub.status.idle":"2022-07-20T13:43:31.247741Z","shell.execute_reply.started":"2022-07-20T13:43:31.216679Z","shell.execute_reply":"2022-07-20T13:43:31.246528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.249634Z","iopub.execute_input":"2022-07-20T13:43:31.250090Z","iopub.status.idle":"2022-07-20T13:43:31.259060Z","shell.execute_reply.started":"2022-07-20T13:43:31.250047Z","shell.execute_reply":"2022-07-20T13:43:31.257761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.261133Z","iopub.execute_input":"2022-07-20T13:43:31.261821Z","iopub.status.idle":"2022-07-20T13:43:31.274262Z","shell.execute_reply.started":"2022-07-20T13:43:31.261778Z","shell.execute_reply":"2022-07-20T13:43:31.273031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nsns.countplot(Y_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.275994Z","iopub.execute_input":"2022-07-20T13:43:31.276719Z","iopub.status.idle":"2022-07-20T13:43:31.499308Z","shell.execute_reply.started":"2022-07-20T13:43:31.276673Z","shell.execute_reply":"2022-07-20T13:43:31.498341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.500780Z","iopub.execute_input":"2022-07-20T13:43:31.501184Z","iopub.status.idle":"2022-07-20T13:43:31.527480Z","shell.execute_reply.started":"2022-07-20T13:43:31.501139Z","shell.execute_reply":"2022-07-20T13:43:31.525878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.529309Z","iopub.execute_input":"2022-07-20T13:43:31.530028Z","iopub.status.idle":"2022-07-20T13:43:31.550464Z","shell.execute_reply.started":"2022-07-20T13:43:31.529982Z","shell.execute_reply":"2022-07-20T13:43:31.549109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"no missing value","metadata":{}},{"cell_type":"markdown","source":"**Normalization and reshape data**","metadata":{}},{"cell_type":"markdown","source":"normalize data","metadata":{}},{"cell_type":"code","source":"X_train/=255.0\ntest/=255.0","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.556086Z","iopub.execute_input":"2022-07-20T13:43:31.556458Z","iopub.status.idle":"2022-07-20T13:43:31.673319Z","shell.execute_reply.started":"2022-07-20T13:43:31.556428Z","shell.execute_reply":"2022-07-20T13:43:31.672357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reshape (heigth,width,channel)\nX_train=X_train.values.reshape((-1,28,28,1))\ntest=test.values.reshape((-1,28,28,1))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.674250Z","iopub.execute_input":"2022-07-20T13:43:31.674548Z","iopub.status.idle":"2022-07-20T13:43:31.680110Z","shell.execute_reply.started":"2022-07-20T13:43:31.674519Z","shell.execute_reply":"2022-07-20T13:43:31.679301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.681355Z","iopub.execute_input":"2022-07-20T13:43:31.681811Z","iopub.status.idle":"2022-07-20T13:43:31.693863Z","shell.execute_reply.started":"2022-07-20T13:43:31.681772Z","shell.execute_reply":"2022-07-20T13:43:31.692729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.695145Z","iopub.execute_input":"2022-07-20T13:43:31.695645Z","iopub.status.idle":"2022-07-20T13:43:31.882388Z","shell.execute_reply.started":"2022-07-20T13:43:31.695614Z","shell.execute_reply":"2022-07-20T13:43:31.881244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.title(Y_train[i])\n    plt.imshow(X_train[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:31.885526Z","iopub.execute_input":"2022-07-20T13:43:31.886392Z","iopub.status.idle":"2022-07-20T13:43:33.798382Z","shell.execute_reply.started":"2022-07-20T13:43:31.886344Z","shell.execute_reply":"2022-07-20T13:43:33.797110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"One Hot encoding for y_train ==> [0,1,0,0,0,0,0,0,0,0] ==> 1","metadata":{}},{"cell_type":"code","source":"Y_train=keras.utils.to_categorical(Y_train,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:33.800001Z","iopub.execute_input":"2022-07-20T13:43:33.800380Z","iopub.status.idle":"2022-07-20T13:43:33.807220Z","shell.execute_reply.started":"2022-07-20T13:43:33.800346Z","shell.execute_reply":"2022-07-20T13:43:33.805916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:33.808684Z","iopub.execute_input":"2022-07-20T13:43:33.809594Z","iopub.status.idle":"2022-07-20T13:43:33.818728Z","shell.execute_reply.started":"2022-07-20T13:43:33.809559Z","shell.execute_reply":"2022-07-20T13:43:33.817726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Split Traing data**","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:33.819950Z","iopub.execute_input":"2022-07-20T13:43:33.820576Z","iopub.status.idle":"2022-07-20T13:43:33.829093Z","shell.execute_reply.started":"2022-07-20T13:43:33.820544Z","shell.execute_reply":"2022-07-20T13:43:33.828247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_val,y_train,y_val=train_test_split(X_train,Y_train,test_size=0.1,random_state=44)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:33.830770Z","iopub.execute_input":"2022-07-20T13:43:33.831746Z","iopub.status.idle":"2022-07-20T13:43:34.266478Z","shell.execute_reply.started":"2022-07-20T13:43:33.831701Z","shell.execute_reply":"2022-07-20T13:43:34.265235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Conv2D,MaxPool2D,Activation,Dense,Flatten","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.267928Z","iopub.execute_input":"2022-07-20T13:43:34.268306Z","iopub.status.idle":"2022-07-20T13:43:34.274617Z","shell.execute_reply.started":"2022-07-20T13:43:34.268273Z","shell.execute_reply":"2022-07-20T13:43:34.273329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), padding='same', activation='relu', input_shape=(28, 28, 1)))\nmodel.add(MaxPool2D(strides=2))\nmodel.add(Conv2D(filters=48, kernel_size=(5,5), padding='valid', activation='relu'))\nmodel.add(MaxPool2D(strides=2))\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(84, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.276638Z","iopub.execute_input":"2022-07-20T13:43:34.277345Z","iopub.status.idle":"2022-07-20T13:43:34.355901Z","shell.execute_reply.started":"2022-07-20T13:43:34.277299Z","shell.execute_reply":"2022-07-20T13:43:34.354865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"adam = tf.keras.optimizers.Adam(learning_rate=5e-4)\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.357268Z","iopub.execute_input":"2022-07-20T13:43:34.357587Z","iopub.status.idle":"2022-07-20T13:43:34.368525Z","shell.execute_reply.started":"2022-07-20T13:43:34.357556Z","shell.execute_reply":"2022-07-20T13:43:34.367402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # # Data Augmentation","metadata":{}},{"cell_type":"code","source":"# Data Augmentation\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n            rotation_range=10, \n            width_shift_range=0.1, \n            height_shift_range=0.1, \n            zoom_range=0.1)\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.370133Z","iopub.execute_input":"2022-07-20T13:43:34.370825Z","iopub.status.idle":"2022-07-20T13:43:34.513570Z","shell.execute_reply.started":"2022-07-20T13:43:34.370791Z","shell.execute_reply":"2022-07-20T13:43:34.512370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set a learning rate annealer\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc', \n                                patience=3, \n                                verbose=1, \n                                factor=0.2, \n                                min_lr=1e-6)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.515228Z","iopub.execute_input":"2022-07-20T13:43:34.516030Z","iopub.status.idle":"2022-07-20T13:43:34.520767Z","shell.execute_reply.started":"2022-07-20T13:43:34.515994Z","shell.execute_reply":"2022-07-20T13:43:34.519989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(datagen.flow(x_train,y_train , batch_size=100), steps_per_epoch=len(x_train)/100, \n                    epochs=30, validation_data=(x_val, y_val),verbose=2, callbacks=[reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:43:34.522023Z","iopub.execute_input":"2022-07-20T13:43:34.522517Z","iopub.status.idle":"2022-07-20T13:55:58.392334Z","shell.execute_reply.started":"2022-07-20T13:43:34.522486Z","shell.execute_reply":"2022-07-20T13:55:58.391066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # # predict X_test file","metadata":{}},{"cell_type":"code","source":"model.evaluate(x_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:55:58.393868Z","iopub.execute_input":"2022-07-20T13:55:58.394225Z","iopub.status.idle":"2022-07-20T13:55:59.272096Z","shell.execute_reply.started":"2022-07-20T13:55:58.394189Z","shell.execute_reply":"2022-07-20T13:55:59.271067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:55:59.273921Z","iopub.execute_input":"2022-07-20T13:55:59.274246Z","iopub.status.idle":"2022-07-20T13:56:06.168833Z","shell.execute_reply.started":"2022-07-20T13:55:59.274215Z","shell.execute_reply":"2022-07-20T13:56:06.167985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=np.argmax(y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:56:06.170283Z","iopub.execute_input":"2022-07-20T13:56:06.171269Z","iopub.status.idle":"2022-07-20T13:56:06.176631Z","shell.execute_reply.started":"2022-07-20T13:56:06.171233Z","shell.execute_reply":"2022-07-20T13:56:06.175291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:56:06.182295Z","iopub.execute_input":"2022-07-20T13:56:06.182936Z","iopub.status.idle":"2022-07-20T13:56:06.190520Z","shell.execute_reply.started":"2022-07-20T13:56:06.182900Z","shell.execute_reply":"2022-07-20T13:56:06.189587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.title(y_test[i])\n    plt.imshow(test[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:56:06.191999Z","iopub.execute_input":"2022-07-20T13:56:06.192618Z","iopub.status.idle":"2022-07-20T13:56:08.050869Z","shell.execute_reply.started":"2022-07-20T13:56:06.192585Z","shell.execute_reply":"2022-07-20T13:56:08.050090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit=pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission = pd.DataFrame({\n        \"Id\": submit[\"ImageId\"],\n        \"Label\": y_test\n    })\npath='./'\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T13:56:08.052125Z","iopub.execute_input":"2022-07-20T13:56:08.052921Z","iopub.status.idle":"2022-07-20T13:56:08.117760Z","shell.execute_reply.started":"2022-07-20T13:56:08.052888Z","shell.execute_reply":"2022-07-20T13:56:08.116747Z"},"trusted":true},"execution_count":null,"outputs":[]}]}