{"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 os\nimport pandas as pd\nimport cv2\nimport numpy as np\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom torch.cuda import amp\nimport torchvision","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-23T09:37:15.254355Z","iopub.execute_input":"2023-10-23T09:37:15.254638Z","iopub.status.idle":"2023-10-23T09:37:19.045477Z","shell.execute_reply.started":"2023-10-23T09:37:15.254613Z","shell.execute_reply":"2023-10-23T09:37:19.044491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = '/kaggle/input/UBC-OCEAN'\nTEST_DIR = '/kaggle/input/UBC-OCEAN/test_thumbnails'\nALT_TEST_DIR = '/kaggle/input/UBC-OCEAN/test_images'\nTRAIN_DIR = '/kaggle/input/UBC-OCEAN/train_thumbnails'\nALT_TRAIN_DIR = '/kaggle/input/UBC-OCEAN/train_images'","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.04747Z","iopub.execute_input":"2023-10-23T09:37:19.047972Z","iopub.status.idle":"2023-10-23T09:37:19.054181Z","shell.execute_reply.started":"2023-10-23T09:37:19.047938Z","shell.execute_reply":"2023-10-23T09:37:19.052344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_file_path(image_id, mode):\n    if mode == 1:\n        if os.path.exists(f\"{TEST_DIR}/{image_id}_thumbnail.png\"):\n            return f\"{TEST_DIR}/{image_id}_thumbnail.png\"\n        else:\n            return f\"{ALT_TEST_DIR}/{image_id}.png\"\n    else:\n        if os.path.exists(f\"{TRAIN_DIR}/{image_id}_thumbnail.png\"):\n            return f\"{TRAIN_DIR}/{image_id}_thumbnail.png\"\n        else:\n            return f\"{ALT_TRAIN_DIR}/{image_id}.png\"","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.057453Z","iopub.execute_input":"2023-10-23T09:37:19.057828Z","iopub.status.idle":"2023-10-23T09:37:19.065648Z","shell.execute_reply.started":"2023-10-23T09:37:19.057792Z","shell.execute_reply":"2023-10-23T09:37:19.064781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f\"{ROOT_DIR}/test.csv\")\ndf['file_path'] = df['image_id'].apply(get_file_path, args=(1, ))","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.068086Z","iopub.execute_input":"2023-10-23T09:37:19.068411Z","iopub.status.idle":"2023-10-23T09:37:19.089721Z","shell.execute_reply.started":"2023-10-23T09:37:19.068382Z","shell.execute_reply":"2023-10-23T09:37:19.088807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform_test = transforms.Compose([\n        transforms.CenterCrop(512),\n        transforms.ToTensor(),\n        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),\n    ])\n\nclass UBCDataset(Dataset):\n\n    def __init__(self, df, transform=None):\n        self.df = df\n        self.img_id = df['image_id']\n        self.img_path = df['file_path']\n        self.transform = transform\n \n    def __getitem__(self, index):\n        img_path = self.img_path[index]\n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        \n        if self.transform:\n            img = transforms.ToPILImage()(img)\n            img = self.transform(img)\n        \n        return img\n\n    def __len__(self):\n        return len(self.df)\n    \ntest_dataset = UBCDataset(df, transform=transform_test)\ntest_loader = DataLoader(test_dataset, batch_size=1, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.090886Z","iopub.execute_input":"2023-10-23T09:37:19.091239Z","iopub.status.idle":"2023-10-23T09:37:19.100854Z","shell.execute_reply.started":"2023-10-23T09:37:19.091206Z","shell.execute_reply":"2023-10-23T09:37:19.099973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_dict = {\n    0 : 'CC',\n    1 : 'EC',\n    2 : \"HGSC\",\n    3 : \"LGSC\",\n    4 : \"MC\",\n    5 : \"Other\"\n}\n# output_dict = {\n#     0 : 'HGSC',\n#     1 : 'LGSC',\n#     2 : \"EC\",\n#     3 : \"CC\",\n#     4 : \"MC\",\n#     5 : \"others\"\n# }","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.101892Z","iopub.execute_input":"2023-10-23T09:37:19.102655Z","iopub.status.idle":"2023-10-23T09:37:19.10962Z","shell.execute_reply.started":"2023-10-23T09:37:19.102631Z","shell.execute_reply":"2023-10-23T09:37:19.108636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.load(\"/kaggle/input/pretrainnew/best_normal.pth\")\nmodel.eval()\nmodel = model.cuda()\nres = []\nfor i, x in enumerate(test_loader):\n#     tmp_list = []\n    x = x.cuda()\n    pred = model(x)\n#     tmp_list.append(img_id.item())\n    pred = torch.nn.functional.softmax(pred)\n    label_pred = pred.argmax().item()\n    if pred.max().item() < 0.1:\n        label_pred = 5\n    output = output_dict[label_pred]\n    res.append(output)\ndf_sub = pd.read_csv(f\"{ROOT_DIR}/sample_submission.csv\")  \n\ndf_sub[\"label\"] = res\ndf_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:19.111208Z","iopub.execute_input":"2023-10-23T09:37:19.111749Z","iopub.status.idle":"2023-10-23T09:37:28.193679Z","shell.execute_reply.started":"2023-10-23T09:37:19.111717Z","shell.execute_reply":"2023-10-23T09:37:28.192689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pd.read_csv(\"/kaggle/working/submission.csv\").head())","metadata":{"execution":{"iopub.status.busy":"2023-10-23T09:37:28.19506Z","iopub.execute_input":"2023-10-23T09:37:28.195659Z","iopub.status.idle":"2023-10-23T09:37:28.205324Z","shell.execute_reply.started":"2023-10-23T09:37:28.195623Z","shell.execute_reply":"2023-10-23T09:37:28.204367Z"},"trusted":true},"execution_count":null,"outputs":[]}]}