{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":112509,"databundleVersionId":14254895,"sourceType":"competition"},{"sourceId":13996715,"sourceType":"datasetVersion","datasetId":8877250},{"sourceId":281521088,"sourceType":"kernelVersion"},{"sourceId":282842771,"sourceType":"kernelVersion"}],"dockerImageVersionId":31193,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install --no-index --no-deps --force-reinstall --find-links /kaggle/input/sklearn-python-package-1-7-2/sklearn_pkg scikit-learn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:04.578507Z","iopub.execute_input":"2025-11-30T11:10:04.579111Z","iopub.status.idle":"2025-11-30T11:10:09.830628Z","shell.execute_reply.started":"2025-11-30T11:10:04.579088Z","shell.execute_reply":"2025-11-30T11:10:09.829864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --no-index --no-deps --force-reinstall --find-links /kaggle/input/timm-python-package-1-0-22/timm_pkg timm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:09.831943Z","iopub.execute_input":"2025-11-30T11:10:09.832180Z","iopub.status.idle":"2025-11-30T11:10:12.743379Z","shell.execute_reply.started":"2025-11-30T11:10:09.832157Z","shell.execute_reply":"2025-11-30T11:10:12.742515Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\n\nimport timm\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom PIL import Image\nfrom torchvision import transforms\n\nimport gc\nimport random\nfrom sklearn.linear_model import Ridge\nimport joblib","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:12.744418Z","iopub.execute_input":"2025-11-30T11:10:12.744669Z","iopub.status.idle":"2025-11-30T11:10:22.919958Z","shell.execute_reply.started":"2025-11-30T11:10:12.744646Z","shell.execute_reply":"2025-11-30T11:10:22.919383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BASE_DIR = \"/kaggle/input/\"\n\nPRETRAINED_MODELS_DIR = \"/kaggle/input/timm-pretrained-model-weights/\"\n\nMODEL_CHECKPOINTS_DIR_v8 = \"/kaggle/input/csiro-i2b-model-checkpoints-v8\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:22.921383Z","iopub.execute_input":"2025-11-30T11:10:22.921784Z","iopub.status.idle":"2025-11-30T11:10:22.925330Z","shell.execute_reply.started":"2025-11-30T11:10:22.921765Z","shell.execute_reply":"2025-11-30T11:10:22.924586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"torch:\",torch.__version__)\nprint(\"timm:\",timm.__version__)\nprint(torch.cuda.is_available())\nprint(torch.cuda.get_device_name(0) if torch.cuda.is_available() else \"No GPU detected\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:22.926763Z","iopub.execute_input":"2025-11-30T11:10:22.926997Z","iopub.status.idle":"2025-11-30T11:10:23.092106Z","shell.execute_reply.started":"2025-11-30T11:10:22.926980Z","shell.execute_reply":"2025-11-30T11:10:23.091307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.092896Z","iopub.execute_input":"2025-11-30T11:10:23.093149Z","iopub.status.idle":"2025-11-30T11:10:23.097318Z","shell.execute_reply.started":"2025-11-30T11:10:23.093125Z","shell.execute_reply":"2025-11-30T11:10:23.096621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ======== Seed ========\ndef seed_everything(seed: int = 42):\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    os.environ[\"PYTHONHASHSEED\"] = str(seed)\n    # \n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\nseed_everything(42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.097943Z","iopub.execute_input":"2025-11-30T11:10:23.098135Z","iopub.status.idle":"2025-11-30T11:10:23.112246Z","shell.execute_reply.started":"2025-11-30T11:10:23.098120Z","shell.execute_reply":"2025-11-30T11:10:23.111441Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(os.path.join(BASE_DIR, \"csiro-biomass/test.csv\"))\ntest_df['image_path'] = test_df['image_path'].apply(lambda x: x.split('/')[1].split('.')[0])\ntest_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.113746Z","iopub.execute_input":"2025-11-30T11:10:23.113991Z","iopub.status.idle":"2025-11-30T11:10:23.143965Z","shell.execute_reply.started":"2025-11-30T11:10:23.113975Z","shell.execute_reply":"2025-11-30T11:10:23.143215Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"unique_test_image_paths = list(test_df['image_path'].unique())\nlen(unique_test_image_paths)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.144783Z","iopub.execute_input":"2025-11-30T11:10:23.145116Z","iopub.status.idle":"2025-11-30T11:10:23.151305Z","shell.execute_reply.started":"2025-11-30T11:10:23.145091Z","shell.execute_reply":"2025-11-30T11:10:23.150664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class ImageDataset(Dataset):\n    def __init__(self, unique_test_image_paths, directory, transform=None, patch_size=500):\n        \n        self.directory = directory\n        self.patch_size = patch_size\n        self.unique_test_image_paths = unique_test_image_paths\n\n        # Define a default transform if none provided\n        if transform is None:\n            self.transform = transforms.Compose([\n                transforms.Resize((300, 300)),  # match EfficientNet input size\n                transforms.ToTensor(),\n                transforms.Normalize(\n                    mean=(0.485, 0.456, 0.406),\n                    std=(0.229, 0.224, 0.225)\n                ),\n            ])\n        else:\n            self.transform = transform\n\n    def __len__(self):\n        return len(self.unique_test_image_paths)\n\n    def _split_into_patches(self, image):\n        \"\"\"Split 2000x1000 image into P patches of patch_size x patch_size.\"\"\"\n        width, height = image.size  # (2000, 1000)\n        pw, ph = self.patch_size, self.patch_size\n\n        patches = []\n        for top in range(0, height, ph):\n            for left in range(0, width, pw):\n                # Crop the patch\n                box = (left, top, left + pw, top + ph)\n                patch = image.crop(box)\n                patch = self.transform(patch)\n                patches.append(patch)\n\n        # patches → (P, 3, H, W)\n        return torch.stack(patches, dim=0)\n\n    def __getitem__(self, idx):\n        sample_id = self.unique_test_image_paths[idx]\n\n        # Load the original image\n        img_path = f\"{self.directory}/{sample_id}.jpg\"\n        image = Image.open(img_path).convert(\"RGB\")\n\n        # Split into P sub-images\n        patches = self._split_into_patches(image)\n\n        return patches","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.153272Z","iopub.execute_input":"2025-11-30T11:10:23.153787Z","iopub.status.idle":"2025-11-30T11:10:23.163895Z","shell.execute_reply.started":"2025-11-30T11:10:23.153770Z","shell.execute_reply":"2025-11-30T11:10:23.163277Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_A","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8A = []\n\nfor fold_name in ['model_A_fold_1.pth', 'model_A_fold_2.pth','model_A_fold_3.pth','model_A_fold_4.pth','model_A_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8A.append(fold_pred)\ny_pred_8A = np.stack(y_pred_8A, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:23.164641Z","iopub.execute_input":"2025-11-30T11:10:23.165206Z","iopub.status.idle":"2025-11-30T11:10:42.125398Z","shell.execute_reply.started":"2025-11-30T11:10:23.165189Z","shell.execute_reply":"2025-11-30T11:10:42.124843Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_B","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'beit3_base_patch16_224.in22k_ft_in1k'\n\nINPUT_PATCH_SIZE = 224\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8B = []\n\nfor fold_name in ['model_B_fold_1.pth', 'model_B_fold_2.pth','model_B_fold_3.pth','model_B_fold_4.pth','model_B_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8B.append(fold_pred)\ny_pred_8B = np.stack(y_pred_8B, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:10:42.126623Z","iopub.execute_input":"2025-11-30T11:10:42.126881Z","iopub.status.idle":"2025-11-30T11:11:00.201347Z","shell.execute_reply.started":"2025-11-30T11:10:42.126862Z","shell.execute_reply":"2025-11-30T11:11:00.200713Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_C","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'convnext_base.dinov3_lvd1689m'\n\nINPUT_PATCH_SIZE = 224\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(1024, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8C = []\n\nfor fold_name in ['model_C_fold_1.pth', 'model_C_fold_2.pth','model_C_fold_3.pth','model_C_fold_4.pth','model_C_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8C.append(fold_pred)\ny_pred_8C = np.stack(y_pred_8C, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:11:00.202025Z","iopub.execute_input":"2025-11-30T11:11:00.202325Z","iopub.status.idle":"2025-11-30T11:11:17.703110Z","shell.execute_reply.started":"2025-11-30T11:11:00.202306Z","shell.execute_reply":"2025-11-30T11:11:17.702351Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_D","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch32_clip_224.laion2b'\n\nINPUT_PATCH_SIZE = 224\nmean = (0.48145466, 0.4578275, 0.40821073)\nstd = (0.26862954, 0.26130258, 0.27577711)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8D = []\n\nfor fold_name in ['model_D_fold_1.pth', 'model_D_fold_2.pth','model_D_fold_3.pth','model_D_fold_4.pth','model_D_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8D.append(fold_pred)\ny_pred_8D = np.stack(y_pred_8D, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:11:17.704021Z","iopub.execute_input":"2025-11-30T11:11:17.704244Z","iopub.status.idle":"2025-11-30T11:11:34.755994Z","shell.execute_reply.started":"2025-11-30T11:11:17.704226Z","shell.execute_reply":"2025-11-30T11:11:34.755398Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_E","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8E = []\n\nfor fold_name in ['model_E_fold_1.pth', 'model_E_fold_2.pth','model_E_fold_3.pth','model_E_fold_4.pth','model_E_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8E.append(fold_pred)\ny_pred_8E = np.stack(y_pred_8E, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_F","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'convnext_base.dinov3_lvd1689m'\n\nINPUT_PATCH_SIZE = 224\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(1024, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8F = []\n\nfor fold_name in ['model_F_fold_1.pth', 'model_F_fold_2.pth','model_F_fold_3.pth','model_F_fold_4.pth','model_F_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8F.append(fold_pred)\ny_pred_8F = np.stack(y_pred_8F, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_G","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'beit3_base_patch16_224.in22k_ft_in1k'\n\nINPUT_PATCH_SIZE = 224\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8G = []\n\nfor fold_name in ['model_G_fold_1.pth', 'model_G_fold_2.pth','model_G_fold_3.pth','model_G_fold_4.pth','model_G_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8G.append(fold_pred)\ny_pred_8G = np.stack(y_pred_8G, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_H","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch32_siglip_256.v2_webli'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.5, 0.5, 0.5)\nstd = (0.5, 0.5, 0.5)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8H = []\n\nfor fold_name in ['model_H_fold_1.pth', 'model_H_fold_2.pth','model_H_fold_3.pth','model_H_fold_4.pth','model_H_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8H.append(fold_pred)\ny_pred_8H = np.stack(y_pred_8H, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_I","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8I = []\n\nfor fold_name in ['model_I_fold_1.pth', 'model_I_fold_2.pth','model_I_fold_3.pth','model_I_fold_4.pth','model_I_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8I.append(fold_pred)\ny_pred_8I = np.stack(y_pred_8I, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8J = []\n\nfor fold_name in ['model_J_fold_1.pth', 'model_J_fold_2.pth','model_J_fold_3.pth','model_J_fold_4.pth','model_J_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8J.append(fold_pred)\ny_pred_8J = np.stack(y_pred_8J, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_K","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8K = []\n\nfor fold_name in ['model_K_fold_1.pth', 'model_K_fold_2.pth','model_K_fold_3.pth','model_K_fold_4.pth','model_K_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8K.append(fold_pred)\ny_pred_8K = np.stack(y_pred_8K, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_L","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8L = []\n\nfor fold_name in ['model_L_fold_1.pth', 'model_L_fold_2.pth','model_L_fold_3.pth','model_L_fold_4.pth','model_L_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=500,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8L.append(fold_pred)\ny_pred_8L = np.stack(y_pred_8L, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_M","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8M = []\n\nfor fold_name in ['model_M_fold_1.pth', 'model_M_fold_2.pth','model_M_fold_3.pth','model_M_fold_4.pth','model_M_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    \n    test_dataset_1 = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=500,\n    )\n    test_dataset_loader_1 = DataLoader(test_dataset_1, batch_size=4, shuffle=False)\n    \n    fold_pred_1 = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader_1):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred_1.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred_1 = np.array(fold_pred_1).reshape(-1,5)\n\n    test_dataset_2 = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader_2 = DataLoader(test_dataset_2, batch_size=4, shuffle=False)\n    \n    fold_pred_2 = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader_2):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred_2.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred_2 = np.array(fold_pred_2).reshape(-1,5)\n\n    fold_pred = (fold_pred_1 + fold_pred_2) / 2.0\n    \n    y_pred_8M.append(fold_pred)\ny_pred_8M = np.stack(y_pred_8M, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_N","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8N = []\n\nfor fold_name in ['model_N_fold_1.pth', 'model_N_fold_2.pth','model_N_fold_3.pth','model_N_fold_4.pth','model_N_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8N.append(fold_pred)\ny_pred_8N = np.stack(y_pred_8N, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_O","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8O = []\n\nfor fold_name in ['model_O_fold_1.pth', 'model_O_fold_2.pth','model_O_fold_3.pth','model_O_fold_4.pth','model_O_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=500,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8O.append(fold_pred)\ny_pred_8O = np.stack(y_pred_8O, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_P","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8P = []\n\nfor fold_name in ['model_P_fold_1.pth', 'model_P_fold_2.pth','model_P_fold_3.pth','model_P_fold_4.pth','model_P_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8P.append(fold_pred)\ny_pred_8P = np.stack(y_pred_8P, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_Q","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8Q = []\n\nfor fold_name in ['model_Q_fold_1.pth', 'model_Q_fold_2.pth','model_Q_fold_3.pth','model_Q_fold_4.pth','model_Q_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8Q.append(fold_pred)\ny_pred_8Q = np.stack(y_pred_8Q, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_R","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8R = []\n\nfor fold_name in ['model_R_fold_1.pth', 'model_R_fold_2.pth','model_R_fold_3.pth','model_R_fold_4.pth','model_R_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8R.append(fold_pred)\ny_pred_8R = np.stack(y_pred_8R, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_S","metadata":{}},{"cell_type":"code","source":"'''backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8S = []\n\nfor fold_name in ['model_S_fold_1.pth', 'model_S_fold_2.pth','model_S_fold_3.pth','model_S_fold_4.pth','model_S_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8S.append(fold_pred)\ny_pred_8S = np.stack(y_pred_8S, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()'''","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# code_v8/model_T","metadata":{}},{"cell_type":"code","source":"backbone_model_name = 'vit_base_patch16_dinov3_qkvb.lvd1689m'\n\nINPUT_PATCH_SIZE = 256\nmean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\nval_transform = transforms.Compose([\n    transforms.Resize((INPUT_PATCH_SIZE, INPUT_PATCH_SIZE)),  # match backbone model input size\n    transforms.ToTensor(),\n    transforms.Normalize(\n        mean=mean,\n        std=std\n    ),\n])\n\nclass ResidualBlock(nn.Module):\n    def __init__(self, dim, dropout=0.3):\n        super().__init__()\n        self.block = nn.Sequential(\n            nn.Linear(dim, dim),\n            nn.BatchNorm1d(dim),\n            nn.GELU(),\n            nn.Dropout(dropout)\n        )\n    def forward(self, x):\n        return x + self.block(x)\n\nclass Model(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.backbone_model = timm.create_model(backbone_model_name, pretrained=False, num_classes=0).to(device)\n\n        self.patch_head = nn.Sequential(\n\n            nn.Linear(768, 128),\n            nn.GELU(),\n\n            ResidualBlock(128, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(128, 64),\n            nn.GELU(),\n\n            ResidualBlock(64, dropout=0.5),\n            nn.Dropout(0.5),\n\n            nn.Linear(64, 64),\n            nn.GELU(),\n\n            #ResidualBlock(64, dropout=0.5),\n            #nn.Dropout(0.5),\n\n            #nn.Linear(64, 64),\n            #nn.GELU(),\n\n            nn.Linear(64, 6),\n            nn.Softplus(beta=0.50, threshold=30)\n        )\n\n    def forward(self, x):\n        x = self.backbone_model(x)\n        x  = self.patch_head(x)\n        return x\n\n\ny_pred_8T = []\n\nfor fold_name in ['model_T_fold_1.pth', 'model_T_fold_2.pth','model_T_fold_3.pth','model_T_fold_4.pth','model_T_fold_5.pth']:\n    \n    model = Model().to(device)\n    model.load_state_dict(torch.load(os.path.join(MODEL_CHECKPOINTS_DIR_v8, fold_name)))\n    model.backbone_model.eval()\n    model.eval()\n    test_dataset = ImageDataset(\n        unique_test_image_paths,\n        directory=os.path.join(BASE_DIR, \"csiro-biomass/test/\"),\n        transform=val_transform,\n        patch_size=1000,\n    )\n    test_dataset_loader = DataLoader(test_dataset, batch_size=4, shuffle=False)\n    \n    fold_pred = []\n\n    with torch.no_grad():\n        for imgs in tqdm(test_dataset_loader):\n            imgs = imgs.to(device, dtype=torch.float32)\n            \n            B, P, C, H, W = imgs.shape\n            imgs = imgs.view(B * P, C, H, W)\n            \n            outputs = model(imgs)\n            outputs = outputs.view(B, P, 6)\n            outputs = outputs[:,:,:5]\n            outputs = nn.ReLU()(outputs)\n            outputs = outputs.sum(dim=1)\n        \n            for x in range(outputs.shape[0]):\n                fold_pred.append(outputs[x].detach().cpu().numpy())\n    \n    fold_pred = np.array(fold_pred).reshape(-1,5)\n    \n    y_pred_8T.append(fold_pred)\ny_pred_8T = np.stack(y_pred_8T, axis=0).mean(axis=0)\n\ndel model\ntorch.cuda.empty_cache()\ngc.collect()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Ensemble!","metadata":{}},{"cell_type":"code","source":"'''X_test = np.concatenate([y_pred_8A, y_pred_8B, y_pred_8C, y_pred_8D], axis=1)\nX_test.shape'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:11:34.756667Z","iopub.execute_input":"2025-11-30T11:11:34.756935Z","iopub.status.idle":"2025-11-30T11:11:34.761942Z","shell.execute_reply.started":"2025-11-30T11:11:34.756918Z","shell.execute_reply":"2025-11-30T11:11:34.761254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''X_test'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:11:47.418759Z","iopub.execute_input":"2025-11-30T11:11:47.419379Z","iopub.status.idle":"2025-11-30T11:11:47.424064Z","shell.execute_reply.started":"2025-11-30T11:11:47.419355Z","shell.execute_reply":"2025-11-30T11:11:47.423498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"'''best_model_paths = [\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold0_target0.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold0_target1.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold0_target2.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold0_target3.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold0_target4.joblib\"),\n]\n\npreds_f0 = np.zeros((len(X_test), 5), dtype=np.float32)\n\nfor i, path in enumerate(best_model_paths):\n    model = joblib.load(path)\n    preds_f0[:, i] = model.predict(X_test)\n\nbest_model_paths = [\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold1_target0.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold1_target1.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold1_target2.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold1_target3.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold1_target4.joblib\"),\n]\n\npreds_f1 = np.zeros((len(X_test), 5), dtype=np.float32)\n\nfor i, path in enumerate(best_model_paths):\n    model = joblib.load(path)\n    preds_f1[:, i] = model.predict(X_test)\n\nbest_model_paths = [\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold2_target0.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold2_target1.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold2_target2.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold2_target3.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold2_target4.joblib\"),\n]\n\npreds_f2 = np.zeros((len(X_test), 5), dtype=np.float32)\n\nfor i, path in enumerate(best_model_paths):\n    model = joblib.load(path)\n    preds_f2[:, i] = model.predict(X_test)\n\nbest_model_paths = [\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold3_target0.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold3_target1.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold3_target2.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold3_target3.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold3_target4.joblib\"),\n]\n\npreds_f3 = np.zeros((len(X_test), 5), dtype=np.float32)\n\nfor i, path in enumerate(best_model_paths):\n    model = joblib.load(path)\n    preds_f3[:, i] = model.predict(X_test)\n\nbest_model_paths = [\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold4_target0.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold4_target1.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold4_target2.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold4_target3.joblib\"),\n    os.path.join(MODEL_CHECKPOINTS_DIR_v8,\"ridge_fold4_target4.joblib\"),\n]\n\npreds_f4 = np.zeros((len(X_test), 5), dtype=np.float32)\n\nfor i, path in enumerate(best_model_paths):\n    model = joblib.load(path)\n    preds_f4[:, i] = model.predict(X_test)'''","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:27:33.338915Z","iopub.execute_input":"2025-11-30T11:27:33.339583Z","iopub.status.idle":"2025-11-30T11:27:33.385120Z","shell.execute_reply.started":"2025-11-30T11:27:33.339532Z","shell.execute_reply":"2025-11-30T11:27:33.384588Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = y_pred_8A + y_pred_8C + y_pred_8E + y_pred_8G + y_pred_8I + y_pred_8J + y_pred_8L + y_pred_8M + y_pred_8N + y_pred_8O + y_pred_8P + y_pred_8T\npredictions = predictions/12.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:28:07.317077Z","iopub.execute_input":"2025-11-30T11:28:07.317782Z","iopub.status.idle":"2025-11-30T11:28:07.321196Z","shell.execute_reply.started":"2025-11-30T11:28:07.317753Z","shell.execute_reply":"2025-11-30T11:28:07.320455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_data = []\nfor i,x in enumerate(unique_test_image_paths):\n    submission_data.append({\n        \"sample_id\": x+\"__Dry_Green_g\",\n        \"target\": float(predictions[i, 0])\n    })\n    submission_data.append({\n        \"sample_id\": x+\"__Dry_Dead_g\",\n        \"target\": float(predictions[i, 1])\n    })\n    submission_data.append({\n        \"sample_id\": x+\"__Dry_Clover_g\",\n        \"target\": float(predictions[i, 2])\n    })\n    submission_data.append({\n        \"sample_id\": x+\"__GDM_g\",\n        \"target\": float(predictions[i, 3])\n    })\n    submission_data.append({\n        \"sample_id\": x+\"__Dry_Total_g\",\n        \"target\": float(predictions[i, 4])\n    })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:28:13.573411Z","iopub.execute_input":"2025-11-30T11:28:13.573996Z","iopub.status.idle":"2025-11-30T11:28:13.578879Z","shell.execute_reply.started":"2025-11-30T11:28:13.573971Z","shell.execute_reply":"2025-11-30T11:28:13.578082Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_data)\nsubmission_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:28:14.605206Z","iopub.execute_input":"2025-11-30T11:28:14.605781Z","iopub.status.idle":"2025-11-30T11:28:14.613708Z","shell.execute_reply.started":"2025-11-30T11:28:14.605758Z","shell.execute_reply":"2025-11-30T11:28:14.612996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-30T11:28:17.245372Z","iopub.execute_input":"2025-11-30T11:28:17.245983Z","iopub.status.idle":"2025-11-30T11:28:17.254341Z","shell.execute_reply.started":"2025-11-30T11:28:17.245958Z","shell.execute_reply":"2025-11-30T11:28:17.253680Z"}},"outputs":[],"execution_count":null}]}