{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt # data visualization\nimport seaborn as sns # data visualization\n\nimport tensorflow as tf\nimport numpy as np\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, BatchNormalization, Flatten, GlobalMaxPooling2D, LeakyReLU\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.regularizers import l2\nimport pandas as pd\nfrom tensorflow.keras.optimizers import Adam, SGD\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport os\nfor dirname, _, filenames in os.walk('../kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ndf_test = pd.read_csv('../input/Kannada-MNIST/Dig-MNIST.csv')\ny_train = df_train.iloc[:,0].values\ny_test = df_test.iloc[:,0].values\ndf_train = df_train.drop(['label'], axis=1)\ndf_test = df_test.drop(['label'], axis=1)\nN_train = len(y_train)\nN_test = len(y_test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = np.uint8(df_train.values.reshape(-1,28,28))\nx_test = np.uint8(df_test.values.reshape(-1,28,28))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocessing(img):\n  kernel = np.array([[0,-1.5,0],[-1.5,8.5,-1.5],[0,-1.5,0]])\n  img = cv2.filter2D(img,-1,kernel)\n  img_thresh = img/255.0 *2 -1\n  return img_thresh\nx_train = np.array(list(map(preprocessing,x_train)))\nx_test = np.array(list(map(preprocessing, x_test)))\nx_train= x_train.reshape(-1,28,28,1)\nx_test= x_test.reshape(-1,28,28,1)\ny_train = tf.keras.utils.to_categorical(y_train,10)\ny_test = tf.keras.utils.to_categorical(y_test,10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, random_state=0, test_size=0.15)\nX_train.shape, X_val.shape, Y_train.shape, Y_val.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nfrom tensorflow.keras.layers import Add\n\ni = Input(shape=(X_train[0].shape))\n\nx1 = Conv2D(32, (3,3), padding=\"same\")(i)\nx1 = LeakyReLU(0.2)(x1)\nx1 = BatchNormalization()(x1)\nx1 = Conv2D(32, (3,3), padding=\"same\")(x1)\nx1 = LeakyReLU(0.2)(x1)\nx1 = BatchNormalization()(x1)\nx1 = Conv2D(64, (3,3), padding=\"same\")(x1)\nx1 = LeakyReLU(0.2)(x1)\nb1 = BatchNormalization()(x1)\n\nx2 = Conv2D(64, (3,3), padding=\"same\")(b1)\nx2 = LeakyReLU(0.2)(x2)\nx2 = BatchNormalization()(x2)\nx2 = Conv2D(64, (5,5), padding=\"same\")(x2)\nx2 = LeakyReLU(0.2)(x2)\nx2 = BatchNormalization()(x2)\n\nx3 = Add()([b1,x2])\nx3 = MaxPooling2D()(x3)\nx3 = Dropout(0.2)(x3)\n\nx3 = Conv2D(128, (3,3), padding=\"same\")(x3)\nx3 = LeakyReLU(0.1)(x3)\nx3 = BatchNormalization()(x3)\nx3 = Conv2D(128, (3,3), padding=\"same\")(x3)\nx3 = LeakyReLU(0.1)(x3)\nx3 = Conv2D(256, (3,3), padding=\"same\")(x3)\nx3 = LeakyReLU(0.1)(x3)\nb2 = BatchNormalization()(x3)\n\nx3 = Conv2D(256, (5,5), padding=\"same\")(b2)\nx3 = LeakyReLU(0.1)(x3)\nx3 = BatchNormalization()(x3)\n\nx = Add()([b2,x3])\n\nx = MaxPooling2D()(x)\nx = Conv2D(256, (3,3), padding=\"same\")(x)\nx = LeakyReLU(0.1)(x)\nx = GlobalMaxPooling2D()(x)\n\nx = Flatten()(x)\nx = Dense(128, activation='relu', kernel_regularizer=l2(0.01))(x)\nx = Dropout(0.2)(x)\nx = Dense(128, activation='relu', kernel_regularizer=l2(0.01))(x)\nx = Dropout(0.2)(x)\n\nx = Dense(10, kernel_regularizer=l2(0.01) )(x)\n\nmodel = Model(i,x)\n#model.summary()\nplot_model(model, \"model.png\", show_shapes=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=Adam(lr=0.001, beta_1=0.9), \n              loss='squared_hinge',\n              metrics=['accuracy'])\ndataGen = ImageDataGenerator(rotation_range=15,width_shift_range=0.2, height_shift_range=0.2,zoom_range=0.15)\ndataGen.fit(X_train)\nbatch_size=32\nreduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.3,\n                              patience=3, min_lr=1e-10)    \nr = model.fit_generator(dataGen.flow(X_train, Y_train, batch_size=32),\n                        steps_per_epoch=X_train.shape[0]//batch_size, epochs=50, validation_data=(X_val,Y_val), verbose=2,callbacks=[reduce_lr])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(r.history['loss'], label = 'loss')\nplt.plot(r.history['val_loss'], label = 'val_loss')\nplt.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate(x_test, y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_final = pd.read_csv('../input/Kannada-MNIST/test.csv')\ndf_final = df_final.drop(['id'], axis=1)\nX_final = np.uint8(df_final.values.reshape(-1,28,28))\nX_final = np.array(list(map(preprocessing,X_final)))\nX_final= X_final.reshape(-1,28,28,1)\npred = model.predict(X_final).argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsub['label'] = pred\nsub['label']= sub['label'].astype(int)\nsub.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":4}