{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","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 sklearn.metrics import classification_report\nfrom IPython.display import FileLink","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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train=pd.read_csv(r'../input/Kannada-MNIST/train.csv')\ntest=pd.read_csv(r'../input/Kannada-MNIST/test.csv')\nval=pd.read_csv(r'../input/Kannada-MNIST/Dig-MNIST.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Train Set Shape:',train.shape)\nprint('Test Set Shape:',test.shape)\nprint('Validation Set Shape:',val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data=train.iloc[:,1:].values\ndata=data.reshape(-1,28,28,1)/255\nprint('Total training data shape:',data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels=pd.get_dummies(train.iloc[:,0]).values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_val=val.iloc[:,1:].values\nx_val=x_val.reshape(-1,28,28,1)/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_val=pd.get_dummies(val.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":"# 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(data,labels,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(64,(3,3),input_shape=(28,28,1),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,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(128,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(128,(5,5),strides=(2,2),padding='same',activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(256,(3,3),activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Flatten())\n    model.add(Dense(1024,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,shuffle=True)","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 Training+Validation data"},{"metadata":{"trusted":true},"cell_type":"code","source":"data=np.concatenate((data,x_val))\nlabels=np.concatenate((labels,y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print('Data shape:',data.shape)","execution_count":null,"outputs":[]},{"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,shuffle=True)","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='Total 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='Total 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":{"trusted":true},"cell_type":"code","source":"print(classification_report(np.argmax(y_val,1),np.argmax(model.predict(x_val),1)))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Generating predictions for Test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"test_id=test['id'].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.drop('id',axis=1,inplace=True)","execution_count":null,"outputs":[]},{"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":{},"cell_type":"markdown","source":"# Creating Submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"def submit(test_id,pred):\n    df=np.concatenate((test_id.reshape(-1,1),pred.reshape(-1,1)),axis=1)\n    df=pd.DataFrame(df,columns=['id','label'])\n    df['id']=df['id'].astype('int32')\n    df.to_csv('Submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit(test_id,pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"FileLink('Submission.csv')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.7.3"}},"nbformat":4,"nbformat_minor":1}