{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":154285609,"sourceType":"kernelVersion"}],"dockerImageVersionId":30559,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\n\nimport numpy as np\nimport pandas as pd\n\nimport PIL\nfrom PIL import Image\nfrom tqdm import tqdm\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import  load_model\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.applications import EfficientNetV2S\nfrom tensorflow.keras.preprocessing.image import load_img, img_to_array\n\nPIL.Image.MAX_IMAGE_PIXELS = 933120000000","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-09T16:16:00.144767Z","iopub.execute_input":"2023-12-09T16:16:00.145175Z","iopub.status.idle":"2023-12-09T16:16:00.152112Z","shell.execute_reply.started":"2023-12-09T16:16:00.145132Z","shell.execute_reply":"2023-12-09T16:16:00.151077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PATH_DIR = '/kaggle/input/UBC-OCEAN'\nIMG_SIZE = 600\nNUM_CLASSES = 5\n\nbatch_size = 8","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:16:00.370369Z","iopub.execute_input":"2023-12-09T16:16:00.371237Z","iopub.status.idle":"2023-12-09T16:16:00.376184Z","shell.execute_reply.started":"2023-12-09T16:16:00.371194Z","shell.execute_reply":"2023-12-09T16:16:00.375023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(os.path.join(PATH_DIR, 'test.csv'))\n\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:16:01.525261Z","iopub.execute_input":"2023-12-09T16:16:01.525993Z","iopub.status.idle":"2023-12-09T16:16:01.560041Z","shell.execute_reply.started":"2023-12-09T16:16:01.525958Z","shell.execute_reply":"2023-12-09T16:16:01.559033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encode = {'HGSC': 0, 'LGSC': 1, 'EC': 2, 'CC': 3, 'MC': 4}\ndecode = {lbl: idx for idx, lbl in encode.items()}\ndecode","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:16:02.285369Z","iopub.execute_input":"2023-12-09T16:16:02.285742Z","iopub.status.idle":"2023-12-09T16:16:02.292632Z","shell.execute_reply.started":"2023-12-09T16:16:02.285706Z","shell.execute_reply":"2023-12-09T16:16:02.291733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_file_path(image_id):\n    if os.path.exists(f\"{PATH_DIR}/test_thumbnails/{image_id}_thumbnail.png\"):\n        return f\"{PATH_DIR}/test_thumbnails/{image_id}_thumbnail.png\"\n    else:\n        return f\"{PATH_DIR}/train_images/{image_id}.png\"\n    \n    \ntest_df['file_path'] = test_df['image_id'].apply(get_file_path)\n\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:17:01.067436Z","iopub.execute_input":"2023-12-09T16:17:01.067791Z","iopub.status.idle":"2023-12-09T16:17:01.080207Z","shell.execute_reply.started":"2023-12-09T16:17:01.067761Z","shell.execute_reply":"2023-12-09T16:17:01.079098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load test Images","metadata":{}},{"cell_type":"code","source":"test_files = test_df['image_id'].unique().tolist()\n\nprint(\"Testing Images: \", len(test_files))","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:17:01.995428Z","iopub.execute_input":"2023-12-09T16:17:01.996063Z","iopub.status.idle":"2023-12-09T16:17:02.0066Z","shell.execute_reply.started":"2023-12-09T16:17:01.996033Z","shell.execute_reply":"2023-12-09T16:17:02.005662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = []\n\ndef load_image(filename):\n    img = load_img(filename, target_size=(IMG_SIZE, IMG_SIZE))\n    img = img_to_array(img)\n    return img\n\n\nfor i in tqdm(range(len(test_files))):\n    file_path = test_df.iloc[i]['file_path']\n    test.append(load_image(file_path))\n    \nds_test = tf.data.Dataset.from_tensor_slices(np.array(test))\nds_test = ds_test.batch(batch_size=batch_size)\n\nprint(ds_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:17:02.407043Z","iopub.execute_input":"2023-12-09T16:17:02.407386Z","iopub.status.idle":"2023-12-09T16:17:02.780596Z","shell.execute_reply.started":"2023-12-09T16:17:02.40736Z","shell.execute_reply":"2023-12-09T16:17:02.779554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load PreTrained Model","metadata":{}},{"cell_type":"code","source":"model = load_model('/kaggle/input/starter-code-effnetv2s-training/UBC-OCEAN_42_2964.h5')","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:17:03.97308Z","iopub.execute_input":"2023-12-09T16:17:03.973887Z","iopub.status.idle":"2023-12-09T16:17:09.901413Z","shell.execute_reply.started":"2023-12-09T16:17:03.973851Z","shell.execute_reply":"2023-12-09T16:17:09.900573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(ds_test)\npred = pred.argmax(axis=-1)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T16:17:09.903377Z","iopub.execute_input":"2023-12-09T16:17:09.903702Z","iopub.status.idle":"2023-12-09T16:17:19.96795Z","shell.execute_reply.started":"2023-12-09T16:17:09.903675Z","shell.execute_reply":"2023-12-09T16:17:19.96711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Submission Files","metadata":{}},{"cell_type":"code","source":"sub_df = pd.DataFrame({'image_id':test_df['image_id'],\n                       'label': [decode[i] for i in pred]\n                      })\n\nsub_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-05T11:26:45.075644Z","iopub.execute_input":"2023-11-05T11:26:45.076336Z","iopub.status.idle":"2023-11-05T11:26:45.083895Z","shell.execute_reply.started":"2023-11-05T11:26:45.0763Z","shell.execute_reply":"2023-11-05T11:26:45.083198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-05T11:26:45.085165Z","iopub.execute_input":"2023-11-05T11:26:45.085941Z","iopub.status.idle":"2023-11-05T11:26:45.096951Z","shell.execute_reply.started":"2023-11-05T11:26:45.085907Z","shell.execute_reply":"2023-11-05T11:26:45.096017Z"},"trusted":true},"execution_count":null,"outputs":[]}]}