{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"root_path = '/kaggle/input/Kannada-MNIST/'\noutput_path = '/kaggle/working/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.read_csv(root_path + 'train.csv')\ndf.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_data = df.values[:, 1:]\ny_data = df.label.values\n\nX_data.shape, y_data.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df2 = pd.read_csv(root_path + 'Dig-MNIST.csv')\ndf2.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_X = df2.values[:, 1:]\ntest_y = df2.label.values\n\ntest_X.shape, test_y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = X_data.shape[0]\nperm = np.random.permutation(size)\n\nX_data = X_data[perm]\ny_data = y_data[perm]\n\ntrain_size = int(size / 10 * 7.5)\ntrain_X, valid_X = X_data[:train_size], X_data[train_size:]\ntrain_y, valid_y = y_data[:train_size], y_data[train_size:]\n\ntrain_X.shape, train_y.shape, test_X.shape, test_y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_X = train_X.reshape(train_X.shape[0], 28, 28, 1)\nvalid_X = valid_X.reshape(valid_X.shape[0], 28, 28, 1)\ntest_X = test_X.reshape(test_X.shape[0], 28, 28, 1)\n\ntrain_X = np.pad(train_X, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')\nvalid_X = np.pad(valid_X, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')\ntest_X = np.pad(test_X, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')\n\nmean_px = train_X.mean().astype(float)\nstd_px = train_X.std().astype(float)\ntrain_X = (train_X - mean_px) / (std_px)\nvalid_X = (valid_X - mean_px) / (std_px)\ntest_X = (test_X - mean_px) / (std_px)\n\ntrain_X.shape, valid_X.shape, test_X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\n\ntrain_y = to_categorical(train_y)\nvalid_y = to_categorical(valid_y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nfrom keras.layers import *\nfrom keras.models import Model\nfrom keras.callbacks import CSVLogger, ModelCheckpoint","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = keras.Sequential()\n\nmodel.add(Conv2D(filters = 6, kernel_size = 5, strides = 1, activation = 'tanh', input_shape = (32,32,1)))\nmodel.add(AveragePooling2D(pool_size = 2, strides = 2))\nmodel.add(Conv2D(filters = 16, kernel_size = 5, strides = 1, activation = 'tanh', input_shape = (14,14,6)))\nmodel.add(AveragePooling2D(pool_size = 2, strides = 2))\nmodel.add(Conv2D(filters = 120, kernel_size = 5, strides = 1, activation = 'tanh', input_shape = (5, 5, 16)))\nmodel.add(Flatten())\nmodel.add(Dense(units = 84, activation = 'tanh'))\nmodel.add(Dense(units = 10, activation = 'softmax'))\n\nmodel.compile(optimizer ='sgd', loss = 'categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(train_X, train_y,\n          batch_size=16,\n          epochs=30,\n          verbose=1,\n          validation_data=(valid_X, valid_y),\n          callbacks=[\n              CSVLogger(output_path + 'log.csv'),\n              ModelCheckpoint(output_path + 'model.h5', save_best_only=True),\n          ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nmodel = keras.models.load_model(output_path + 'model.h5')\npred_probas = model.predict(test_X, batch_size=16)\nprediction = pred_probas.argmax(axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(test_y, prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df3 = pd.read_csv(root_path + 'test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = df3.id.values\nres_X = df3.values[:, 1:]\n\nres_X.shape, ids.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"res_X = res_X.reshape(res_X.shape[0], 28, 28, 1)\nres_X = np.pad(res_X, ((0, 0), (2, 2), (2, 2), (0, 0)), 'constant')\nres_X = (res_X - mean_px) / (std_px)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_probas = model.predict(res_X, batch_size=16)\nprediction = pred_probas.argmax(axis=1)\n\nprediction[:10], prediction.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv(root_path + 'sample_submission.csv')\nsubmission['label'] = prediction\n\nsubmission.head()","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":"","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}