{"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\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 read-only \"../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# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.utils import *\nfrom tensorflow.keras.layers import *\nfrom tensorflow.keras.models import *\nfrom tensorflow.keras.optimizers import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_data = np.genfromtxt('../input/Kannada-MNIST/train.csv', delimiter=',', skip_header=1, usecols=np.arange(1, 785))\n\n#create train picture data\nslices_size =int((len(x_data)*0.85))\nx_train = x_data[:slices_size]\nx_train = x_train.reshape(slices_size, 28, 28)\n\n#create test picture data\nslices_size_test = len(x_data) - slices_size\nx_test = x_data[slices_size:]\nx_test = x_test.reshape(slices_size_test, 28, 28)\n\n#create train labels\ny_data = np.genfromtxt('../input/Kannada-MNIST/train.csv', delimiter=',', skip_header=1, usecols=0)\ny_train = y_data[:slices_size]\ny_test = y_data[slices_size:]\n\n\ntrain_size, test_size = x_train.shape[0], x_test.shape[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numbers_df = pd.read_csv('../input/Kannada-MNIST/train.csv')\nnumbers_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"numbers_x_df = numbers_df.drop(labels = ['label'], axis = 1)\nnumbers_x_df = numbers_x_df.values.reshape(-1, 28, 28, 1)\nnumbers_x_df = numbers_x_df / 255.0\nplt.imshow(numbers_x_df[0][:,:,0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\nnumbers_y_df = numbers_df['label']\nsns.countplot(numbers_y_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Settings \nwidth = 28\nheight = 28\ndepth = 1\nnum_classes = 10\nepochs = 7\n\nx_train = x_train.reshape(train_size, width, height, depth)\nx_test = x_test.reshape(test_size, width, height, depth)\ny_train = to_categorical(y_train, num_classes=num_classes)\ny_test = to_categorical(y_test, num_classes=num_classes)\n\n# Define the DNN\ninput_img = Input(shape=(width, height, depth))\n\nx = Conv2D(filters=16, kernel_size=3, padding='same')(input_img)\nx = Activation(\"relu\")(x)\nx = Conv2D(filters=32, kernel_size=3, padding='same')(x)\nx = Activation(\"relu\")(x)\nx = MaxPool2D()(x)\n\nx = Conv2D(filters=32, kernel_size=3, padding='same')(x)\nx = Activation(\"relu\")(x)\nx = Conv2D(filters=64, kernel_size=3, padding='same')(x)\nx = Activation(\"relu\")(x)\nx = MaxPool2D()(x)\n\nx = Flatten()(x)\n\nx = Dense(64)(x)\nx = Activation(\"relu\")(x)\nx = Dense(num_classes)(x)\noutput_pred = Activation(\"softmax\")(x)\n\nmodel = Model(inputs=[input_img], outputs=[output_pred])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Train the DNN\nlr = 5e-4\noptimizer = RMSprop(lr=lr)\n\nmodel.compile(\n    loss=\"categorical_crossentropy\",\n    optimizer=optimizer,\n    metrics=[\"accuracy\"])\n\nmodel.fit(\n    x=x_train,\n    y=y_train,\n    verbose=1,\n    batch_size=64,\n    epochs=epochs,\n    validation_data=[x_test, y_test])\n\n# Test the DNN\nscore = model.evaluate(x_test, y_test, verbose=0)\nprint(\"Score: \", score)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_sub = np.genfromtxt('../input/Kannada-MNIST/test.csv', delimiter=',', skip_header=1, usecols=np.arange(1, 785))\nx_sub = x_sub.reshape(len(x_sub), 28, 28)\npredict = model.predict(x_sub)\npredict = np.argmax(predict, axis=1)\n\n\n# create submission file\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = predict\nsubmission.to_csv('submission.csv', index=False)\n\nsubmission\n","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}