{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.10.12"},"papermill":{"default_parameters":{},"duration":26.601064,"end_time":"2023-11-13T02:44:18.358611","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2023-11-13T02:43:51.757547","version":"2.4.0"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6640479,"sourceType":"datasetVersion","datasetId":3833517},{"sourceId":6746686,"sourceType":"datasetVersion","datasetId":3884593},{"sourceId":6827935,"sourceType":"datasetVersion","datasetId":3926155},{"sourceId":6874344,"sourceType":"datasetVersion","datasetId":3950227},{"sourceId":7259757,"sourceType":"datasetVersion","datasetId":4207197},{"sourceId":7270449,"sourceType":"datasetVersion","datasetId":4214573},{"sourceId":7300930,"sourceType":"datasetVersion","datasetId":4235448},{"sourceId":7301410,"sourceType":"datasetVersion","datasetId":4235809},{"sourceId":7302608,"sourceType":"datasetVersion","datasetId":4236678},{"sourceId":7308207,"sourceType":"datasetVersion","datasetId":4240466},{"sourceId":7317147,"sourceType":"datasetVersion","datasetId":4246151},{"sourceId":7321053,"sourceType":"datasetVersion","datasetId":4248583},{"sourceId":7327694,"sourceType":"datasetVersion","datasetId":4253240},{"sourceId":147635265,"sourceType":"kernelVersion"}],"dockerImageVersionId":30627,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport math\nimport copy\nimport time\nimport random\nimport glob\nfrom PIL import Image\n\nimport numpy as np\nimport pandas as pd\n\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 torch.cuda import amp\nimport torchvision\n\nimport joblib\nfrom tqdm import tqdm\nfrom collections import defaultdict\n\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\n\nimport timm\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\nfrom colorama import Fore, Back, Style\nb_ = Fore.BLUE\nsr_ = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"\n","metadata":{"papermill":{"duration":8.038554,"end_time":"2023-11-13T02:44:03.310789","exception":false,"start_time":"2023-11-13T02:43:55.272235","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:29.155731Z","iopub.execute_input":"2024-01-03T09:13:29.156619Z","iopub.status.idle":"2024-01-03T09:13:35.547323Z","shell.execute_reply.started":"2024-01-03T09:13:29.156586Z","shell.execute_reply":"2024-01-03T09:13:35.546355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CONFIG = {\n    \"seed\": 40,\n    \"img_size\": 2054,\n    \"model_name\": \"tf_efficientnetv2_s_in21ft1k\",\n    \"num_classes\": 5,\n    \"valid_batch_size\": 4,\n    \"device\": torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\"),\n}","metadata":{"papermill":{"duration":0.071669,"end_time":"2023-11-13T02:44:03.4033","exception":false,"start_time":"2023-11-13T02:44:03.331631","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.549147Z","iopub.execute_input":"2024-01-03T09:13:35.549552Z","iopub.status.idle":"2024-01-03T09:13:35.578246Z","shell.execute_reply.started":"2024-01-03T09:13:35.549526Z","shell.execute_reply":"2024-01-03T09:13:35.577285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=42):\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    os.environ['PYTHONHASHSEED'] = str(seed)\n\nset_seed(CONFIG['seed'])","metadata":{"papermill":{"duration":0.019962,"end_time":"2023-11-13T02:44:03.43038","exception":false,"start_time":"2023-11-13T02:44:03.410418","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.579413Z","iopub.execute_input":"2024-01-03T09:13:35.579682Z","iopub.status.idle":"2024-01-03T09:13:35.602445Z","shell.execute_reply.started":"2024-01-03T09:13:35.579657Z","shell.execute_reply":"2024-01-03T09:13:35.601698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### **《《《　Dataset Paths　》》》**\n---","metadata":{}},{"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'\nModel_predict = '/kaggle/input/check-ponit007'\nLABEL_ENCODER_BIN = \"/kaggle/input/ubcpytorchwith-classweights-training-fold1of5/label_encoder.pkl\"","metadata":{"execution":{"iopub.status.busy":"2024-01-03T09:13:35.604827Z","iopub.execute_input":"2024-01-03T09:13:35.605557Z","iopub.status.idle":"2024-01-03T09:13:35.609767Z","shell.execute_reply.started":"2024-01-03T09:13:35.605531Z","shell.execute_reply":"2024-01-03T09:13:35.608948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_file_path(image_id):\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","metadata":{"papermill":{"duration":0.015472,"end_time":"2023-11-13T02:44:03.47424","exception":false,"start_time":"2023-11-13T02:44:03.458768","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.610847Z","iopub.execute_input":"2024-01-03T09:13:35.611146Z","iopub.status.idle":"2024-01-03T09:13:35.618294Z","shell.execute_reply.started":"2024-01-03T09:13:35.611123Z","shell.execute_reply":"2024-01-03T09:13:35.617246Z"},"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_test_file_path)\ndf['label'] = 0","metadata":{"papermill":{"duration":0.041286,"end_time":"2023-11-13T02:44:03.522376","exception":false,"start_time":"2023-11-13T02:44:03.48109","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.619325Z","iopub.execute_input":"2024-01-03T09:13:35.619578Z","iopub.status.idle":"2024-01-03T09:13:35.642874Z","shell.execute_reply.started":"2024-01-03T09:13:35.619556Z","shell.execute_reply":"2024-01-03T09:13:35.642049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.read_csv(f\"{ROOT_DIR}/sample_submission.csv\")","metadata":{"papermill":{"duration":0.022531,"end_time":"2023-11-13T02:44:03.552076","exception":false,"start_time":"2023-11-13T02:44:03.529545","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.644098Z","iopub.execute_input":"2024-01-03T09:13:35.644664Z","iopub.status.idle":"2024-01-03T09:13:35.652058Z","shell.execute_reply.started":"2024-01-03T09:13:35.644632Z","shell.execute_reply":"2024-01-03T09:13:35.651328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **《《《　LE Contain Other　》》》**","metadata":{}},{"cell_type":"code","source":"encoder = joblib.load(LABEL_ENCODER_BIN)\n\n\"\"\" Other \"\"\"\nle = LabelEncoder()\nencoder_append_other = le.fit(['CC', 'EC', 'HGSC', 'LGSC', 'MC', 'Other'])\nencoder_append_other.classes_","metadata":{"papermill":{"duration":0.017015,"end_time":"2023-11-13T02:44:03.575976","exception":false,"start_time":"2023-11-13T02:44:03.558961","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.653071Z","iopub.execute_input":"2024-01-03T09:13:35.653377Z","iopub.status.idle":"2024-01-03T09:13:35.666951Z","shell.execute_reply.started":"2024-01-03T09:13:35.653347Z","shell.execute_reply":"2024-01-03T09:13:35.666077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_cropped_images(file_path, image_id, th_area=1000):\n    \n    image = Image.open(file_path)\n    \n\n    as_ratio = image.size[0] / image.size[1]\n    \n    sxs, exs, sys, eys = [], [], [], []\n    \n    if as_ratio >= 1.5:\n\n        \n\n        mask = np.max(np.array(image) > 0, axis=-1).astype(np.uint8)\n        \n\n        retval, labels = cv2.connectedComponents(mask)\n        \n        if retval >= as_ratio:\n\n            x, y = np.meshgrid(np.arange(image.size[0]), np.arange(image.size[1]))\n            for label in range(1, retval):\n\n                area = np.sum(labels == label)\n                if area < th_area:\n                    continue\n                \n\n                xs, ys = x[labels == label], y[labels == label]\n                \n\n                sx, ex = np.min(xs), np.max(xs)\n                cx = (sx + ex) // 2\n                crop_size = image.size[1]\n                sx = max(0, cx - crop_size // 2)\n                ex = min(sx + crop_size - 1, image.size[0] - 1)\n                sx = ex - crop_size + 1\n                sy, ey = 0, image.size[1] - 1\n                \n\n                sxs.append(sx)\n                exs.append(ex)\n                sys.append(sy)\n                eys.append(ey)\n        else:\n            crop_size = image.size[1]\n            for i in range(int(as_ratio)):\n                sxs.append(i * crop_size)\n                exs.append((i + 1) * crop_size - 1)\n                sys.append(0)\n                eys.append(crop_size - 1)\n    else:\n        sxs, exs, sys, eys = [0,], [image.size[0] - 1], [0,], [image.size[1] - 1]\n\n    df_crop = pd.DataFrame()\n    df_crop[\"image_id\"] = [image_id] * len(sxs)\n    df_crop[\"file_path\"] = [file_path] * len(sxs)\n    df_crop[\"sx\"] = sxs\n    df_crop[\"ex\"] = exs\n    df_crop[\"sy\"] = sys\n    df_crop[\"ey\"] = eys\n    \n    return df_crop\n","metadata":{"papermill":{"duration":0.024171,"end_time":"2023-11-13T02:44:03.607376","exception":false,"start_time":"2023-11-13T02:44:03.583205","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.668333Z","iopub.execute_input":"2024-01-03T09:13:35.668577Z","iopub.status.idle":"2024-01-03T09:13:35.681722Z","shell.execute_reply.started":"2024-01-03T09:13:35.668554Z","shell.execute_reply":"2024-01-03T09:13:35.680846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dfs = []\nfor (file_path, image_id) in zip(df[\"file_path\"], df[\"image_id\"]):\n    dfs.append(get_cropped_images(file_path, image_id))\n\n\ndf_crop = pd.concat(dfs)\ndf_crop[\"label\"] = 0  # 🤖 Dummy label for cropped images","metadata":{"papermill":{"duration":0.585144,"end_time":"2023-11-13T02:44:04.199892","exception":false,"start_time":"2023-11-13T02:44:03.614748","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:35.68477Z","iopub.execute_input":"2024-01-03T09:13:35.685121Z","iopub.status.idle":"2024-01-03T09:13:36.252625Z","shell.execute_reply.started":"2024-01-03T09:13:35.685096Z","shell.execute_reply":"2024-01-03T09:13:36.251854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_crop = df_crop.drop_duplicates(subset=[\"image_id\", \"sx\", \"ex\", \"sy\", \"ey\"]).reset_index(drop=True)\n","metadata":{"papermill":{"duration":0.026989,"end_time":"2023-11-13T02:44:04.23431","exception":false,"start_time":"2023-11-13T02:44:04.207321","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.253696Z","iopub.execute_input":"2024-01-03T09:13:36.254014Z","iopub.status.idle":"2024-01-03T09:13:36.265567Z","shell.execute_reply.started":"2024-01-03T09:13:36.253989Z","shell.execute_reply":"2024-01-03T09:13:36.264628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCDataset(Dataset):\n    def __init__(self, df, transforms=None):\n\n        self.df = df\n        self.file_names = df['file_path'].values\n        self.labels = df['label'].values\n        self.transforms = transforms\n        self.sxs = df[\"sx\"].values\n        self.exs = df[\"ex\"].values\n        self.sys = df[\"sy\"].values\n        self.eys = df[\"ey\"].values\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.file_names[index]\n        sx, ex, sy, ey = self.sxs[index], self.exs[index], self.sys[index], self.eys[index]\n        \n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = img[sy:ey, sx:ex, :]\n        label = self.labels[index]\n        \n        if self.transforms:\n            img = self.transforms(image=img)[\"image\"]\n            \n\n        return {\n            'image': img,\n            'label': torch.tensor(label, dtype=torch.long)\n        }\n","metadata":{"papermill":{"duration":0.020623,"end_time":"2023-11-13T02:44:04.262244","exception":false,"start_time":"2023-11-13T02:44:04.241621","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.266635Z","iopub.execute_input":"2024-01-03T09:13:36.266958Z","iopub.status.idle":"2024-01-03T09:13:36.279085Z","shell.execute_reply.started":"2024-01-03T09:13:36.266924Z","shell.execute_reply":"2024-01-03T09:13:36.278247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transforms = {\n    \"valid\": A.Compose([\n        A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n        A.Normalize(\n            mean=[0.485, 0.456, 0.406], \n            std=[0.229, 0.224, 0.225], \n            max_pixel_value=255.0, \n            p=1.0\n        ),\n        ToTensorV2()\n    ], p=1.)\n}","metadata":{"papermill":{"duration":0.01697,"end_time":"2023-11-13T02:44:04.287059","exception":false,"start_time":"2023-11-13T02:44:04.270089","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.280539Z","iopub.execute_input":"2024-01-03T09:13:36.280936Z","iopub.status.idle":"2024-01-03T09:13:36.289429Z","shell.execute_reply.started":"2024-01-03T09:13:36.280875Z","shell.execute_reply":"2024-01-03T09:13:36.288602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM, self).__init__()\n        self.p = nn.Parameter(torch.ones(1) * p)\n        self.eps = eps\n\n    def forward(self, x):\n        return self.gem(x, p=self.p, eps=self.eps)\n        \n    def gem(self, x, p=3, eps=1e-6):\n        return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)\n        \n    def __repr__(self):\n        return self.__class__.__name__ + \\\n                '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + \\\n                ', ' + 'eps=' + str(self.eps) + ')'\n","metadata":{"papermill":{"duration":0.019491,"end_time":"2023-11-13T02:44:04.313901","exception":false,"start_time":"2023-11-13T02:44:04.29441","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.290657Z","iopub.execute_input":"2024-01-03T09:13:36.290973Z","iopub.status.idle":"2024-01-03T09:13:36.300773Z","shell.execute_reply.started":"2024-01-03T09:13:36.290946Z","shell.execute_reply":"2024-01-03T09:13:36.300057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCModel(nn.Module):\n    def __init__(self, model_name, num_classes, pretrained=False, checkpoint_path=None):\n        super(UBCModel, self).__init__()\n        self.model = timm.create_model(model_name, pretrained=pretrained)\n        in_features = self.model.classifier.in_features\n        self.model.classifier = nn.Identity()\n        self.model.global_pool = nn.Identity()\n        self.pooling = GeM()\n        self.linear = nn.Linear(in_features, num_classes)\n        self.softmax = nn.Softmax(dim=1)\n        \n    def forward(self, images):\n        features = self.model(images)\n        pooled_features = self.pooling(features).flatten(1)\n        output = self.linear(pooled_features)\n        return output","metadata":{"execution":{"iopub.status.busy":"2024-01-03T09:13:36.301862Z","iopub.execute_input":"2024-01-03T09:13:36.302223Z","iopub.status.idle":"2024-01-03T09:13:36.314361Z","shell.execute_reply.started":"2024-01-03T09:13:36.302166Z","shell.execute_reply":"2024-01-03T09:13:36.313599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### **《《《　Model Weight Paths　》》》**\n---","metadata":{}},{"cell_type":"code","source":"BEST_WEIGHT = \"/kaggle/input/baseline-0-36/Acc0.70_Loss1.0140_epoch29_tf_efficientnetv2_s_in21ft1k_0.36.bin\"\n\nBEST_WEIGHT_0 = \"/kaggle/input/alef-a-180-train-x2054-tf-efficientnetv2-m/fold0_Acc0.59_Loss1.5363_epoch7.bin\"\nBEST_WEIGHT_1 = \"/kaggle/input/alef-a-180-train-x2054-tf-efficientnetv2-m/fold1_Acc0.55_Loss2.9564_epoch14.bin\"\nBEST_WEIGHT_2 = \"/kaggle/input/alef-a-180-train-x2054-tf-efficientnetv2-m/fold2_Acc0.30_Loss5.6010_epoch11.bin\"\nBEST_WEIGHT_3 = \"/kaggle/input/alef-a-180-train-x2054-tf-efficientnetv2-m/fold3_Acc0.52_Loss3.6907_epoch8.bin\"\nBEST_WEIGHT_4 = \"/kaggle/input/alef-a-180-train-x2054-tf-efficientnetv2-m/fold4_Acc0.54_Loss2.7089_epoch18.bin\"\n\nBEST_WEIGHT_1831_0 = \"/kaggle/input/alef-a-203-train-x2054-tf-efficientnet-b3ns/fold0_Acc0.85_Loss0.6007_epoch11.bin\"\nBEST_WEIGHT_1831_1 = \"/kaggle/input/alef-a-203-train-x2054-tf-efficientnet-b3ns/fold1_Acc0.72_Loss1.1860_epoch11.bin\"\nBEST_WEIGHT_1831_2 = \"/kaggle/input/alef-a-203-train-x2054-tf-efficientnet-b3ns/fold2_Acc0.75_Loss1.0323_epoch24.bin\"\nBEST_WEIGHT_1831_3 = \"/kaggle/input/alef-a-203-train-x2054-tf-efficientnet-b3ns/fold3_Acc0.73_Loss1.1860_epoch21.bin\"\nBEST_WEIGHT_1831_4 = \"/kaggle/input/alef-a-203-train-x2054-tf-efficientnet-b3ns/fold4_Acc0.72_Loss1.0653_epoch12.bin\"\n\nBEST_WEIGHT2 = \"/kaggle/input/ubc-efficienetnetb0-fold1of10-2048pix-thumbnails/Recall0.9178_Acc0.9437_Loss0.1685_epoch9.bin\"\nBEST_WEIGHT3 = \"/kaggle/input/ubc-efficienetnetb0-fold1of10-2048pix-thumbnails/Recall0.8858_Acc0.9155_Loss0.2106_epoch1.bin\"\n# BEST_WEIGHT4 = \"/kaggle/input/ver-21-10/Acc0.50_Loss1.2095_epoch4.bin\"","metadata":{"papermill":{"duration":0.014908,"end_time":"2023-11-13T02:44:03.452122","exception":false,"start_time":"2023-11-13T02:44:03.437214","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.315275Z","iopub.execute_input":"2024-01-03T09:13:36.315505Z","iopub.status.idle":"2024-01-03T09:13:36.325628Z","shell.execute_reply.started":"2024-01-03T09:13:36.315484Z","shell.execute_reply":"2024-01-03T09:13:36.324775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# --------------------------------------------------------------------------------- #\n# Model Def\n# --------------------------------------------------------------------------------- #\n\"\"\" PUB \"\"\"\nmodel = UBCModel('tf_efficientnetv2_s_in21ft1k', CONFIG['num_classes'])\n\"\"\" v2m \"\"\"\nmodel_0 = UBCModel('tf_efficientnetv2_m.in1k', CONFIG['num_classes'])\nmodel_1 = UBCModel('tf_efficientnetv2_m.in1k', CONFIG['num_classes'])\nmodel_2 = UBCModel('tf_efficientnetv2_m.in1k', CONFIG['num_classes'])\nmodel_3 = UBCModel('tf_efficientnetv2_m.in1k', CONFIG['num_classes'])\nmodel_4 = UBCModel('tf_efficientnetv2_m.in1k', CONFIG['num_classes'])\n\"\"\" st2 b0 \"\"\"\nmodel_1830_0 = UBCModel('tf_efficientnet_b3.ns_jft_in1k', CONFIG['num_classes'])\nmodel_1830_1 = UBCModel('tf_efficientnet_b3.ns_jft_in1k', CONFIG['num_classes'])\nmodel_1830_2 = UBCModel('tf_efficientnet_b3.ns_jft_in1k', CONFIG['num_classes'])\nmodel_1830_3 = UBCModel('tf_efficientnet_b3.ns_jft_in1k', CONFIG['num_classes'])\nmodel_1830_4 = UBCModel('tf_efficientnet_b3.ns_jft_in1k', CONFIG['num_classes'])\n\"\"\" PUB \"\"\"\nmodel2 = UBCModel('tf_efficientnet_b0_ns', CONFIG['num_classes'])\nmodel3 = UBCModel('tf_efficientnet_b0_ns', CONFIG['num_classes'])\n# model4 = UBCModel('tf_efficientnet_b0_ns', CONFIG['num_classes'])\n\n# --------------------------------------------------------------------------------- #\n# Load\n# --------------------------------------------------------------------------------- #\nmodel.load_state_dict(torch.load(BEST_WEIGHT))\nmodel_0.load_state_dict(torch.load(BEST_WEIGHT_0, map_location=CONFIG['device']))\nmodel_1.load_state_dict(torch.load(BEST_WEIGHT_1, map_location=CONFIG['device']))\nmodel_2.load_state_dict(torch.load(BEST_WEIGHT_2, map_location=CONFIG['device']))\nmodel_3.load_state_dict(torch.load(BEST_WEIGHT_3, map_location=CONFIG['device']))\nmodel_4.load_state_dict(torch.load(BEST_WEIGHT_4, map_location=CONFIG['device']))\n\nmodel_1830_0.load_state_dict(torch.load(BEST_WEIGHT_1831_0, map_location=CONFIG['device']))\nmodel_1830_1.load_state_dict(torch.load(BEST_WEIGHT_1831_1, map_location=CONFIG['device']))\nmodel_1830_2.load_state_dict(torch.load(BEST_WEIGHT_1831_2, map_location=CONFIG['device']))\nmodel_1830_3.load_state_dict(torch.load(BEST_WEIGHT_1831_3, map_location=CONFIG['device']))\nmodel_1830_4.load_state_dict(torch.load(BEST_WEIGHT_1831_4, map_location=CONFIG['device']))\n\nmodel2.load_state_dict(torch.load(BEST_WEIGHT2))\nmodel3.load_state_dict(torch.load(BEST_WEIGHT3))\n# model4.load_state_dict(torch.load(BEST_WEIGHT3))\n\n# --------------------------------------------------------------------------------- #\n# to Device\n# --------------------------------------------------------------------------------- #\nmodel.to(CONFIG['device'])\nmodel_0.to(CONFIG['device'])\nmodel_1.to(CONFIG['device'])\nmodel_2.to(CONFIG['device'])\nmodel_3.to(CONFIG['device'])\nmodel_4.to(CONFIG['device'])\n\nmodel_1830_0.to(CONFIG['device'])\nmodel_1830_1.to(CONFIG['device'])\nmodel_1830_2.to(CONFIG['device'])\nmodel_1830_3.to(CONFIG['device'])\nmodel_1830_4.to(CONFIG['device'])\n\nmodel2.to(CONFIG['device'])\nmodel3.to(CONFIG['device'])\n# model4.to(CONFIG['device'])\n\nprint(\"[INFO]Model Load End.\")","metadata":{"papermill":{"duration":5.537674,"end_time":"2023-11-13T02:44:09.859078","exception":false,"start_time":"2023-11-13T02:44:04.321404","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:13:36.326961Z","iopub.execute_input":"2024-01-03T09:13:36.327301Z","iopub.status.idle":"2024-01-03T09:14:02.500287Z","shell.execute_reply.started":"2024-01-03T09:13:36.327269Z","shell.execute_reply":"2024-01-03T09:14:02.499357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = UBCDataset(df_crop, transforms=data_transforms[\"valid\"])\n\ntest_loader = DataLoader(\n    test_dataset, \n    batch_size=CONFIG['valid_batch_size'], \n    num_workers=2,\n    shuffle=False,\n    pin_memory=True\n)","metadata":{"papermill":{"duration":0.017005,"end_time":"2023-11-13T02:44:09.884413","exception":false,"start_time":"2023-11-13T02:44:09.867408","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:14:02.501626Z","iopub.execute_input":"2024-01-03T09:14:02.502002Z","iopub.status.idle":"2024-01-03T09:14:02.507877Z","shell.execute_reply.started":"2024-01-03T09:14:02.501967Z","shell.execute_reply":"2024-01-03T09:14:02.506941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### **《《《　Pred & Weight　》》》**\n---","metadata":{}},{"cell_type":"code","source":"preds = []\n\nwith torch.no_grad():\n    bar = tqdm(enumerate(test_loader), total=len(test_loader))\n    for step, data in bar:\n        images = data['image'].to(CONFIG[\"device\"], dtype=torch.float)\n      \n        # --------------------------------------------------------------------------------- #\n        # Pred\n        # --------------------------------------------------------------------------------- #\n        outputs1 = model(images)\n        outputs1_0 = model_0(images)\n        outputs1_1 = model_1(images)\n        outputs1_2 = model_2(images)\n        outputs1_3 = model_3(images)\n        outputs1_4 = model_4(images)\n        outputs_1830_0 = model_1830_0(images)\n        outputs_1830_1 = model_1830_1(images)\n        outputs_1830_2 = model_1830_2(images)\n        outputs_1830_3 = model_1830_3(images)\n        outputs_1830_4 = model_1830_4(images)\n        outputs2 = model2(images)\n        outputs3 = model3(images)\n#         outputs4 = model4(images)\n        \n        # --------------------------------------------------------------------------------- #\n        # Model Weight\n        # --------------------------------------------------------------------------------- #\n        outputs = 0.550 * outputs2 \\\n                + 0.325 * (0.4 * outputs1 + 0.6 * outputs3) \\\n                + 0.050 * ((outputs_1830_0+outputs_1830_1+outputs_1830_2+outputs_1830_3+outputs_1830_4)/5) \\\n                + 0.125 * ((outputs1_0+outputs1_1+outputs1_2+outputs1_3+outputs1_4)/5)\n\n        outputs = model.softmax(outputs)\n        preds.append(outputs.detach().cpu().numpy())\n\n\npreds = np.vstack(preds)\nprint(preds.shape)","metadata":{"papermill":{"duration":5.8547,"end_time":"2023-11-13T02:44:15.747075","exception":false,"start_time":"2023-11-13T02:44:09.892375","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:14:22.974666Z","iopub.execute_input":"2024-01-03T09:14:22.975127Z","iopub.status.idle":"2024-01-03T09:14:26.179343Z","shell.execute_reply.started":"2024-01-03T09:14:22.975093Z","shell.execute_reply":"2024-01-03T09:14:26.178299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---\n### **《《《　Post-processing & Create Sub　》》》**\n---","metadata":{}},{"cell_type":"code","source":"for i in range(preds.shape[-1]):\n    df_crop[f\"cat{i}\"] = preds[:, i]\n\"\"\"\n    Postprocess Other Class\n    * If the maximum value of proba of the 5 classes resulting from inference is less than the threshold, treat it as other classes.\n\"\"\"\nOTHER_TH = 0.3\ndf_crop[f\"cat5\"] = df_crop[[\"cat0\",\"cat1\",\"cat2\",\"cat3\",\"cat4\"]].max(axis=1)\ndf_crop.loc[df_crop['cat5'] <=  OTHER_TH, 'cat5'] = 1.0\n\ndict_label = {}\nfor image_id, gdf in df_crop.groupby(\"image_id\"):\n    dict_label[image_id] = np.argmax(gdf[[f\"cat{i}\" for i in range(6)]].values.max(axis=0))\n\npreds = np.array([dict_label[image_id] for image_id in df[\"image_id\"].values])","metadata":{"papermill":{"duration":0.027482,"end_time":"2023-11-13T02:44:15.783349","exception":false,"start_time":"2023-11-13T02:44:15.755867","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:14:26.18261Z","iopub.execute_input":"2024-01-03T09:14:26.183012Z","iopub.status.idle":"2024-01-03T09:14:26.196696Z","shell.execute_reply.started":"2024-01-03T09:14:26.182974Z","shell.execute_reply":"2024-01-03T09:14:26.195616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = encoder_append_other.inverse_transform(preds)\ndf_sub[\"label\"] = pred_labels\ndf_sub.to_csv(\"submission.csv\", index=False)","metadata":{"papermill":{"duration":0.020861,"end_time":"2023-11-13T02:44:15.812793","exception":false,"start_time":"2023-11-13T02:44:15.791932","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-01-03T09:14:26.19784Z","iopub.execute_input":"2024-01-03T09:14:26.198132Z","iopub.status.idle":"2024-01-03T09:14:26.212565Z","shell.execute_reply.started":"2024-01-03T09:14:26.198107Z","shell.execute_reply":"2024-01-03T09:14:26.21174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}