{"cells":[{"metadata":{"id":"7HDksY_aztfq","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\n\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.utils.np_utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.callbacks import ReduceLROnPlateau","execution_count":null,"outputs":[]},{"metadata":{"id":"weUu93o_20rd","outputId":"1641cf45-2ca4-4177-e82e-6f0a60bd2a5e","trusted":false},"cell_type":"code","source":"# from google.colab import drive\n# drive.mount('/content/drive')","execution_count":null,"outputs":[]},{"metadata":{"id":"Nmci3gkD21qx","trusted":true},"cell_type":"code","source":"train  = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"id":"BJCDzI4l4teQ","outputId":"66cb6aa3-ab96-4324-d600-0b866244a8ee","trusted":true},"cell_type":"code","source":"train.head(4)","execution_count":null,"outputs":[]},{"metadata":{"id":"NmTYgQtD4voh","outputId":"2acc6adf-be9c-441b-82f2-6d690f169557","trusted":true},"cell_type":"code","source":"train.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"id":"rUYC5Iki42Je","outputId":"34461bf8-0d87-4729-e512-6451d8d531b1","trusted":true},"cell_type":"code","source":"sns.countplot(train['label'])","execution_count":null,"outputs":[]},{"metadata":{"id":"I03MpbUK5C5L","trusted":true},"cell_type":"code","source":"X_train = train.drop(['label'],axis=1)\nY_train = train['label']","execution_count":null,"outputs":[]},{"metadata":{"id":"AXMo4rQh5cQ5","trusted":true},"cell_type":"code","source":"#Reshaping data\n","execution_count":null,"outputs":[]},{"metadata":{"id":"sYDZNpy25eYx","trusted":true},"cell_type":"code","source":"X_train = X_train/255.0","execution_count":null,"outputs":[]},{"metadata":{"id":"oerWXUPf5oTB","outputId":"63e41477-bc7e-4fb3-ddf5-abd3c2877fa8","trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"qF5Tk82p5pSy","trusted":true},"cell_type":"code","source":"testids = test['id']","execution_count":null,"outputs":[]},{"metadata":{"id":"74dT0HDH5sp9","trusted":true},"cell_type":"code","source":"test.drop(['id'],axis=1,inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"id":"1slJvHSO5xA4","trusted":true},"cell_type":"code","source":"test = test/255.0","execution_count":null,"outputs":[]},{"metadata":{"id":"reDM2m9U5zdf","trusted":true},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"id":"nPDsMGMw6A4z","trusted":true},"cell_type":"code","source":"Y_train = to_categorical(Y_train,num_classes=10)","execution_count":null,"outputs":[]},{"metadata":{"id":"vBWja3wC6OMS","trusted":true},"cell_type":"code","source":"X_train,X_test,Y_train,Y_test = train_test_split(X_train,Y_train,random_state=2,test_size=0.1)","execution_count":null,"outputs":[]},{"metadata":{"id":"ELI3jylW65v_","outputId":"d055c532-7e09-439d-b039-bef0d82ca3d7","trusted":true},"cell_type":"code","source":"g = plt.imshow(X_train[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"id":"Hvt5J9Hu7Fxu","trusted":true},"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=32,kernel_size=(5,5),padding='Same',activation='relu',input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=32,kernel_size=(5,5),padding='Same',activation='relu'))\n\nmodel.add(BatchNormalization(momentum=.15))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),padding='Same',activation='relu',))\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),padding='Same',activation='relu',))\n\nmodel.add(BatchNormalization(momentum=.15))\n\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Dropout(0.5))\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"id":"DSOVzIrW7MJX","trusted":true},"cell_type":"code","source":"\noptimizer = RMSprop(lr=0.001,rho=0.9,epsilon=1e-08,decay=0.0)","execution_count":null,"outputs":[]},{"metadata":{"id":"f9bTVbDb7ast","trusted":true},"cell_type":"code","source":"model.compile(optimizer=optimizer,loss='categorical_crossentropy',metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"id":"K48J8Mhn8Brr","trusted":true},"cell_type":"code","source":"#Reducing the learning rate","execution_count":null,"outputs":[]},{"metadata":{"id":"YXaioHLN8Pl0","trusted":true},"cell_type":"code","source":"learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',patience=3,verbose=1,factor=0.5,min_lir=0.00001)","execution_count":null,"outputs":[]},{"metadata":{"id":"DYQy2ABB8gB-","trusted":true},"cell_type":"code","source":"epochs=30\nbatch_size=86","execution_count":null,"outputs":[]},{"metadata":{"id":"THRsJ1fl8j6_","trusted":true},"cell_type":"code","source":"# Data Augmentation","execution_count":null,"outputs":[]},{"metadata":{"id":"1Dfd4Fvv8wRR","trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(featurewise_center=False,\n                             samplewise_center=False,\n                             featurewise_std_normalization=False,\n                             samplewise_std_normalization=False,\n                             zca_whitening=False,\n                             rotation_range=0,\n                             zoom_range=0.1,\n                             width_shift_range=0.1,\n                             height_shift_range=0.1)","execution_count":null,"outputs":[]},{"metadata":{"id":"wC3v8-429NQ-","trusted":true},"cell_type":"code","source":"datagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"id":"pPCtySms9TTU","outputId":"b1bedc98-a03c-4da3-ce10-d85a76989eed","trusted":true},"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,Y_train,batch_size=batch_size),\n                              epochs=epochs,\n                              validation_data=(X_test,Y_test),\n                              verbose=2,\n                              steps_per_epoch=X_train.shape[0]//batch_size,\n                              callbacks=[learning_rate_reduction]\n                              )","execution_count":null,"outputs":[]},{"metadata":{"id":"ZNhXBBNJttb3","trusted":true},"cell_type":"code","source":"test = pd.read_csv('../input/Kannada-MNIST/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_ids = test['id']","execution_count":null,"outputs":[]},{"metadata":{"id":"noriNrswuAXd","trusted":true},"cell_type":"code","source":"test = test.drop(['id'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"VHV-ZmTcuE2y","trusted":true},"cell_type":"code","source":"test = test/255.0","execution_count":null,"outputs":[]},{"metadata":{"id":"9JlZ0dTvuHTm","trusted":true},"cell_type":"code","source":"test=test.values.reshape(test.shape[0],28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"id":"7UBwHrEzuMmI","outputId":"2d577825-5c72-4663-f4c6-50bc2fdd8138","trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"id":"gK7x2bxauRdE","trusted":true},"cell_type":"code","source":"\ny_pre = model.predict(test)","execution_count":null,"outputs":[]},{"metadata":{"id":"d6ixBiAzuqSz","trusted":true},"cell_type":"code","source":"y_pre = np.argmax(y_pre,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"marAW2BquvrD","trusted":true},"cell_type":"code","source":"sample_sub = pd.DataFrame(y_pre)","execution_count":null,"outputs":[]},{"metadata":{"id":"MJMUf-2FuzU2","outputId":"3f340987-e38b-47b2-c54b-bf6dbcbca68e","trusted":true},"cell_type":"code","source":"sample_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"jVVTLqs2vPG2","trusted":true},"cell_type":"code","source":"sub1 = sample_sub","execution_count":null,"outputs":[]},{"metadata":{"id":"5Mh9AzXavTEC","trusted":true},"cell_type":"code","source":"sub1 = pd.concat([test_ids,sub1],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"id":"Y6xufnyDvgAB","outputId":"58efce6d-308b-4b5b-b7a5-0c701e60890a","trusted":true},"cell_type":"code","source":"sub1.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"Eyfu4wI4vhN3","trusted":true},"cell_type":"code","source":"sub1 = sub1.rename(columns={0:'label'})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub1.head()","execution_count":null,"outputs":[]},{"metadata":{"id":"2WolvewbwClg","trusted":true},"cell_type":"code","source":"sub1.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]},{"metadata":{"id":"YgnzLy4rwoc9","trusted":false},"cell_type":"code","source":"","execution_count":0,"outputs":[]}],"metadata":{"colab":{"name":"Kannada.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"}},"nbformat":4,"nbformat_minor":4}