{"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 5GB 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\nimport numpy as np\nimport tensorflow as tf\nimport time\n\nimport matplotlib.pyplot as plt\n\nimport keras\nfrom keras.layers import Activation, Input, Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers.normalization import BatchNormalization\nimport pandas as pd\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def deep_cnn_model(input_shape):\n    model = Sequential()\n\n    model.add(Conv2D(32,kernel_size=3,activation='relu',input_shape=input_shape))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32,kernel_size=3,activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(32,kernel_size=5,strides=2,padding='same',activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Conv2D(64,kernel_size=3,activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64,kernel_size=3,activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Conv2D(64,kernel_size=5,strides=2,padding='same',activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(0.4))\n    model.add(Dense(10, activation='softmax'))\n\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n    return model\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_f = \"/kaggle/input/Kannada-MNIST/\"\ntrain = np.genfromtxt(os.path.join(data_f,\"train.csv\"), delimiter=',')\ntest = np.genfromtxt(os.path.join(data_f,\"test.csv\"), delimiter=',')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = train[1:,1:]\ny_train = train[1:,0]\n\n\n# x_val = val[:,1:]\n# y_val = val[:,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train = x_train.reshape((-1,28,28,1))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model = deep_cnn_model((28,28,1))\ncnn_model.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn_model.fit(x_train, to_categorical(y_train), epochs=5, batch_size=128)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_, accuracy = cnn_model.evaluate(x_train, to_categorical(y_train))\nprint('Train Accuracy: %.2f' % (accuracy*100))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = test[1:,1:]\ntest = test.reshape((-1,28,28,1))\npreds_test = np.argmax(cnn_model.predict(test),axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save test predictions to file\noutput = pd.DataFrame({'id': list(range(test.shape[0])),\n                       'label': preds_test})\noutput.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}