{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport glob\nimport matplotlib.pyplot as plt\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-08T16:30:54.440887Z","iopub.execute_input":"2022-10-08T16:30:54.441251Z","iopub.status.idle":"2022-10-08T16:30:54.446198Z","shell.execute_reply.started":"2022-10-08T16:30:54.441221Z","shell.execute_reply":"2022-10-08T16:30:54.445103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndf = pd.read_csv('../input/datathonindoml-2022/train_labels.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:30:54.575423Z","iopub.execute_input":"2022-10-08T16:30:54.576418Z","iopub.status.idle":"2022-10-08T16:30:54.592653Z","shell.execute_reply.started":"2022-10-08T16:30:54.576380Z","shell.execute_reply":"2022-10-08T16:30:54.591587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:30:54.724318Z","iopub.execute_input":"2022-10-08T16:30:54.725035Z","iopub.status.idle":"2022-10-08T16:30:54.731492Z","shell.execute_reply.started":"2022-10-08T16:30:54.724981Z","shell.execute_reply":"2022-10-08T16:30:54.730532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:30:59.010018Z","iopub.execute_input":"2022-10-08T16:30:59.010925Z","iopub.status.idle":"2022-10-08T16:30:59.015276Z","shell.execute_reply.started":"2022-10-08T16:30:59.010890Z","shell.execute_reply":"2022-10-08T16:30:59.014334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ind in df.index:\n    path = os.path.join(\"./trainclass\",str(df[\"label\"][ind]))\n    if not os.path.exists(path):\n        os.makedirs(path)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:31:32.741716Z","iopub.execute_input":"2022-10-08T16:31:32.742088Z","iopub.status.idle":"2022-10-08T16:31:32.928720Z","shell.execute_reply.started":"2022-10-08T16:31:32.742057Z","shell.execute_reply":"2022-10-08T16:31:32.927469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for ind in df.index:\n    path = os.path.join(\"./trainclass\",str(df[\"label\"][ind]))\n    shutil.copy('../input/datathonindoml-2022/train/train/'+str(df['id'][ind])+'.tif', './trainclass/'+str(df['label'][ind]))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:31:34.789117Z","iopub.execute_input":"2022-10-08T16:31:34.789464Z","iopub.status.idle":"2022-10-08T16:33:08.334176Z","shell.execute_reply.started":"2022-10-08T16:31:34.789434Z","shell.execute_reply":"2022-10-08T16:33:08.333166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:35:07.286466Z","iopub.execute_input":"2022-10-08T16:35:07.286959Z","iopub.status.idle":"2022-10-08T16:35:07.294320Z","shell.execute_reply.started":"2022-10-08T16:35:07.286918Z","shell.execute_reply":"2022-10-08T16:35:07.292996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagenerator = ImageDataGenerator(rescale = 1./255.,rotation_range = 35, width_shift_range = 0.2, height_shift_range = 0.2, shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True,validation_split=0.2)\nvalidation_datagenerator = ImageDataGenerator(rescale = 1./255, )\n\ntrain_data = train_datagenerator.flow_from_directory('./trainclass',\n                                                   target_size = (256,256),\n                                                   batch_size = 128,\n                                                   subset = \"training\",\n                                                   color_mode = \"grayscale\")\n\ntesting_data = train_datagenerator.flow_from_directory('./trainclass',\n                                                      target_size = (256,256),\n                                                      batch_size = 128,\n                                                      subset = \"validation\",\n                                                      color_mode = \"grayscale\")\n\nvalidation_data = validation_datagenerator.flow_from_directory('../input/datathonindoml-2022/validation',\n                                                              target_size = (256,256),\n                                                              batch_size = 128,\n                                                              color_mode = 'grayscale')","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:47:16.203019Z","iopub.execute_input":"2022-10-08T16:47:16.203386Z","iopub.status.idle":"2022-10-08T16:47:17.167194Z","shell.execute_reply.started":"2022-10-08T16:47:16.203357Z","shell.execute_reply":"2022-10-08T16:47:17.166177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Flatten, Dense, InputLayer, BatchNormalization, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:54:14.652950Z","iopub.execute_input":"2022-10-08T16:54:14.653298Z","iopub.status.idle":"2022-10-08T16:54:14.658589Z","shell.execute_reply.started":"2022-10-08T16:54:14.653268Z","shell.execute_reply":"2022-10-08T16:54:14.657668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# build a sequential model\nmodel = Sequential()\nmodel.add(InputLayer(input_shape=(256, 256, 1)))\n\n# 1st conv block\nmodel.add(Conv2D(25, (5, 5), activation='relu', strides=(1, 1), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='same'))\n# 2nd conv block\nmodel.add(Conv2D(50, (5, 5), activation='relu', strides=(2, 2), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='same'))\nmodel.add(BatchNormalization())\n# 3rd conv block\nmodel.add(Conv2D(70, (3, 3), activation='relu', strides=(2, 2), padding='same'))\nmodel.add(MaxPool2D(pool_size=(2, 2), padding='valid'))\nmodel.add(BatchNormalization())\n# ANN block\nmodel.add(Flatten())\nmodel.add(Dense(units=100, activation='relu'))\nmodel.add(Dense(units=100, activation='relu'))\nmodel.add(Dropout(0.25))\n\n\n# output layer\nmodel.add(Dense(units=16, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:54:14.928298Z","iopub.execute_input":"2022-10-08T16:54:14.928990Z","iopub.status.idle":"2022-10-08T16:54:15.014966Z","shell.execute_reply.started":"2022-10-08T16:54:14.928937Z","shell.execute_reply":"2022-10-08T16:54:15.013962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer=\"adam\", metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:54:37.320275Z","iopub.execute_input":"2022-10-08T16:54:37.320680Z","iopub.status.idle":"2022-10-08T16:54:37.337151Z","shell.execute_reply.started":"2022-10-08T16:54:37.320642Z","shell.execute_reply":"2022-10-08T16:54:37.336070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_data, epochs=30, validation_data=testing_data)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:55:29.408261Z","iopub.execute_input":"2022-10-08T16:55:29.408783Z","iopub.status.idle":"2022-10-08T17:42:49.829993Z","shell.execute_reply.started":"2022-10-08T16:55:29.408732Z","shell.execute_reply":"2022-10-08T17:42:49.828703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"SavedModel/CNN_model_Saved_Version.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:25:36.618382Z","iopub.execute_input":"2022-10-08T18:25:36.618790Z","iopub.status.idle":"2022-10-08T18:25:36.678362Z","shell.execute_reply.started":"2022-10-08T18:25:36.618757Z","shell.execute_reply":"2022-10-08T18:25:36.677325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(validation_data, verbose = 1, batch_size = 128)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:26:55.327604Z","iopub.execute_input":"2022-10-08T18:26:55.328328Z","iopub.status.idle":"2022-10-08T18:27:02.644696Z","shell.execute_reply.started":"2022-10-08T18:26:55.328291Z","shell.execute_reply":"2022-10-08T18:27:02.643787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_class = np.argmax(prediction,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:40:36.328305Z","iopub.execute_input":"2022-10-08T18:40:36.328688Z","iopub.status.idle":"2022-10-08T18:40:36.333850Z","shell.execute_reply.started":"2022-10-08T18:40:36.328655Z","shell.execute_reply":"2022-10-08T18:40:36.332751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"docnames = [i.split(\"/\")[1].split(\".tif\")[0] for i in validation_data.filenames]\nanswer = pd.DataFrame({\"id\":docnames,\"label\":prediction_class}, columns = [\"id\",\"label\"])","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:42:51.553987Z","iopub.execute_input":"2022-10-08T18:42:51.554455Z","iopub.status.idle":"2022-10-08T18:42:51.569857Z","shell.execute_reply.started":"2022-10-08T18:42:51.554402Z","shell.execute_reply":"2022-10-08T18:42:51.568510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"answer.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:42:59.617596Z","iopub.execute_input":"2022-10-08T18:42:59.618792Z","iopub.status.idle":"2022-10-08T18:42:59.628214Z","shell.execute_reply.started":"2022-10-08T18:42:59.618750Z","shell.execute_reply":"2022-10-08T18:42:59.627180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"answer.to_csv(\"SUBMISSION_File.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T18:43:30.978162Z","iopub.execute_input":"2022-10-08T18:43:30.978538Z","iopub.status.idle":"2022-10-08T18:43:30.988813Z","shell.execute_reply.started":"2022-10-08T18:43:30.978506Z","shell.execute_reply":"2022-10-08T18:43:30.987728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}