{"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_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-11T05:57:15.289018Z","iopub.execute_input":"2021-10-11T05:57:15.289564Z","iopub.status.idle":"2021-10-11T05:57:15.303176Z","shell.execute_reply.started":"2021-10-11T05:57:15.289519Z","shell.execute_reply":"2021-10-11T05:57:15.301852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-10-11T05:57:15.305243Z","iopub.execute_input":"2021-10-11T05:57:15.305624Z","iopub.status.idle":"2021-10-11T05:57:18.270856Z","shell.execute_reply.started":"2021-10-11T05:57:15.305586Z","shell.execute_reply":"2021-10-11T05:57:18.270061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Training set:', train.shape)\nprint('Test set: ', test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:18.272233Z","iopub.execute_input":"2021-10-11T05:57:18.272681Z","iopub.status.idle":"2021-10-11T05:57:18.278881Z","shell.execute_reply.started":"2021-10-11T05:57:18.272635Z","shell.execute_reply":"2021-10-11T05:57:18.277732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train\nX_test = test\nY_train = X_train.pop('label')\nX_test.pop('id')","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:18.280607Z","iopub.execute_input":"2021-10-11T05:57:18.281239Z","iopub.status.idle":"2021-10-11T05:57:18.409711Z","shell.execute_reply.started":"2021-10-11T05:57:18.281200Z","shell.execute_reply":"2021-10-11T05:57:18.408777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''Preprocess'''\nimport numpy as np\nX_train, X_test = np.array(X_train)/255.0, np.array(X_test)/255.0\n","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:18.413267Z","iopub.execute_input":"2021-10-11T05:57:18.413649Z","iopub.status.idle":"2021-10-11T05:57:18.716663Z","shell.execute_reply.started":"2021-10-11T05:57:18.413610Z","shell.execute_reply":"2021-10-11T05:57:18.715673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 28, 28,1)\nX_test = X_test.reshape(X_test.shape[0], 28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:18.719167Z","iopub.execute_input":"2021-10-11T05:57:18.719535Z","iopub.status.idle":"2021-10-11T05:57:18.724528Z","shell.execute_reply.started":"2021-10-11T05:57:18.719496Z","shell.execute_reply":"2021-10-11T05:57:18.723446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.utils import shuffle\nX_train, Y_train = shuffle(X_train, Y_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:18.725989Z","iopub.execute_input":"2021-10-11T05:57:18.726341Z","iopub.status.idle":"2021-10-11T05:57:19.267811Z","shell.execute_reply.started":"2021-10-11T05:57:18.726305Z","shell.execute_reply":"2021-10-11T05:57:19.266812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create validation set\nfrom sklearn.model_selection import train_test_split\nX_train, X_valid, Y_train, Y_valid = train_test_split(X_train, Y_train, test_size=0.2, shuffle=True)\nprint('Training set:', X_train.shape, Y_train.shape)\nprint('Validation set:', X_valid.shape, Y_valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.269216Z","iopub.execute_input":"2021-10-11T05:57:19.269593Z","iopub.status.idle":"2021-10-11T05:57:19.397320Z","shell.execute_reply.started":"2021-10-11T05:57:19.269553Z","shell.execute_reply":"2021-10-11T05:57:19.396582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Flatten, Dropout\nfrom tensorflow.keras.layers import Dense, BatchNormalization, Activation, LeakyReLU","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.398977Z","iopub.execute_input":"2021-10-11T05:57:19.399340Z","iopub.status.idle":"2021-10-11T05:57:19.405606Z","shell.execute_reply.started":"2021-10-11T05:57:19.399301Z","shell.execute_reply":"2021-10-11T05:57:19.404347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.407080Z","iopub.execute_input":"2021-10-11T05:57:19.407480Z","iopub.status.idle":"2021-10-11T05:57:19.421727Z","shell.execute_reply.started":"2021-10-11T05:57:19.407441Z","shell.execute_reply":"2021-10-11T05:57:19.420935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Conv2D(filters=32, kernel_size=[3,3],input_shape=[28,28,1], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.422897Z","iopub.execute_input":"2021-10-11T05:57:19.423264Z","iopub.status.idle":"2021-10-11T05:57:19.459715Z","shell.execute_reply.started":"2021-10-11T05:57:19.423227Z","shell.execute_reply":"2021-10-11T05:57:19.458975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(filters=32, kernel_size=[3,3], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=64, kernel_size=[3,3], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))\nmodel.add(Conv2D(filters=64, kernel_size=[3,3], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=256, kernel_size=[3,3], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))\nmodel.add(Conv2D(filters=256, kernel_size=[3,3], padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(LeakyReLU(0.05))\nmodel.add(MaxPooling2D(pool_size=(2,2)))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.460860Z","iopub.execute_input":"2021-10-11T05:57:19.461218Z","iopub.status.idle":"2021-10-11T05:57:19.616065Z","shell.execute_reply.started":"2021-10-11T05:57:19.461182Z","shell.execute_reply":"2021-10-11T05:57:19.615419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(64, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.617151Z","iopub.execute_input":"2021-10-11T05:57:19.617472Z","iopub.status.idle":"2021-10-11T05:57:19.667544Z","shell.execute_reply.started":"2021-10-11T05:57:19.617434Z","shell.execute_reply":"2021-10-11T05:57:19.666933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.669698Z","iopub.execute_input":"2021-10-11T05:57:19.670195Z","iopub.status.idle":"2021-10-11T05:57:19.685363Z","shell.execute_reply.started":"2021-10-11T05:57:19.670154Z","shell.execute_reply":"2021-10-11T05:57:19.684715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.688906Z","iopub.execute_input":"2021-10-11T05:57:19.689154Z","iopub.status.idle":"2021-10-11T05:57:19.704055Z","shell.execute_reply.started":"2021-10-11T05:57:19.689130Z","shell.execute_reply":"2021-10-11T05:57:19.702595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.705144Z","iopub.execute_input":"2021-10-11T05:57:19.705466Z","iopub.status.idle":"2021-10-11T05:57:19.710288Z","shell.execute_reply.started":"2021-10-11T05:57:19.705432Z","shell.execute_reply":"2021-10-11T05:57:19.709252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator (rotation_range = 10,\nwidth_shift_range = 0.25,\nheight_shift_range = 0.25,\nshear_range = 0.1,\nzoom_range = 0.25,\nhorizontal_flip = False)","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.711343Z","iopub.execute_input":"2021-10-11T05:57:19.711812Z","iopub.status.idle":"2021-10-11T05:57:19.719419Z","shell.execute_reply.started":"2021-10-11T05:57:19.711776Z","shell.execute_reply":"2021-10-11T05:57:19.718391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size =  32\nmodel.fit(datagen.flow(X_train, Y_train), batch_size=batch_size, epochs=20, validation_data=(X_valid, Y_valid))","metadata":{"execution":{"iopub.status.busy":"2021-10-11T05:57:19.721118Z","iopub.execute_input":"2021-10-11T05:57:19.721603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head(5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\nid = test['id']","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(X_test)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = np.argmax(predict, axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':id,'label':predict})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filename = 'submission.csv'\nsubmission.to_csv(filename, index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head(5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}