{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport sklearn.model_selection\nimport 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\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size = 100\nnum_class = 10\nepochs = 45\n\nimg_rows = 28\nimg_cols = 28\n\ninput_shape = (img_rows, img_cols, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train[\"label\"]\nx = train.drop([\"label\"], axis = 1)\n\nx_train, x_val, y_train, y_val = sklearn.model_selection.train_test_split(x.values, y.values, test_size = 0.10)\n\nprint(y_train.shape)\nprint(x_train.shape)\nprint(y_val.shape)\nprint(x_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(x_train.shape[0])\nx_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)\nx_train = x_train.astype('float32')\nx_train /= 255\n\nprint(x_val.shape[0])\nx_val = x_val.reshape(x_val.shape[0], img_rows, img_cols, 1)\nx_val = x_val.astype('float32')\nx_val /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train, num_class)\ny_val = tf.keras.utils.to_categorical(y_val, num_class)\n\nk_init = tf.initializers.TruncatedNormal(mean = 0.1, stddev = 0.05)\nb_init = tf.initializers.constant(value = 1e-4)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=15,\n    zoom_range = 0.20,\n    width_shift_range=0.20,\n    height_shift_range=0.20\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, kernel_size = (3, 3), activation='relu', input_shape = input_shape),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(32, kernel_size = (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(32, kernel_size = (3, 3), activation='relu', strides=2, padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.4),\n    \n    tf.keras.layers.Conv2D(64, kernel_size = (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(64, kernel_size = (3, 3), activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2D(64, kernel_size = (3, 3), activation='relu', strides=2, padding='same'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.4),\n    \n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(128, activation='relu'),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Dense(num_class, activation='softmax')\n])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.002,\n                    rho=0.9,\n                    momentum=0.1,\n                    epsilon=1e-07,\n                    centered=True,\n                    name='RMSprop')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='loss',\n                                          factor=0.25,\n                                          patience=2,\n                                          verbose=1,\n                                          mode=\"auto\",\n                                          min_delta=0.0001,\n                                          cooldown=0,\n                                          min_lr=0.00001)\nes = tf.keras.callbacks.EarlyStopping(monitor='loss', \n                   mode='min', verbose=1,\n                   patience=300,\n                   restore_best_weights=False )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nmodel.compile(loss= \"categorical_crossentropy\",  optimizer = optimizer, metrics=['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(datagen.flow(x_train, y_train, batch_size = batch_size),\n                              epochs = epochs,\n                              steps_per_epoch = 100,\n                              validation_data = (x_val, y_val),\n                              validation_steps=50,\n                              callbacks=[lr, es],\n                              verbose=2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scores = model.evaluate(x_val, y_val)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test=pd.read_csv('../input/Kannada-MNIST/test.csv')\n\ntest_id=test.id\n\ntest=test.drop('id',axis=1)\ntest=test/255\ntest=test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pre=model.predict(test)     ##making prediction\ny_pre=np.argmax(y_pre,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_sub=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\n\nsample_sub['label']=y_pre\nsample_sub.to_csv('submission.csv',index=False)\nsample_sub.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.chdir(\"..\")\nos.listdir()","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":1}