{"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":"none","dataSources":[{"sourceId":112509,"databundleVersionId":14254895,"sourceType":"competition"},{"sourceId":285785949,"sourceType":"kernelVersion"},{"sourceId":286813572,"sourceType":"kernelVersion"}],"dockerImageVersionId":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The notebook below preprocesses the images using the EfficientNet-B2 model (EfficientNet B2 pre-trained on imagenet dataset), and the preprocessed images is then fed to three different XGBoost Regressors, each for dry_clover_g, dry_dead_g and dry_green_g.\n\nIn this case the hyperparameters were chosen using optuna for submissions.\n\nConclusion :- (for clover, dead and green respectively)\n\nMSE (Clover): 97.58017793287635\n\nR2 Score (Clover): 0.45244639924102226\n\nMSE (Dead): 90.34844043514008\n\nR2 Score (Dead): 0.3753779046045064\n\nMSE (Green): 152.6680605821952\n\nR2 Score (Green): 0.6754128113919913\n\ntotal = clover + dead + green\n\nGDM = clover + green","metadata":{}},{"cell_type":"code","source":"!pip install --no-index --find-links /kaggle/input/transformers-wheel torch tqdm torchvision optuna-integration[xgboost] transformers==4.39.0 protobuf==3.20.3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:32:26.030156Z","iopub.execute_input":"2025-12-18T08:32:26.030387Z","iopub.status.idle":"2025-12-18T08:35:37.501232Z","shell.execute_reply.started":"2025-12-18T08:32:26.030370Z","shell.execute_reply":"2025-12-18T08:35:37.499678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split, KFold, cross_val_score\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision.models import efficientnet_v2_m\nfrom PIL import Image\nimport urllib.request\nimport torchvision.transforms as T\nfrom sklearn.metrics import mean_squared_error, r2_score\nfrom tqdm import tqdm\nimport cv2\n\nLEARNING_RATE = 1e-3\nWEIGHT_DECAY = 1e-4\nEPOCHS = 50\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nMAIN_PATH = \"/kaggle/input/csiro-biomass\"\nWEIGHTS_PATH = \"/kaggle/input/efficientnetv2-m-model/efficientnet_v2_m_state_dict.pth\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:37.505477Z","iopub.execute_input":"2025-12-18T08:35:37.505839Z","iopub.status.idle":"2025-12-18T08:35:45.391291Z","shell.execute_reply.started":"2025-12-18T08:35:37.505812Z","shell.execute_reply":"2025-12-18T08:35:45.390342Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:45.392444Z","iopub.execute_input":"2025-12-18T08:35:45.392819Z","iopub.status.idle":"2025-12-18T08:35:45.435233Z","shell.execute_reply.started":"2025-12-18T08:35:45.392798Z","shell.execute_reply":"2025-12-18T08:35:45.433983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, _ in train_data.iterrows():\n    train_data.loc[i, \"sample_id\"] = train_data.loc[i, \"sample_id\"].split(\"_\")[0]\ncols = [c for c in train_data.columns if c not in [\"target_name\", \"target\"]]\ndf_static = (\n    train_data[cols]\n      .drop_duplicates(subset=\"sample_id\")\n)\n\ndf_pivot = (\n    train_data.pivot(index=\"sample_id\", columns=\"target_name\", values=\"target\")\n      .rename(columns=lambda x: f\"{x}_target\")\n      .reset_index()\n)\n\ndf_final = df_static.merge(df_pivot, on=\"sample_id\", how=\"left\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:45.436224Z","iopub.execute_input":"2025-12-18T08:35:45.436486Z","iopub.status.idle":"2025-12-18T08:35:45.892132Z","shell.execute_reply.started":"2025-12-18T08:35:45.436466Z","shell.execute_reply":"2025-12-18T08:35:45.890212Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_final","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:45.893207Z","iopub.execute_input":"2025-12-18T08:35:45.893513Z","iopub.status.idle":"2025-12-18T08:35:45.935198Z","shell.execute_reply.started":"2025-12-18T08:35:45.893488Z","shell.execute_reply":"2025-12-18T08:35:45.933625Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# PreProcessing","metadata":{}},{"cell_type":"code","source":"model = efficientnet_v2_m(weights=None)\nstate_dict = torch.load(WEIGHTS_PATH, map_location=\"cpu\")\nmodel.load_state_dict(state_dict)\n\npreprocess = T.Compose([\n    T.Resize(480, interpolation=T.InterpolationMode.BILINEAR),\n    T.CenterCrop(480),\n    T.ToTensor(),\n    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n\nmodel.classifier = nn.Identity()\nmodel.eval()\n\nprint(f\"EfficientNetV2-M Ready !\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:45.936525Z","iopub.execute_input":"2025-12-18T08:35:45.937374Z","iopub.status.idle":"2025-12-18T08:35:49.190449Z","shell.execute_reply.started":"2025-12-18T08:35:45.937337Z","shell.execute_reply":"2025-12-18T08:35:49.189570Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class Head(nn.Module):\n    def __init__(self, in_features = 1536, hidden_features = 1024, out_features = 3):\n        super().__init__()\n        self.layers = nn.Sequential(\n            nn.Linear(in_features, hidden_features),\n            nn.BatchNorm1d(hidden_features),\n            nn.ReLU(),\n            nn.Linear(hidden_features, hidden_features // 2),\n            nn.BatchNorm1d(hidden_features // 2),\n            nn.ReLU(),\n            nn.Linear(hidden_features // 2, hidden_features // 4),\n            nn.BatchNorm1d(hidden_features // 4),\n            nn.ReLU(),\n            nn.Linear(hidden_features // 4, hidden_features // 8),\n            nn.BatchNorm1d(hidden_features // 8),\n            nn.ReLU(),\n            nn.Linear(hidden_features // 8, out_features),\n            nn.ReLU()\n        )\n    def forward(self, x):\n        return self.layers(x)\nHead_model = Head(in_features = 1280).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:49.192946Z","iopub.execute_input":"2025-12-18T08:35:49.193204Z","iopub.status.idle":"2025-12-18T08:35:49.220556Z","shell.execute_reply.started":"2025-12-18T08:35:49.193185Z","shell.execute_reply":"2025-12-18T08:35:49.219355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessed_images = []\nfor _, row in tqdm(df_final.iterrows(), total=len(df_final)):\n    image_path = os.path.join(MAIN_PATH, row[\"image_path\"])\n    try:\n        img = Image.open(image_path).convert(\"RGB\")\n    except Exception as e:\n        print(f\"Skipping bad image {image_path}: {e}\")\n        continue\n    img_tensor = preprocess(img).unsqueeze(0).to(device)\n    with torch.no_grad():\n        feature = model(img_tensor)\n\n    preprocessed_images.append(feature.cpu())\n    \npreprocessed_images = torch.cat(preprocessed_images, dim=0)\n\nprint(f\"Final Tensor Shape: {preprocessed_images.shape}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:35:49.221367Z","iopub.execute_input":"2025-12-18T08:35:49.221592Z","iopub.status.idle":"2025-12-18T08:39:59.048257Z","shell.execute_reply.started":"2025-12-18T08:35:49.221566Z","shell.execute_reply":"2025-12-18T08:39:59.047201Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessed_images.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.049200Z","iopub.execute_input":"2025-12-18T08:39:59.049494Z","iopub.status.idle":"2025-12-18T08:39:59.054903Z","shell.execute_reply.started":"2025-12-18T08:39:59.049464Z","shell.execute_reply":"2025-12-18T08:39:59.054186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = preprocessed_images\ny = df_final.loc[:, [\"Dry_Clover_g_target\", \"Dry_Dead_g_target\", \"Dry_Green_g_target\"]]\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.055721Z","iopub.execute_input":"2025-12-18T08:39:59.055987Z","iopub.status.idle":"2025-12-18T08:39:59.073868Z","shell.execute_reply.started":"2025-12-18T08:39:59.055967Z","shell.execute_reply":"2025-12-18T08:39:59.073048Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class custom_dataset(Dataset):\n    def __init__(self, features, labels):\n        self.features = features\n        self.labels = labels\n\n    def __len__(self):\n        return self.features.shape[0]\n\n    def __getitem__(self, idx):\n        return self.features[idx], torch.tensor(self.labels.iloc[idx, :].values, dtype = torch.float32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.074705Z","iopub.execute_input":"2025-12-18T08:39:59.074886Z","iopub.status.idle":"2025-12-18T08:39:59.087187Z","shell.execute_reply.started":"2025-12-18T08:39:59.074871Z","shell.execute_reply":"2025-12-18T08:39:59.086057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = custom_dataset(X, y)\ntrain_loader = DataLoader(train_dataset, batch_size = 16, shuffle = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.088110Z","iopub.execute_input":"2025-12-18T08:39:59.088526Z","iopub.status.idle":"2025-12-18T08:39:59.101791Z","shell.execute_reply.started":"2025-12-18T08:39:59.088507Z","shell.execute_reply":"2025-12-18T08:39:59.100481Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = optim.SGD(Head_model.parameters(), lr = LEARNING_RATE, weight_decay = WEIGHT_DECAY)\ncriterion = nn.MSELoss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.103509Z","iopub.execute_input":"2025-12-18T08:39:59.103833Z","iopub.status.idle":"2025-12-18T08:39:59.117088Z","shell.execute_reply.started":"2025-12-18T08:39:59.103806Z","shell.execute_reply":"2025-12-18T08:39:59.115883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for epoch in range(EPOCHS):\n    Head_model.train()\n    tr_loss = 0\n    for batch_features, batch_labels in train_loader:    \n        batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)    \n        outputs = Head_model(batch_features)\n\n        loss = criterion(outputs, batch_labels)\n        tr_loss += loss\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n    print(f\"Epoch : {epoch + 1}\\tTraining Loss : {tr_loss/len(train_loader)}\\t\", end = \"\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:39:59.118007Z","iopub.execute_input":"2025-12-18T08:39:59.118326Z","iopub.status.idle":"2025-12-18T08:40:10.205254Z","shell.execute_reply.started":"2025-12-18T08:39:59.118302Z","shell.execute_reply":"2025-12-18T08:40:10.204377Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data_2 = test_data.copy()\nfor i, _ in test_data.iterrows():\n    test_data_2.loc[i, \"sample_id\"] = test_data_2.loc[i, \"sample_id\"].split(\"_\")[0]\ncols = [c for c in test_data_2.columns if c not in [\"target_name\", \"target\"]]\nfinal_test_data = (\n    test_data_2[cols]\n      .drop_duplicates(subset=\"sample_id\")\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.206209Z","iopub.execute_input":"2025-12-18T08:40:10.206500Z","iopub.status.idle":"2025-12-18T08:40:10.215623Z","shell.execute_reply.started":"2025-12-18T08:40:10.206474Z","shell.execute_reply":"2025-12-18T08:40:10.214836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessed_test_images = []\nfor _, row in tqdm(final_test_data.iterrows(), total=len(final_test_data)):\n    image_path = os.path.join(MAIN_PATH, row[\"image_path\"])\n    try:\n        img = Image.open(image_path).convert(\"RGB\")\n    except Exception as e:\n        print(f\"Skipping bad image {image_path}: {e}\")\n        continue\n    img_tensor = preprocess(img).unsqueeze(0).to(device)\n    with torch.no_grad():\n        feature = model(img_tensor)\n\n    preprocessed_test_images.append(feature.cpu())\n    \npreprocessed_test_images = torch.cat(preprocessed_test_images, dim=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.216370Z","iopub.execute_input":"2025-12-18T08:40:10.216609Z","iopub.status.idle":"2025-12-18T08:40:10.937599Z","shell.execute_reply.started":"2025-12-18T08:40:10.216589Z","shell.execute_reply":"2025-12-18T08:40:10.936715Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preprocessed_test_images.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.938625Z","iopub.execute_input":"2025-12-18T08:40:10.938891Z","iopub.status.idle":"2025-12-18T08:40:10.944751Z","shell.execute_reply.started":"2025-12-18T08:40:10.938873Z","shell.execute_reply":"2025-12-18T08:40:10.943629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Head_model.eval()\noutputs = Head_model(preprocessed_test_images).to(device)\nclover = outputs[:, 0]\ndead = outputs[:, 1]\ngreen = outputs[:, 2]\ntotal = clover + dead + green\ngdm = clover + green\nprint(f\"Targets Predicted !\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.945762Z","iopub.execute_input":"2025-12-18T08:40:10.946183Z","iopub.status.idle":"2025-12-18T08:40:10.965572Z","shell.execute_reply.started":"2025-12-18T08:40:10.946163Z","shell.execute_reply":"2025-12-18T08:40:10.963986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"c, d, g, t, gd = 0, 0, 0, 0, 0\nsubmission = []\nfor _, row in test_data.iterrows():\n    if \"Clover\" in row[\"sample_id\"]:\n        if outputs.shape[0] == 1:\n            value = clover[c].item\n        value = clover[c].item()\n        c += 1\n    elif \"Dead\" in row[\"sample_id\"]:\n        value = dead[d].item()\n        d += 1\n    elif \"Green\" in row[\"sample_id\"]:\n        value = green[g].item()\n        g += 1\n    elif \"Total\" in row[\"sample_id\"]:\n        value = total[t].item()\n        t += 1\n    else:\n        value = gdm[gd].item()\n        gd += 1\n    submission_entry = {\n        \"sample_id\" : row[\"sample_id\"],\n        \"target\" : value\n    }\n    submission.append(submission_entry)\nsubmission = pd.DataFrame(submission)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.968492Z","iopub.execute_input":"2025-12-18T08:40:10.969924Z","iopub.status.idle":"2025-12-18T08:40:10.985230Z","shell.execute_reply.started":"2025-12-18T08:40:10.969885Z","shell.execute_reply":"2025-12-18T08:40:10.984504Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:10.986589Z","iopub.execute_input":"2025-12-18T08:40:10.986957Z","iopub.status.idle":"2025-12-18T08:40:11.008221Z","shell.execute_reply.started":"2025-12-18T08:40:10.986934Z","shell.execute_reply":"2025-12-18T08:40:11.007364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\"/kaggle/working/submission.csv\", index = False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:11.009125Z","iopub.execute_input":"2025-12-18T08:40:11.009367Z","iopub.status.idle":"2025-12-18T08:40:11.030408Z","shell.execute_reply.started":"2025-12-18T08:40:11.009350Z","shell.execute_reply":"2025-12-18T08:40:11.028711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"The entire notebook runs successfully, if error happens, I don't just know why.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-18T08:40:11.033636Z","iopub.execute_input":"2025-12-18T08:40:11.033936Z","iopub.status.idle":"2025-12-18T08:40:11.043588Z","shell.execute_reply.started":"2025-12-18T08:40:11.033910Z","shell.execute_reply":"2025-12-18T08:40:11.042421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}