{"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":"#importing necessary libraries\nimport numpy as np \nimport pandas as pd \n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom keras.models import Sequential\nfrom keras.layers import Input,Conv2D,Activation,Dense,Lambda,Flatten,Embedding,PReLU,BatchNormalization,Dropout,MaxPooling2D,GlobalMaxPooling2D,GlobalAveragePooling2D\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Model\nfrom keras import  backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import EarlyStopping\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import categorical_crossentropy, binary_crossentropy, mean_squared_error,sparse_categorical_crossentropy\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau,LearningRateScheduler, TensorBoard, EarlyStopping, ModelCheckpoint\n\ntrain=pd.read_csv('../input/digit-recognizer/train.csv')\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:48.235549Z","iopub.execute_input":"2021-11-02T19:01:48.236388Z","iopub.status.idle":"2021-11-02T19:01:56.606549Z","shell.execute_reply.started":"2021-11-02T19:01:48.236257Z","shell.execute_reply":"2021-11-02T19:01:56.605808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('../input/digit-recognizer/test.csv')\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:56.608158Z","iopub.execute_input":"2021-11-02T19:01:56.609451Z","iopub.status.idle":"2021-11-02T19:01:58.432447Z","shell.execute_reply.started":"2021-11-02T19:01:56.609411Z","shell.execute_reply":"2021-11-02T19:01:58.431677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=train.to_numpy()\ntest =test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.433801Z","iopub.execute_input":"2021-11-02T19:01:58.434605Z","iopub.status.idle":"2021-11-02T19:01:58.439258Z","shell.execute_reply.started":"2021-11-02T19:01:58.434568Z","shell.execute_reply":"2021-11-02T19:01:58.438445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train[:,1:] # all pixel values\ny_train = train[:,0] # only labels i.e targets digits\nX_test  = test","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.441735Z","iopub.execute_input":"2021-11-02T19:01:58.442226Z","iopub.status.idle":"2021-11-02T19:01:58.447663Z","shell.execute_reply.started":"2021-11-02T19:01:58.442190Z","shell.execute_reply":"2021-11-02T19:01:58.446908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28,1)\nX_test = X_test.reshape(X_test.shape[0], 28, 28,1)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.448842Z","iopub.execute_input":"2021-11-02T19:01:58.449636Z","iopub.status.idle":"2021-11-02T19:01:58.457029Z","shell.execute_reply.started":"2021-11-02T19:01:58.449599Z","shell.execute_reply":"2021-11-02T19:01:58.456390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ny_train= to_categorical(y_train)\nnum_classes = y_train.shape[1]\nnum_classes","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.459201Z","iopub.execute_input":"2021-11-02T19:01:58.459694Z","iopub.status.idle":"2021-11-02T19:01:58.468670Z","shell.execute_reply.started":"2021-11-02T19:01:58.459663Z","shell.execute_reply":"2021-11-02T19:01:58.467793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\ngen = image.ImageDataGenerator()\n# fix random seed for reproducibility\nseed = 42\nnp.random.seed(seed)\n\nfrom sklearn.model_selection import train_test_split\nX = X_train\ny = y_train\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)\nbatches = gen.flow(X_train, y_train, batch_size=64)\nval_batches=gen.flow(X_val, y_val, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.470470Z","iopub.execute_input":"2021-11-02T19:01:58.470792Z","iopub.status.idle":"2021-11-02T19:01:58.904262Z","shell.execute_reply.started":"2021-11-02T19:01:58.470758Z","shell.execute_reply":"2021-11-02T19:01:58.903539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs=Input((28,28,1))\nx=Conv2D(32,(3,3),kernel_initializer='he_uniform')(inputs)\nx=BatchNormalization()(x)\nx=PReLU()(x)\n#x=Dropout(0.5)(x)\n\nx=Conv2D(32,(3,3),kernel_initializer='he_uniform')(x)\nx=BatchNormalization()(x)\nx=PReLU()(x)\n#x=Dropout(0.5)(x)\n\nx=Conv2D(64,(3,3),kernel_initializer='he_uniform')(x)\nx=BatchNormalization()(x)\nx=PReLU()(x)\n#x=Dropout(0.5)(x)\n\nx=Conv2D(64,(3,3),kernel_initializer='he_uniform')(x)\nx=BatchNormalization()(x)\nx=PReLU()(x)\nx=MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)\n#x=Dropout(0.5)(x)\n\nx=Conv2D(128,(3,3),kernel_initializer='he_uniform')(x)\nx=BatchNormalization()(x)\nx=PReLU()(x)\nx=MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)\n#x=Dropout(0.5)(x)\n\nx=Conv2D(128,(3,3),kernel_initializer='he_uniform')(x)\nx=BatchNormalization()(x)\nx=PReLU()(x)\n#x=Dropout(0.5)(x)\n\n#x=Flatten()(x)\nx=GlobalAveragePooling2D()(x)\nx=Dense(64)(x)\nout1=PReLU(name=\"out1\")(x)\n\nout2=Dense(10,activation=\"softmax\")(out1)\n\nmodel=Model(inputs,out2)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:01:58.905995Z","iopub.execute_input":"2021-11-02T19:01:58.906766Z","iopub.status.idle":"2021-11-02T19:02:01.228699Z","shell.execute_reply.started":"2021-11-02T19:01:58.906726Z","shell.execute_reply":"2021-11-02T19:02:01.227968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=categorical_crossentropy,\n              optimizer=Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-7),\n              metrics=['accuracy'])\n\nlr_reducer = ReduceLROnPlateau('val_accuracy', factor=0.08, patience=3, verbose=1, mode='auto')\ntensorboard = TensorBoard(log_dir='./logs')\nearly_stopper = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=8, verbose=1, mode='auto')\n\nmodel.fit(X_train,y_train,batch_size=32,epochs=100,verbose=1,validation_data=(X_val,y_val),\n         shuffle=True,\n          callbacks=[lr_reducer, tensorboard, early_stopper] )\n                    ","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:02:01.230201Z","iopub.execute_input":"2021-11-02T19:02:01.230469Z","iopub.status.idle":"2021-11-02T19:06:34.466451Z","shell.execute_reply.started":"2021-11-02T19:02:01.230435Z","shell.execute_reply":"2021-11-02T19:06:34.465769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test,batch_size=64)\nimage_id = range(1, predictions.shape[0] + 1)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:06:34.469112Z","iopub.execute_input":"2021-11-02T19:06:34.469518Z","iopub.status.idle":"2021-11-02T19:06:36.214335Z","shell.execute_reply.started":"2021-11-02T19:06:34.469485Z","shell.execute_reply":"2021-11-02T19:06:36.213614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = [np.argmax(i) for i in predictions]","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:06:36.215792Z","iopub.execute_input":"2021-11-02T19:06:36.216040Z","iopub.status.idle":"2021-11-02T19:06:36.297082Z","shell.execute_reply.started":"2021-11-02T19:06:36.216007Z","shell.execute_reply":"2021-11-02T19:06:36.296437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:06:36.298177Z","iopub.execute_input":"2021-11-02T19:06:36.298469Z","iopub.status.idle":"2021-11-02T19:06:36.323771Z","shell.execute_reply.started":"2021-11-02T19:06:36.298432Z","shell.execute_reply":"2021-11-02T19:06:36.323108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': image_id, 'Label': pred})\nsubmission.to_csv('submission', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:06:36.324888Z","iopub.execute_input":"2021-11-02T19:06:36.325122Z","iopub.status.idle":"2021-11-02T19:06:36.392767Z","shell.execute_reply.started":"2021-11-02T19:06:36.325091Z","shell.execute_reply":"2021-11-02T19:06:36.391764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-02T19:06:36.394634Z","iopub.execute_input":"2021-11-02T19:06:36.395233Z","iopub.status.idle":"2021-11-02T19:06:36.404865Z","shell.execute_reply.started":"2021-11-02T19:06:36.395193Z","shell.execute_reply":"2021-11-02T19:06:36.403674Z"},"trusted":true},"execution_count":null,"outputs":[]}]}