{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D,MaxPool2D,Dropout,Dense,Flatten,Activation,BatchNormalization\nfrom keras.models import Sequential\nfrom keras.optimizers import Adam\nfrom keras.losses import categorical_crossentropy\nfrom keras.callbacks import LearningRateScheduler\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import classification_report\nfrom IPython.display import clear_output,FileLink\n\nclear_output()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.backend import tensorflow_backend\ntensorflow_backend._get_available_gpus()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Acquisition"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train=pd.read_csv(r'../input/train.csv')\ntest=pd.read_csv(r'../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=train.iloc[:,1:].values\ndata=data.reshape(-1,28,28,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels=pd.get_dummies(train.iloc[:,0]).values","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Visualization:"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,ax=plt.subplots(5,10)\nfor i in range(5):\n    for j in range(10):\n        ax[i][j].imshow(data[np.random.randint(0,data.shape[0]),:,:,0],cmap=plt.cm.binary)\n        ax[i][j].axis('off')\nplt.subplots_adjust(wspace=0, hspace=0)        \nfig.set_figwidth(15)\nfig.set_figheight(7)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Splitting data into Training & Validation Sets"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train,X_val,y_train,y_val=train_test_split(data,labels,test_size=0.33,random_state=42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Data Augmentation:"},{"metadata":{"trusted":true},"cell_type":"code","source":"aug_data=ImageDataGenerator(\n        rotation_range=10,  \n        zoom_range = 0.10,  \n        width_shift_range=0.1, \n        height_shift_range=0.1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Example:"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,ax=plt.subplots(3,10)\nidx=np.random.randint(0,data.shape[0])\nfor i in range(3):\n    for j in range(10):\n        ax[i][j].axis('off')\n        X,y=aug_data.flow(data[idx].reshape(-1,28,28,1),labels[idx].reshape(1,10)).next()\n        ax[i][j].imshow(X.reshape(28,28),cmap=plt.cm.binary)\nfig.set_figheight(5)\nfig.set_figwidth(15)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"generator=aug_data.flow(X_train,y_train,batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model Creation"},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model():\n    model=Sequential()\n\n    model.add(Conv2D(32,(3,3),input_shape=(28,28,1),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32,(5,5),strides=(2,2),padding='same',activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(64,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64,(5,5),strides=(2,2),padding='same',activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(128,(4,4),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Flatten())\n    model.add(Dense(128,activation='relu'))\n\n    model.add(Dropout(0.4))\n    model.add(Dense(10,activation='softmax'))\n\n    model.compile(optimizer='adam',loss=categorical_crossentropy,metrics=['accuracy'])\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=create_model()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fitting model on Training data"},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing Performance of the model:"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,(acc,loss)=plt.subplots(2,1)\n\n\nacc.set_title('Accuracy vs Epochs')\nacc.plot(np.arange(1,len(history.history['accuracy'])+1),history.history['accuracy'],label='Training Accuracy')\nacc.plot(np.arange(1,len(history.history['val_accuracy'])+1),history.history['val_accuracy'],label='Validation Accuracy')\nacc.set_xlabel('Epochs')\nacc.set_ylabel('Accuracy')\nacc.set_xticks(np.arange(1,len(history.history['accuracy'])+1))\nacc.legend(loc='best')\n\nloss.set_title('Loss vs Epochs')\nloss.plot(np.arange(1,len(history.history['loss'])+1),history.history['loss'],label='Training loss')\nloss.plot(np.arange(1,len(history.history['val_loss'])+1),history.history['val_loss'],label='Validation loss')\nloss.set_xlabel('Epochs')\nloss.set_ylabel('Loss')\nloss.set_xticks(np.arange(1,len(history.history['loss'])+1))\nloss.legend(loc='best')\n\nfig.set_figheight(20)\nfig.set_figwidth(20)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fitting Model on Total data"},{"metadata":{"trusted":true},"cell_type":"code","source":"generator=aug_data.flow(data,labels,batch_size=64)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=create_model()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history=model.fit_generator(generator,epochs=50,validation_data=(X_val,y_val),steps_per_epoch=data.shape[0]//64)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model Performance"},{"metadata":{"trusted":true},"cell_type":"code","source":"fig,(acc,loss)=plt.subplots(2,1)\n\n\nacc.set_title('Accuracy vs Epochs')\nacc.plot(np.arange(1,len(history.history['accuracy'])+1),history.history['accuracy'],label='Training Accuracy')\nacc.plot(np.arange(1,len(history.history['val_accuracy'])+1),history.history['val_accuracy'],label='Validation Accuracy')\nacc.set_xlabel('Epochs')\nacc.set_ylabel('Accuracy')\nacc.set_xticks(np.arange(1,len(history.history['accuracy'])+1))\nacc.legend(loc='best')\n\nloss.set_title('Loss vs Epochs')\nloss.plot(np.arange(1,len(history.history['loss'])+1),history.history['loss'],label='Training loss')\nloss.plot(np.arange(1,len(history.history['val_loss'])+1),history.history['val_loss'],label='Validation loss')\nloss.set_xlabel('Epochs')\nloss.set_ylabel('Loss')\nloss.set_xticks(np.arange(1,len(history.history['loss'])+1))\nloss.legend(loc='best')\n\nfig.set_figheight(20)\nfig.set_figwidth(20)\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Generating predictions for Test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"x_test=test.values.reshape(-1,28,28,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred=np.argmax(model.predict(x=x_test),1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"output=np.concatenate((np.arange(1,test.shape[0]+1).reshape(-1,1),pred.reshape(-1,1)),axis=1)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Creating Submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission=pd.DataFrame(output,columns=['ImageId','Label'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('Submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import FileLink","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('model.h5')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Pre-Trained Model Link"},{"metadata":{"trusted":true},"cell_type":"code","source":"FileLink('model.h5')","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}