{"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":"import time\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import accuracy_score\nimport tensorflow as tf\nfrom tensorflow.keras import datasets, layers, models\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom keras.utils.np_utils import to_categorical\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:12.465114Z","iopub.execute_input":"2023-07-30T02:49:12.465572Z","iopub.status.idle":"2023-07-30T02:49:19.613426Z","shell.execute_reply.started":"2023-07-30T02:49:12.465539Z","shell.execute_reply":"2023-07-30T02:49:19.612017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. Get Data","metadata":{}},{"cell_type":"code","source":"def get_data(data_set, label_column='label'):\n    # data_set:   pd DataFrame\n    data_X = data_set.drop(columns=label_column)\n    data_y = data_set[label_column]\n    return data_X, data_y","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:19.615392Z","iopub.execute_input":"2023-07-30T02:49:19.615708Z","iopub.status.idle":"2023-07-30T02:49:19.620527Z","shell.execute_reply.started":"2023-07-30T02:49:19.615673Z","shell.execute_reply":"2023-07-30T02:49:19.619437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data\ntrain_set = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')\ntest_set = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')\ndig_set = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv')","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:21.075896Z","iopub.execute_input":"2023-07-30T02:49:21.076376Z","iopub.status.idle":"2023-07-30T02:49:25.519558Z","shell.execute_reply.started":"2023-07-30T02:49:21.076343Z","shell.execute_reply":"2023-07-30T02:49:25.518411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, y_train = get_data(train_set, label_column='label')\nX_dig, y_dig = get_data(dig_set, label_column='label')","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:25.521486Z","iopub.execute_input":"2023-07-30T02:49:25.521766Z","iopub.status.idle":"2023-07-30T02:49:25.650395Z","shell.execute_reply.started":"2023-07-30T02:49:25.521737Z","shell.execute_reply":"2023-07-30T02:49:25.649448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape, X_dig.shape, y_dig.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:25.651565Z","iopub.execute_input":"2023-07-30T02:49:25.651950Z","iopub.status.idle":"2023-07-30T02:49:25.659976Z","shell.execute_reply.started":"2023-07-30T02:49:25.651910Z","shell.execute_reply":"2023-07-30T02:49:25.658775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.to_numpy().reshape(-1, 28, 28, 1)\ny_train = y_train.to_numpy().reshape(-1, 1)\nX_dig = X_dig.to_numpy().reshape(-1, 28, 28, 1)\ny_dig = y_dig.to_numpy().reshape(-1, 1)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:25.661124Z","iopub.execute_input":"2023-07-30T02:49:25.661397Z","iopub.status.idle":"2023-07-30T02:49:25.673201Z","shell.execute_reply.started":"2023-07-30T02:49:25.661371Z","shell.execute_reply":"2023-07-30T02:49:25.672382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = test_set.drop(columns='id').to_numpy().reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:26.396168Z","iopub.execute_input":"2023-07-30T02:49:26.396651Z","iopub.status.idle":"2023-07-30T02:49:26.408610Z","shell.execute_reply.started":"2023-07-30T02:49:26.396620Z","shell.execute_reply":"2023-07-30T02:49:26.406961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_cat = to_categorical(y_train)\ny_dig_cat = to_categorical(y_dig)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:27.516929Z","iopub.execute_input":"2023-07-30T02:49:27.517315Z","iopub.status.idle":"2023-07-30T02:49:27.523431Z","shell.execute_reply.started":"2023-07-30T02:49:27.517272Z","shell.execute_reply":"2023-07-30T02:49:27.522420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nclass_names = np.arange(10)\n\nplt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(X_train[i,:,:,0], cmap='gray')\n    # y_train_cat is in one-hot format\n    plt.xlabel(np.where(y_train_cat[i]==1)[0][0])\nplt.show()\n\"\"\";","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:28.323392Z","iopub.execute_input":"2023-07-30T02:49:28.323744Z","iopub.status.idle":"2023-07-30T02:49:28.328695Z","shell.execute_reply.started":"2023-07-30T02:49:28.323713Z","shell.execute_reply":"2023-07-30T02:49:28.327309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Image Generator","metadata":{}},{"cell_type":"code","source":"d_width = 2\nd_height = 2\nzoom_range = [0.8,1.2]\nbrightness_range = [0.5,1.0]\nfill_mode = 'nearest'\ndatagen = ImageDataGenerator(brightness_range=brightness_range, zoom_range=zoom_range,\n                             width_shift_range=[-d_width, d_width],\n                             height_shift_range=[-d_height, d_height], fill_mode=fill_mode )","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:29.291865Z","iopub.execute_input":"2023-07-30T02:49:29.292171Z","iopub.status.idle":"2023-07-30T02:49:29.296857Z","shell.execute_reply.started":"2023-07-30T02:49:29.292140Z","shell.execute_reply":"2023-07-30T02:49:29.296099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nit = datagen.flow(X_train[0:10], batch_size=1)\nplt.figure(figsize=(5, 5))\nfor i in range(9):\n    plt.subplot(330 + 1+ i)\n    batch = it.next()\n    image = batch[0].astype('uint8')\n    # print(image.shape)\n    plt.imshow(image[:,:,0], cmap='gray')\n\"\"\";","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:30.195972Z","iopub.execute_input":"2023-07-30T02:49:30.196346Z","iopub.status.idle":"2023-07-30T02:49:30.200427Z","shell.execute_reply.started":"2023-07-30T02:49:30.196312Z","shell.execute_reply":"2023-07-30T02:49:30.199644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. Build Model","metadata":{}},{"cell_type":"code","source":"# conv layers\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(layers.MaxPooling2D((2, 2)))\n# add dropout\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Dropout(0.5))\n\n# Fully connected layers\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:31.416615Z","iopub.execute_input":"2023-07-30T02:49:31.417215Z","iopub.status.idle":"2023-07-30T02:49:31.597425Z","shell.execute_reply.started":"2023-07-30T02:49:31.417170Z","shell.execute_reply":"2023-07-30T02:49:31.596350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Adam optimizer\nlearning_rate = 0.001\nbeta_1 = 0.9\nbeta_2 = 0.999\noptimizer = Adam(lr=learning_rate, beta_1=beta_1, beta_2=beta_2)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:34.499218Z","iopub.execute_input":"2023-07-30T02:49:34.499600Z","iopub.status.idle":"2023-07-30T02:49:34.504882Z","shell.execute_reply.started":"2023-07-30T02:49:34.499568Z","shell.execute_reply":"2023-07-30T02:49:34.503755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizer,\n              loss=['categorical_crossentropy'],\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:35.538723Z","iopub.execute_input":"2023-07-30T02:49:35.539145Z","iopub.status.idle":"2023-07-30T02:49:35.551741Z","shell.execute_reply.started":"2023-07-30T02:49:35.539115Z","shell.execute_reply":"2023-07-30T02:49:35.551098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\n# reduce learning rate\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=10, min_lr=0.0001)\nhistory = model.fit(datagen.flow(X_train, y_train_cat, batch_size=batch_size), callbacks=[reduce_lr],\n          steps_per_epoch=len(X_train) / batch_size, epochs=10, validation_data=(X_dig, y_dig_cat))","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:49:38.770669Z","iopub.execute_input":"2023-07-30T02:49:38.771195Z","iopub.status.idle":"2023-07-30T02:55:52.439147Z","shell.execute_reply.started":"2023-07-30T02:49:38.771151Z","shell.execute_reply":"2023-07-30T02:55:52.437818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history.history","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nplt.plot(history.history['acc'], label='accuracy')\nplt.plot(history.history['val_acc'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-07-30T01:40:00.102827Z","iopub.execute_input":"2023-07-30T01:40:00.103478Z","iopub.status.idle":"2023-07-30T01:40:00.111255Z","shell.execute_reply.started":"2023-07-30T01:40:00.103420Z","shell.execute_reply":"2023-07-30T01:40:00.109772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start= time.time()\ny_pred = model.predict_classes(test_set)\nend = time.time()\ntest_time = (end - start) / 60\nprint(f'testing time: {test_time}')","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:56:06.441052Z","iopub.execute_input":"2023-07-30T02:56:06.441396Z","iopub.status.idle":"2023-07-30T02:56:07.320934Z","shell.execute_reply.started":"2023-07-30T02:56:06.441365Z","shell.execute_reply":"2023-07-30T02:56:07.320010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_pd = pd.DataFrame({'id': np.arange(len(y_pred)),\n                          'label': y_pred})\ny_pred_pd.to_csv(\"submission.csv\", sep=',', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-30T02:56:54.518524Z","iopub.execute_input":"2023-07-30T02:56:54.519101Z","iopub.status.idle":"2023-07-30T02:56:54.950341Z","shell.execute_reply.started":"2023-07-30T02:56:54.519052Z","shell.execute_reply":"2023-07-30T02:56:54.948395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}