{"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\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport matplotlib.image as mpimg\nimport os\nimport seaborn as sns\nimport cv2\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-08T07:45:01.801917Z","iopub.execute_input":"2022-10-08T07:45:01.802331Z","iopub.status.idle":"2022-10-08T07:45:02.760902Z","shell.execute_reply.started":"2022-10-08T07:45:01.802297Z","shell.execute_reply":"2022-10-08T07:45:02.759953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/datathonindoml-2022/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:45:02.762786Z","iopub.execute_input":"2022-10-08T07:45:02.763163Z","iopub.status.idle":"2022-10-08T07:45:02.780059Z","shell.execute_reply.started":"2022-10-08T07:45:02.763127Z","shell.execute_reply":"2022-10-08T07:45:02.779237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:45:03.179689Z","iopub.execute_input":"2022-10-08T07:45:03.180007Z","iopub.status.idle":"2022-10-08T07:45:03.198695Z","shell.execute_reply.started":"2022-10-08T07:45:03.179979Z","shell.execute_reply":"2022-10-08T07:45:03.197621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"images\")\nos.mkdir(\"images/l0\")\nos.mkdir(\"images/l1\")\nos.mkdir(\"images/l2\")\nos.mkdir(\"images/l3\")\nos.mkdir(\"images/l4\")\nos.mkdir(\"images/l5\")\nos.mkdir(\"images/l6\")\nos.mkdir(\"images/l7\")\nos.mkdir(\"images/l8\")\nos.mkdir(\"images/l9\")\nos.mkdir(\"images/l10\")\nos.mkdir(\"images/l11\")\nos.mkdir(\"images/l12\")\nos.mkdir(\"images/l13\")\nos.mkdir(\"images/l14\")\nos.mkdir(\"images/l15\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:45:04.087879Z","iopub.execute_input":"2022-10-08T07:45:04.088249Z","iopub.status.idle":"2022-10-08T07:45:04.097711Z","shell.execute_reply.started":"2022-10-08T07:45:04.088219Z","shell.execute_reply":"2022-10-08T07:45:04.096710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\noriginal_folder = '../input/datathonindoml-2022/train/train/'\nimages_folder = './images' \n\n\nfor i in tqdm(range(14000)):\n    try:\n        a = os.listdir(\"../input/datathonindoml-2022/train/train/\")[i].split(\".\")[0]\n        if ( a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 0:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l0/\"+filename\n            img.save(newImgPath)\n  \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 1:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l1/\"+filename\n            img.save(newImgPath)            \n  \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 2:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l2/\"+filename\n            img.save(newImgPath)\n              \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 3:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l3/\"+filename\n            img.save(newImgPath)\n             \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 4:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l4/\"+filename\n            img.save(newImgPath)\n \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 5:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l5/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 6:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l6/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 7:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l7/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 8:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l8/\"+filename\n            img.save(newImgPath)\n \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 9:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l9/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 10:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l10/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 11:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l11/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 12:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l12/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 13:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l13/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 14:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l14/\"+filename\n            img.save(newImgPath)\n            \n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 15:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = images_folder+\"/l15/\"+filename\n            img.save(newImgPath)\n            \n    except:\n        continue\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:45:04.711168Z","iopub.execute_input":"2022-10-08T07:45:04.711491Z","iopub.status.idle":"2022-10-08T07:49:13.490121Z","shell.execute_reply.started":"2022-10-08T07:45:04.711461Z","shell.execute_reply":"2022-10-08T07:49:13.489024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir(\"valid\")\nos.mkdir(\"valid/l0\")\nos.mkdir(\"valid/l1\")\nos.mkdir(\"valid/l2\")\nos.mkdir(\"valid/l3\")\nos.mkdir(\"valid/l4\")\nos.mkdir(\"valid/l5\")\nos.mkdir(\"valid/l6\")\nos.mkdir(\"valid/l7\")\nos.mkdir(\"valid/l8\")\nos.mkdir(\"valid/l9\")\nos.mkdir(\"valid/l10\")\nos.mkdir(\"valid/l11\")\nos.mkdir(\"valid/l12\")\nos.mkdir(\"valid/l13\")\nos.mkdir(\"valid/l14\")\nos.mkdir(\"valid/l15\")","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:49:13.492252Z","iopub.execute_input":"2022-10-08T07:49:13.492915Z","iopub.status.idle":"2022-10-08T07:49:13.500607Z","shell.execute_reply.started":"2022-10-08T07:49:13.492874Z","shell.execute_reply":"2022-10-08T07:49:13.499708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\noriginal_folder = \"../input/datathonindoml-2022/train/train/\"\nvalidation = \"./valid\"\n\nfor i in tqdm(range(14000,16000)):\n    try:\n        a = os.listdir(\"../input/datathonindoml-2022/train/train/\")[i].split(\".\")[0]\n        if ( a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 0:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l0/\"+filename\n            img.save(newImgPath)\n  \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 1:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l1/\"+filename\n            img.save(newImgPath)            \n  \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 2:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l2/\"+filename\n            img.save(newImgPath)\n              \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 3:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l3/\"+filename\n            img.save(newImgPath)\n             \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 4:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l4/\"+filename\n            img.save(newImgPath)\n \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 5:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l5/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 6:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l6/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 7:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l7/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 8:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l8/\"+filename\n            img.save(newImgPath)\n \n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 9:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l9/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 10:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l10/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 11:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l11/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 12:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l12/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 13:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l13/\"+filename\n            img.save(newImgPath)\n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 14:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l14/\"+filename\n            img.save(newImgPath)\n            \n\n        elif (a == str(df[\"id\"][int(a)])) and df[\"label\"][int(a)] == 15:\n            filename = os.listdir(original_folder)[i]\n            img_path = original_folder+filename\n            img = Image.open(img_path)\n            newImgPath = validation+\"/l15/\"+filename\n            img.save(newImgPath)\n            \n    except:\n        continue\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-08T07:49:13.502514Z","iopub.execute_input":"2022-10-08T07:49:13.502878Z","iopub.status.idle":"2022-10-08T07:49:43.442119Z","shell.execute_reply.started":"2022-10-08T07:49:13.502843Z","shell.execute_reply":"2022-10-08T07:49:43.441089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import the libraries \n\nfrom tensorflow.keras.layers import Input, Lambda, Dense, Flatten\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\nfrom tensorflow.keras.applications.xception import Xception\n# from tensorflow.keras.applications.efficientnet import EfficientNetV2L\nfrom tensorflow.keras.applications import InceptionV3\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator,load_img\nfrom tensorflow.keras.models import Sequential\nimport numpy as np\nfrom glob import glob","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:17.612291Z","iopub.execute_input":"2022-10-08T09:21:17.612671Z","iopub.status.idle":"2022-10-08T09:21:17.620733Z","shell.execute_reply.started":"2022-10-08T09:21:17.612622Z","shell.execute_reply":"2022-10-08T09:21:17.619682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = VGG16(weights=\"imagenet\", include_top=False, input_shape=(256,256,3))\nbase_model.trainable = False ## Not trainable weights","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:17.948123Z","iopub.execute_input":"2022-10-08T09:21:17.948428Z","iopub.status.idle":"2022-10-08T09:21:18.218998Z","shell.execute_reply.started":"2022-10-08T09:21:17.948400Z","shell.execute_reply":"2022-10-08T09:21:18.217973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:18.947310Z","iopub.execute_input":"2022-10-08T09:21:18.947699Z","iopub.status.idle":"2022-10-08T09:21:18.955177Z","shell.execute_reply.started":"2022-10-08T09:21:18.947645Z","shell.execute_reply":"2022-10-08T09:21:18.954052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n\nflatten_layer = layers.Flatten()\ndense_layer_1 = layers.Dense(50, activation='relu')\ndense_layer_2 = layers.Dense(20, activation='relu')\nprediction_layer = layers.Dense(16, activation='softmax')\n\n\nmodel = models.Sequential([\n    base_model,\n    flatten_layer,\n    dense_layer_1,\n    dense_layer_2,\n    prediction_layer\n])","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:20.020124Z","iopub.execute_input":"2022-10-08T09:21:20.020483Z","iopub.status.idle":"2022-10-08T09:21:20.099572Z","shell.execute_reply.started":"2022-10-08T09:21:20.020449Z","shell.execute_reply":"2022-10-08T09:21:20.098699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\nimport tensorflow_addons as tfa\n\nf1 = tfa.metrics.F1Score(num_classes=16, average='macro',threshold = 0.25)\n\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=[f1],\n)\n\nes = EarlyStopping(monitor=f1, mode='max', patience=5,  restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:21.020705Z","iopub.execute_input":"2022-10-08T09:21:21.021059Z","iopub.status.idle":"2022-10-08T09:21:21.039634Z","shell.execute_reply.started":"2022-10-08T09:21:21.021029Z","shell.execute_reply":"2022-10-08T09:21:21.038794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                                   shear_range = 0.2,\n#                                    brightness_range=[0.1, 0.7],\n#                                    width_shift_range=0.1,\n#                                    height_shift_range=0.1,\n                                   zoom_range=0.2)\n\ntest_datagen = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:21.911435Z","iopub.execute_input":"2022-10-08T09:21:21.912113Z","iopub.status.idle":"2022-10-08T09:21:21.917824Z","shell.execute_reply.started":"2022-10-08T09:21:21.912077Z","shell.execute_reply":"2022-10-08T09:21:21.916892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make sure you provide the same target size as initialied for the image size\ntraining_set = train_datagen.flow_from_directory('images',\n                                                 target_size = (256,256),\n                                                 batch_size = 8,\n                                                 class_mode = 'categorical')","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:22.493360Z","iopub.execute_input":"2022-10-08T09:21:22.495575Z","iopub.status.idle":"2022-10-08T09:21:23.244839Z","shell.execute_reply.started":"2022-10-08T09:21:22.495543Z","shell.execute_reply":"2022-10-08T09:21:23.243848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set = test_datagen.flow_from_directory('valid',\n                                            target_size = (256,256),\n                                            batch_size = 8,\n                                            class_mode = 'categorical')","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:23.457115Z","iopub.execute_input":"2022-10-08T09:21:23.457414Z","iopub.status.idle":"2022-10-08T09:21:23.566984Z","shell.execute_reply.started":"2022-10-08T09:21:23.457385Z","shell.execute_reply":"2022-10-08T09:21:23.565967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = model.fit(training_set,validation_data=test_set, epochs=15,callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2022-10-08T09:21:24.559954Z","iopub.execute_input":"2022-10-08T09:21:24.560602Z","iopub.status.idle":"2022-10-08T10:19:06.124959Z","shell.execute_reply.started":"2022-10-08T09:21:24.560565Z","shell.execute_reply":"2022-10-08T10:19:06.123149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(r.history['f1_score'],color='red',label='train')\nplt.plot(r.history['val_f1_score'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:06.126787Z","iopub.execute_input":"2022-10-08T10:19:06.127535Z","iopub.status.idle":"2022-10-08T10:19:06.380578Z","shell.execute_reply.started":"2022-10-08T10:19:06.127492Z","shell.execute_reply":"2022-10-08T10:19:06.379566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['f1_score'],color='red',label='train')\nplt.plot(r.history['val_f1_score'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:11.591182Z","iopub.execute_input":"2022-10-08T10:19:11.591553Z","iopub.status.idle":"2022-10-08T10:19:11.804188Z","shell.execute_reply.started":"2022-10-08T10:19:11.591521Z","shell.execute_reply":"2022-10-08T10:19:11.803281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['loss'],color='red',label='train')\nplt.plot(r.history['val_loss'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:12.317347Z","iopub.execute_input":"2022-10-08T10:19:12.318026Z","iopub.status.idle":"2022-10-08T10:19:12.514492Z","shell.execute_reply.started":"2022-10-08T10:19:12.317986Z","shell.execute_reply":"2022-10-08T10:19:12.513637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(r.history['loss'],color='red',label='train')\nplt.plot(r.history['val_loss'],color='blue',label='validation')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:12.912138Z","iopub.execute_input":"2022-10-08T10:19:12.912445Z","iopub.status.idle":"2022-10-08T10:19:13.110746Z","shell.execute_reply.started":"2022-10-08T10:19:12.912416Z","shell.execute_reply":"2022-10-08T10:19:13.109887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\ntest_img = cv2.imread('../input/datathonindoml-2022/validation/validation/17801.tif')\nplt.imshow(test_img)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:17.782282Z","iopub.execute_input":"2022-10-08T10:19:17.782746Z","iopub.status.idle":"2022-10-08T10:19:18.162265Z","shell.execute_reply.started":"2022-10-08T10:19:17.782699Z","shell.execute_reply":"2022-10-08T10:19:18.161318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:18.180777Z","iopub.execute_input":"2022-10-08T10:19:18.181210Z","iopub.status.idle":"2022-10-08T10:19:18.193763Z","shell.execute_reply.started":"2022-10-08T10:19:18.181169Z","shell.execute_reply":"2022-10-08T10:19:18.192708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = cv2.resize(test_img,(256,256))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:18.559823Z","iopub.execute_input":"2022-10-08T10:19:18.560310Z","iopub.status.idle":"2022-10-08T10:19:18.574511Z","shell.execute_reply.started":"2022-10-08T10:19:18.560279Z","shell.execute_reply":"2022-10-08T10:19:18.573511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_input = test_img.reshape((1,256,256,3))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:19.381962Z","iopub.execute_input":"2022-10-08T10:19:19.382401Z","iopub.status.idle":"2022-10-08T10:19:19.393050Z","shell.execute_reply.started":"2022-10-08T10:19:19.382362Z","shell.execute_reply":"2022-10-08T10:19:19.388999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(test_input)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:21.789897Z","iopub.execute_input":"2022-10-08T10:19:21.790341Z","iopub.status.idle":"2022-10-08T10:19:22.306623Z","shell.execute_reply.started":"2022-10-08T10:19:21.790303Z","shell.execute_reply":"2022-10-08T10:19:22.305384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in model.predict(test_input.tolist()):\n    r = list(i)\n    print(r.index(max(r)))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:22.308443Z","iopub.execute_input":"2022-10-08T10:19:22.308822Z","iopub.status.idle":"2022-10-08T10:19:24.017445Z","shell.execute_reply.started":"2022-10-08T10:19:22.308788Z","shell.execute_reply":"2022-10-08T10:19:24.016377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(img_path):\n    test_img = cv2.imread(img_path)\n    test_img = cv2.resize(test_img,(256,256))\n    test_input = test_img.reshape((1,256,256,3))\n    for i in model.predict(test_input.tolist()):\n        r = list(i)\n    \n    return r.index(max(r))","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:19:30.443516Z","iopub.execute_input":"2022-10-08T10:19:30.444103Z","iopub.status.idle":"2022-10-08T10:19:30.452471Z","shell.execute_reply.started":"2022-10-08T10:19:30.444057Z","shell.execute_reply":"2022-10-08T10:19:30.450416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\nide = []\nlabel = []\n\nfor i in tqdm(range(17801,18701)):\n    try:  \n        e = predict('../input/datathonindoml-2022/validation/validation/' + str(i) + '.tif')\n        label.append(e)\n        ide.append(i)\n    except:\n        continue\n\n\n#  18701","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:42:25.471605Z","iopub.execute_input":"2022-10-08T10:42:25.472005Z","iopub.status.idle":"2022-10-08T10:59:07.822914Z","shell.execute_reply.started":"2022-10-08T10:42:25.471974Z","shell.execute_reply":"2022-10-08T10:59:07.821923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nclass_dictionary = {\n    0:'memo',\n    1:'form',\n    2:'email',\n    3:'handwritten',\n    4:'advertisement',\n    5:'scientific_report',\n    6:'scientific_publication',\n    7:'specification',\n    8:'file_folder',\n    9:'news_article',\n    10:'budget',\n    11:'invoice',\n    12:'presentation',\n    13:'questionaire',\n    14:'resume',\n    15:'letter'\n}","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:38:08.729780Z","iopub.execute_input":"2022-10-08T10:38:08.730722Z","iopub.status.idle":"2022-10-08T10:38:08.737298Z","shell.execute_reply.started":"2022-10-08T10:38:08.730648Z","shell.execute_reply":"2022-10-08T10:38:08.736310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.DataFrame({'id' : ide,'label' : label})","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:59:23.794169Z","iopub.execute_input":"2022-10-08T10:59:23.794545Z","iopub.status.idle":"2022-10-08T10:59:23.801107Z","shell.execute_reply.started":"2022-10-08T10:59:23.794514Z","shell.execute_reply":"2022-10-08T10:59:23.800008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:59:34.953703Z","iopub.execute_input":"2022-10-08T10:59:34.954188Z","iopub.status.idle":"2022-10-08T10:59:34.961377Z","shell.execute_reply.started":"2022-10-08T10:59:34.954148Z","shell.execute_reply":"2022-10-08T10:59:34.960253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T10:59:39.752446Z","iopub.execute_input":"2022-10-08T10:59:39.752838Z","iopub.status.idle":"2022-10-08T10:59:39.762599Z","shell.execute_reply.started":"2022-10-08T10:59:39.752805Z","shell.execute_reply":"2022-10-08T10:59:39.761687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.to_csv('sample_sumission_3.csv',index = False)  # 73","metadata":{"execution":{"iopub.status.busy":"2022-10-08T11:01:54.342180Z","iopub.execute_input":"2022-10-08T11:01:54.342547Z","iopub.status.idle":"2022-10-08T11:01:54.351816Z","shell.execute_reply.started":"2022-10-08T11:01:54.342515Z","shell.execute_reply":"2022-10-08T11:01:54.350809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thank You","metadata":{}}]}