{"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":31234,"isInternetEnabled":false,"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-19T00:26:28.707351Z","iopub.execute_input":"2026-01-19T00:26:28.707817Z","iopub.status.idle":"2026-01-19T00:26:31.053879Z","shell.execute_reply.started":"2026-01-19T00:26:28.707771Z","shell.execute_reply":"2026-01-19T00:26:31.052701Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# CSIRO Image2Biomass Prediction\n\n**Objective:** Maximize Weighted R² with emphasis on `Dry_Total_g`  \n**Strategy:** Transfer learning (EfficientNet) + classical regression  \n**Principles:** Stable CV, image-grouped validation, reproducible pipeline\n","metadata":{}},{"cell_type":"markdown","source":"### 1. Imports & Global Config","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nimport numpy as np\nimport pandas as pd\n\nfrom pathlib import Path\nfrom tqdm import tqdm\n\nimport torch\nimport torch.nn as nn\nfrom torchvision import models, transforms\nfrom PIL import Image\n\nfrom sklearn.model_selection import GroupKFold\nfrom sklearn.linear_model import Ridge\nfrom sklearn.metrics import r2_score\n\nimport matplotlib.pyplot as plt\n\nSEED = 42\ndef seed_everything(seed=SEED):\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n\nseed_everything()\n\nDEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(\"Device:\", DEVICE)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T00:26:40.094255Z","iopub.execute_input":"2026-01-19T00:26:40.094648Z","iopub.status.idle":"2026-01-19T00:26:52.151163Z","shell.execute_reply.started":"2026-01-19T00:26:40.094615Z","shell.execute_reply":"2026-01-19T00:26:52.149820Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 2. Paths & Data Loading","metadata":{}},{"cell_type":"code","source":"BASE_PATH = Path(\"/kaggle/input/csiro-biomass\")\n\nTRAIN_CSV = BASE_PATH / \"train.csv\"\nTEST_CSV  = BASE_PATH / \"test.csv\"\n\ndf_train = pd.read_csv(TRAIN_CSV)\ndf_test  = pd.read_csv(TEST_CSV)\n\nprint(df_train.shape, df_test.shape)\ndf_train\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T00:27:31.856301Z","iopub.execute_input":"2026-01-19T00:27:31.857224Z","iopub.status.idle":"2026-01-19T00:27:31.950210Z","shell.execute_reply.started":"2026-01-19T00:27:31.857186Z","shell.execute_reply":"2026-01-19T00:27:31.948868Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 3. Basic Sanity Checks (Non-Optional)","metadata":{}},{"cell_type":"code","source":"# Each image should appear 5 times (one per target)\nassert df_train.groupby(\"image_path\").size().nunique() == 1\n\nTARGETS = df_train[\"target_name\"].unique().tolist()\nprint(\"Targets:\", TARGETS)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T00:36:23.056194Z","iopub.execute_input":"2026-01-19T00:36:23.056652Z","iopub.status.idle":"2026-01-19T00:36:23.070635Z","shell.execute_reply.started":"2026-01-19T00:36:23.056620Z","shell.execute_reply":"2026-01-19T00:36:23.069509Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4. Minimal EDA (Decision-Driven)","metadata":{}},{"cell_type":"code","source":"# Target distributions\ndf_train.groupby(\"target_name\")[\"target\"].describe()\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T00:45:46.621220Z","iopub.execute_input":"2026-01-19T00:45:46.621713Z","iopub.status.idle":"2026-01-19T00:45:46.674427Z","shell.execute_reply.started":"2026-01-19T00:45:46.621680Z","shell.execute_reply":"2026-01-19T00:45:46.673358Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 5. Baseline Model (MANDATORY)","metadata":{}},{"cell_type":"code","source":"baseline_means = (\n    df_train\n    .groupby(\"target_name\")[\"target\"]\n    .mean()\n    .reset_index()\n)\nbaseline_submission = (\n    df_test\n    .merge(baseline_means, on=\"target_name\", how=\"left\")\n    [[\"sample_id\", \"target\"]]\n)\n\nbaseline_submission.to_csv(\"submission.csv\", index=False)\nbaseline_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-19T01:02:28.245192Z","iopub.execute_input":"2026-01-19T01:02:28.246915Z","iopub.status.idle":"2026-01-19T01:02:28.265540Z","shell.execute_reply.started":"2026-01-19T01:02:28.246817Z","shell.execute_reply":"2026-01-19T01:02:28.264353Z"}},"outputs":[],"execution_count":null}]}