{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","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"}],"dockerImageVersionId":31259,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-01-16T11:46:37.432505Z","iopub.execute_input":"2026-01-16T11:46:37.43359Z","iopub.status.idle":"2026-01-16T11:46:37.70079Z","shell.execute_reply.started":"2026-01-16T11:46:37.433467Z","shell.execute_reply":"2026-01-16T11:46:37.69946Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\nfrom transformers import AutoModel\nfrom tqdm import tqdm\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T11:57:33.194409Z","iopub.execute_input":"2026-01-16T11:57:33.195336Z","iopub.status.idle":"2026-01-16T11:57:47.729916Z","shell.execute_reply.started":"2026-01-16T11:57:33.195261Z","shell.execute_reply":"2026-01-16T11:57:47.728867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport os\n\n# CSV load karo\ndf = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\n\n# Image folder\nIMG_DIR = \"/kaggle/input/csiro-biomass/train\"\n\n# Pehla row uthao\nrow = df.iloc[0]\n\n# Full path\nimg_path = os.path.join(IMG_DIR, row[\"image_path\"])\n\nprint(img_path)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:20:53.43041Z","iopub.execute_input":"2026-01-16T12:20:53.431033Z","iopub.status.idle":"2026-01-16T12:20:53.513266Z","shell.execute_reply.started":"2026-01-16T12:20:53.430974Z","shell.execute_reply":"2026-01-16T12:20:53.512309Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Dataset for CSIRO Biomass Prediction\n# ------------------------------------\n# This dataset reads images from the 'train' folder and the target biomass values\n# from the CSV file. It also applies optional transforms (like converting to tensors).\n\nimport os\nimport pandas as pd\nfrom PIL import Image\nfrom torch.utils.data import Dataset\nfrom torchvision import transforms\n\nclass BiomassDataset(Dataset):\n    def __init__(self, csv_file, img_dir, transform=None):\n        # Use full path to CSV file\n        self.data = pd.read_csv(csv_file)\n        self.img_dir = img_dir  # path to the 'train' folder\n        self.transform = transform\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n        row = self.data.iloc[idx]\n        img_name = row['image_path']  # CSV should have just the filename like ID1011485656.jpg\n\n        # Build full path to image\n        img_path = os.path.join(self.img_dir, img_name)\n\n        # Safety check\n        if not os.path.exists(img_path):\n            raise FileNotFoundError(f\"Image not found: {img_path}\")\n\n        # Load and convert image to RGB\n        image = Image.open(img_path).convert(\"RGB\")\n\n        # Target column (biomass)\n        target = row['biomass']\n\n        if self.transform:\n            image = self.transform(image)\n\n        return image, target\n\n# -----------------------------\n# Usage\n# -----------------------------\nIMG_DIR = \"/kaggle/input/csiro-biomass/train\"\nCSV_FILE = \"/kaggle/input/csiro-biomass/train.csv\"  # full path to CSV\n\n# Create dataset\ndataset = BiomassDataset(CSV_FILE, IMG_DIR, transform=transforms.ToTensor())\n\n# Test\nprint(f\"Number of samples: {len(dataset)}\")\nimage, target = dataset[0]\nprint(f\"Image shape: {image.shape}, Target: {target}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:27:24.602747Z","iopub.execute_input":"2026-01-16T12:27:24.60356Z","iopub.status.idle":"2026-01-16T12:27:24.644577Z","shell.execute_reply.started":"2026-01-16T12:27:24.603524Z","shell.execute_reply":"2026-01-16T12:27:24.64351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.Resize((224, 224)),\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","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:00.546675Z","iopub.execute_input":"2026-01-16T12:21:00.546981Z","iopub.status.idle":"2026-01-16T12:21:00.552607Z","shell.execute_reply.started":"2026-01-16T12:21:00.546955Z","shell.execute_reply":"2026-01-16T12:21:00.551686Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dataset = BiomassDataset(CSV_PATH, IMG_DIR, transform)\nloader = DataLoader(\n    dataset,\n    batch_size=16,\n    shuffle=True,\n    num_workers=2\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:01.843613Z","iopub.execute_input":"2026-01-16T12:21:01.84393Z","iopub.status.idle":"2026-01-16T12:21:01.861186Z","shell.execute_reply.started":"2026-01-16T12:21:01.843902Z","shell.execute_reply":"2026-01-16T12:21:01.86036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\ndinov2 = AutoModel.from_pretrained(\"facebook/dinov2-large\")\ndinov2.to(device)\ndinov2.eval()\n\nfor param in dinov2.parameters():\n    param.requires_grad = False\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:03.090886Z","iopub.execute_input":"2026-01-16T12:21:03.091335Z","iopub.status.idle":"2026-01-16T12:21:13.532153Z","shell.execute_reply.started":"2026-01-16T12:21:03.091298Z","shell.execute_reply":"2026-01-16T12:21:13.531084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class BiomassRegressor(nn.Module):\n    def __init__(self, in_features=1536, out_features=5):\n        super().__init__()\n        self.head = nn.Sequential(\n            nn.Linear(in_features, 512),\n            nn.ReLU(),\n            nn.Dropout(0.3),\n            nn.Linear(512, out_features)\n        )\n\n    def forward(self, x):\n        return self.head(x)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:13.53373Z","iopub.execute_input":"2026-01-16T12:21:13.534014Z","iopub.status.idle":"2026-01-16T12:21:13.539725Z","shell.execute_reply.started":"2026-01-16T12:21:13.533988Z","shell.execute_reply":"2026-01-16T12:21:13.538713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"regressor = BiomassRegressor().to(device)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:13.540736Z","iopub.execute_input":"2026-01-16T12:21:13.541019Z","iopub.status.idle":"2026-01-16T12:21:13.579584Z","shell.execute_reply.started":"2026-01-16T12:21:13.540994Z","shell.execute_reply":"2026-01-16T12:21:13.578713Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_features(images):\n    with torch.no_grad():\n        outputs = dinov2(pixel_values=images)\n        features = outputs.last_hidden_state[:, 0]  # CLS token\n    return features\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:16.006609Z","iopub.execute_input":"2026-01-16T12:21:16.006905Z","iopub.status.idle":"2026-01-16T12:21:16.012161Z","shell.execute_reply.started":"2026-01-16T12:21:16.00688Z","shell.execute_reply":"2026-01-16T12:21:16.011002Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"criterion = nn.MSELoss()\noptimizer = torch.optim.Adam(regressor.parameters(), lr=1e-3)\n\nEPOCHS = 10\n\nfor epoch in range(EPOCHS):\n    regressor.train()\n    total_loss = 0\n\n    for images, targets in tqdm(loader):\n        images = images.to(device)\n        targets = targets.to(device)\n\n        features = extract_features(images)\n        preds = regressor(features)\n\n        loss = criterion(preds, targets)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        total_loss += loss.item()\n\n    print(f\"Epoch {epoch+1}/{EPOCHS} | Loss: {total_loss/len(loader):.4f}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:21:27.426836Z","iopub.execute_input":"2026-01-16T12:21:27.42725Z","iopub.status.idle":"2026-01-16T12:21:27.519629Z","shell.execute_reply.started":"2026-01-16T12:21:27.427197Z","shell.execute_reply":"2026-01-16T12:21:27.518027Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\n# Check main input folder\nprint(os.listdir(\"/kaggle/input/csiro-biomass\"))\n\n# Check inside train folder\nprint(os.listdir(\"/kaggle/input/csiro-biomass/train\")[:10])\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-16T12:07:56.93043Z","iopub.execute_input":"2026-01-16T12:07:56.93113Z","iopub.status.idle":"2026-01-16T12:07:56.940317Z","shell.execute_reply.started":"2026-01-16T12:07:56.931039Z","shell.execute_reply":"2026-01-16T12:07:56.939199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}