{"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"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport polars as pl\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns; sns.set()\n\nfrom dataclasses import dataclass\nimport os\nfrom pathlib import Path\n\nfrom tqdm.auto import tqdm\n\nfrom torch import nn; F = nn.functional\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.optim import AdamW\n\nimport timm\n\nimport torch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:33.629615Z","iopub.execute_input":"2025-11-09T21:07:33.629904Z","iopub.status.idle":"2025-11-09T21:07:45.631212Z","shell.execute_reply.started":"2025-11-09T21:07:33.629881Z","shell.execute_reply":"2025-11-09T21:07:45.630609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"@dataclass\nclass CFG:\n    epochs: int = 5\n    device: str = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.632306Z","iopub.execute_input":"2025-11-09T21:07:45.632538Z","iopub.status.idle":"2025-11-09T21:07:45.693529Z","shell.execute_reply.started":"2025-11-09T21:07:45.632512Z","shell.execute_reply":"2025-11-09T21:07:45.692962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cfg = CFG()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.694459Z","iopub.execute_input":"2025-11-09T21:07:45.694730Z","iopub.status.idle":"2025-11-09T21:07:45.712363Z","shell.execute_reply.started":"2025-11-09T21:07:45.694703Z","shell.execute_reply":"2025-11-09T21:07:45.711685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = Path(\"/kaggle/input/csiro-biomass\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.714108Z","iopub.execute_input":"2025-11-09T21:07:45.714597Z","iopub.status.idle":"2025-11-09T21:07:45.725130Z","shell.execute_reply.started":"2025-11-09T21:07:45.714578Z","shell.execute_reply":"2025-11-09T21:07:45.724388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dir = data_dir / \"train\"\ntest_dir = data_dir / \"test\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.725952Z","iopub.execute_input":"2025-11-09T21:07:45.726222Z","iopub.status.idle":"2025-11-09T21:07:45.737687Z","shell.execute_reply.started":"2025-11-09T21:07:45.726196Z","shell.execute_reply":"2025-11-09T21:07:45.737194Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pl.read_csv(data_dir / \"train.csv\") \ntest_df = pl.read_csv(data_dir / \"test.csv\")\nsample = pl.read_csv(data_dir / \"sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.738518Z","iopub.execute_input":"2025-11-09T21:07:45.738747Z","iopub.status.idle":"2025-11-09T21:07:45.829348Z","shell.execute_reply.started":"2025-11-09T21:07:45.738726Z","shell.execute_reply":"2025-11-09T21:07:45.828604Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# check format needed for a submission\nsample.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.830042Z","iopub.execute_input":"2025-11-09T21:07:45.830260Z","iopub.status.idle":"2025-11-09T21:07:45.850878Z","shell.execute_reply.started":"2025-11-09T21:07:45.830235Z","shell.execute_reply":"2025-11-09T21:07:45.850342Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Seems to be in [ID]__[Target]_g","metadata":{}},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.851469Z","iopub.execute_input":"2025-11-09T21:07:45.851648Z","iopub.status.idle":"2025-11-09T21:07:45.858472Z","shell.execute_reply.started":"2025-11-09T21:07:45.851634Z","shell.execute_reply":"2025-11-09T21:07:45.857883Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# seems to be all over\ntrain_df[\"State\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.859170Z","iopub.execute_input":"2025-11-09T21:07:45.859400Z","iopub.status.idle":"2025-11-09T21:07:45.882609Z","shell.execute_reply.started":"2025-11-09T21:07:45.859381Z","shell.execute_reply":"2025-11-09T21:07:45.882037Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"State\"].value_counts(normalize=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.884796Z","iopub.execute_input":"2025-11-09T21:07:45.885014Z","iopub.status.idle":"2025-11-09T21:07:45.890439Z","shell.execute_reply.started":"2025-11-09T21:07:45.885000Z","shell.execute_reply":"2025-11-09T21:07:45.889865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"State\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.891043Z","iopub.execute_input":"2025-11-09T21:07:45.891301Z","iopub.status.idle":"2025-11-09T21:07:45.902252Z","shell.execute_reply.started":"2025-11-09T21:07:45.891277Z","shell.execute_reply":"2025-11-09T21:07:45.901678Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"Species\"].value_counts(sort=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.902927Z","iopub.execute_input":"2025-11-09T21:07:45.903128Z","iopub.status.idle":"2025-11-09T21:07:45.921403Z","shell.execute_reply.started":"2025-11-09T21:07:45.903114Z","shell.execute_reply":"2025-11-09T21:07:45.920881Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"Species\"].value_counts(sort=True, normalize=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.921995Z","iopub.execute_input":"2025-11-09T21:07:45.922225Z","iopub.status.idle":"2025-11-09T21:07:45.929065Z","shell.execute_reply.started":"2025-11-09T21:07:45.922210Z","shell.execute_reply":"2025-11-09T21:07:45.928493Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = train_df.with_columns(Sampling_Date=train_df[\"Sampling_Date\"].str.to_date())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.929765Z","iopub.execute_input":"2025-11-09T21:07:45.930103Z","iopub.status.idle":"2025-11-09T21:07:45.982199Z","shell.execute_reply.started":"2025-11-09T21:07:45.930084Z","shell.execute_reply":"2025-11-09T21:07:45.981534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = train_df.with_columns(Year=train_df[\"Sampling_Date\"].dt.year())\ntrain_df = train_df.with_columns(Month=train_df[\"Sampling_Date\"].dt.month())\ntrain_df = train_df.with_columns(Day=train_df[\"Sampling_Date\"].dt.day())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.983099Z","iopub.execute_input":"2025-11-09T21:07:45.983414Z","iopub.status.idle":"2025-11-09T21:07:45.991558Z","shell.execute_reply.started":"2025-11-09T21:07:45.983388Z","shell.execute_reply":"2025-11-09T21:07:45.990873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"Day\"].value_counts(normalize=True).sort(by=\"Day\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:45.992368Z","iopub.execute_input":"2025-11-09T21:07:45.992660Z","iopub.status.idle":"2025-11-09T21:07:46.004030Z","shell.execute_reply.started":"2025-11-09T21:07:45.992640Z","shell.execute_reply":"2025-11-09T21:07:46.003340Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.barplot(data=train_df[\"Day\"].value_counts(normalize=True).sort(by=\"Day\").to_pandas(),\n            x=\"Day\",\n            y=\"proportion\",\n           )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.004775Z","iopub.execute_input":"2025-11-09T21:07:46.005026Z","iopub.status.idle":"2025-11-09T21:07:46.635514Z","shell.execute_reply.started":"2025-11-09T21:07:46.005007Z","shell.execute_reply":"2025-11-09T21:07:46.634821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"Month\"].value_counts(normalize=True).sort(by=\"Month\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.636261Z","iopub.execute_input":"2025-11-09T21:07:46.636465Z","iopub.status.idle":"2025-11-09T21:07:46.642381Z","shell.execute_reply.started":"2025-11-09T21:07:46.636450Z","shell.execute_reply":"2025-11-09T21:07:46.641593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# no December or March data. Does season matter?\nsns.barplot(\n    data=train_df[\"Month\"].value_counts(normalize=True).sort(by=\"Month\").to_pandas(),\n    x=\"Month\",\n    y=\"proportion\",\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.643012Z","iopub.execute_input":"2025-11-09T21:07:46.643200Z","iopub.status.idle":"2025-11-09T21:07:46.866673Z","shell.execute_reply.started":"2025-11-09T21:07:46.643184Z","shell.execute_reply":"2025-11-09T21:07:46.866019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"Year\"].value_counts(normalize=True).sort(by=\"Year\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.867363Z","iopub.execute_input":"2025-11-09T21:07:46.867659Z","iopub.status.idle":"2025-11-09T21:07:46.874765Z","shell.execute_reply.started":"2025-11-09T21:07:46.867637Z","shell.execute_reply":"2025-11-09T21:07:46.873984Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[[\"target_name\", \"target\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.875376Z","iopub.execute_input":"2025-11-09T21:07:46.875531Z","iopub.status.idle":"2025-11-09T21:07:46.889347Z","shell.execute_reply.started":"2025-11-09T21:07:46.875519Z","shell.execute_reply":"2025-11-09T21:07:46.888767Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"target_name\"].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.890037Z","iopub.execute_input":"2025-11-09T21:07:46.890238Z","iopub.status.idle":"2025-11-09T21:07:46.903110Z","shell.execute_reply.started":"2025-11-09T21:07:46.890219Z","shell.execute_reply":"2025-11-09T21:07:46.902260Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from collections import defaultdict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.903921Z","iopub.execute_input":"2025-11-09T21:07:46.904223Z","iopub.status.idle":"2025-11-09T21:07:46.912722Z","shell.execute_reply.started":"2025-11-09T21:07:46.904202Z","shell.execute_reply":"2025-11-09T21:07:46.911898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_data = defaultdict(list)\n\nfor d in train_df[[\"target_name\", \"target\"]].to_dicts():\n    target_data[d[\"target_name\"]].append(d[\"target\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.913562Z","iopub.execute_input":"2025-11-09T21:07:46.914155Z","iopub.status.idle":"2025-11-09T21:07:46.927513Z","shell.execute_reply.started":"2025-11-09T21:07:46.914133Z","shell.execute_reply":"2025-11-09T21:07:46.926849Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_data_df = pl.DataFrame(target_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.928164Z","iopub.execute_input":"2025-11-09T21:07:46.928432Z","iopub.status.idle":"2025-11-09T21:07:46.941111Z","shell.execute_reply.started":"2025-11-09T21:07:46.928417Z","shell.execute_reply":"2025-11-09T21:07:46.940361Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_data_df.corr()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.941747Z","iopub.execute_input":"2025-11-09T21:07:46.942030Z","iopub.status.idle":"2025-11-09T21:07:46.962218Z","shell.execute_reply.started":"2025-11-09T21:07:46.942007Z","shell.execute_reply":"2025-11-09T21:07:46.961434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"target_data_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.962945Z","iopub.execute_input":"2025-11-09T21:07:46.963179Z","iopub.status.idle":"2025-11-09T21:07:46.967496Z","shell.execute_reply.started":"2025-11-09T21:07:46.963159Z","shell.execute_reply":"2025-11-09T21:07:46.966778Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(target_data_df[\"Dry_Clover_g\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:46.970868Z","iopub.execute_input":"2025-11-09T21:07:46.971070Z","iopub.status.idle":"2025-11-09T21:07:47.177579Z","shell.execute_reply.started":"2025-11-09T21:07:46.971056Z","shell.execute_reply":"2025-11-09T21:07:47.176865Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist((target_data_df[\"Dry_Clover_g\"] + 1).log())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:47.178343Z","iopub.execute_input":"2025-11-09T21:07:47.178581Z","iopub.status.idle":"2025-11-09T21:07:47.372550Z","shell.execute_reply.started":"2025-11-09T21:07:47.178565Z","shell.execute_reply":"2025-11-09T21:07:47.371975Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(target_data_df[\"Dry_Dead_g\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:47.373213Z","iopub.execute_input":"2025-11-09T21:07:47.373467Z","iopub.status.idle":"2025-11-09T21:07:47.567907Z","shell.execute_reply.started":"2025-11-09T21:07:47.373449Z","shell.execute_reply":"2025-11-09T21:07:47.567286Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist((target_data_df[\"Dry_Dead_g\"] + 1).log())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:47.568531Z","iopub.execute_input":"2025-11-09T21:07:47.568713Z","iopub.status.idle":"2025-11-09T21:07:47.750896Z","shell.execute_reply.started":"2025-11-09T21:07:47.568699Z","shell.execute_reply":"2025-11-09T21:07:47.750157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(target_data_df[\"Dry_Green_g\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:47.751702Z","iopub.execute_input":"2025-11-09T21:07:47.751979Z","iopub.status.idle":"2025-11-09T21:07:47.983371Z","shell.execute_reply.started":"2025-11-09T21:07:47.751950Z","shell.execute_reply":"2025-11-09T21:07:47.982721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist((target_data_df[\"Dry_Green_g\"] + 1).log())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:47.984046Z","iopub.execute_input":"2025-11-09T21:07:47.984469Z","iopub.status.idle":"2025-11-09T21:07:48.200231Z","shell.execute_reply.started":"2025-11-09T21:07:47.984447Z","shell.execute_reply":"2025-11-09T21:07:48.199571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(target_data_df[\"Dry_Total_g\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:48.200929Z","iopub.execute_input":"2025-11-09T21:07:48.201128Z","iopub.status.idle":"2025-11-09T21:07:48.408737Z","shell.execute_reply.started":"2025-11-09T21:07:48.201113Z","shell.execute_reply":"2025-11-09T21:07:48.407855Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist((target_data_df[\"Dry_Total_g\"] + 1).log())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:48.409570Z","iopub.execute_input":"2025-11-09T21:07:48.410436Z","iopub.status.idle":"2025-11-09T21:07:48.593556Z","shell.execute_reply.started":"2025-11-09T21:07:48.410416Z","shell.execute_reply":"2025-11-09T21:07:48.592996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist(target_data_df[\"GDM_g\"])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:48.594613Z","iopub.execute_input":"2025-11-09T21:07:48.595041Z","iopub.status.idle":"2025-11-09T21:07:48.815206Z","shell.execute_reply.started":"2025-11-09T21:07:48.595022Z","shell.execute_reply":"2025-11-09T21:07:48.814452Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.hist((target_data_df[\"GDM_g\"] + 1).log())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:48.816004Z","iopub.execute_input":"2025-11-09T21:07:48.816260Z","iopub.status.idle":"2025-11-09T21:07:49.021906Z","shell.execute_reply.started":"2025-11-09T21:07:48.816239Z","shell.execute_reply":"2025-11-09T21:07:49.021190Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Exploring the Images","metadata":{"execution":{"iopub.status.busy":"2025-11-04T02:36:41.479075Z","iopub.execute_input":"2025-11-04T02:36:41.479439Z","iopub.status.idle":"2025-11-04T02:36:41.483817Z","shell.execute_reply.started":"2025-11-04T02:36:41.479387Z","shell.execute_reply":"2025-11-04T02:36:41.482748Z"}}},{"cell_type":"code","source":"train_imgs = os.listdir(train_dir)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.022551Z","iopub.execute_input":"2025-11-09T21:07:49.022801Z","iopub.status.idle":"2025-11-09T21:07:49.032626Z","shell.execute_reply.started":"2025-11-09T21:07:49.022783Z","shell.execute_reply":"2025-11-09T21:07:49.031983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_img = plt.imread(train_dir / train_imgs[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.034051Z","iopub.execute_input":"2025-11-09T21:07:49.034257Z","iopub.status.idle":"2025-11-09T21:07:49.205972Z","shell.execute_reply.started":"2025-11-09T21:07:49.034242Z","shell.execute_reply":"2025-11-09T21:07:49.205369Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_img.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.206634Z","iopub.execute_input":"2025-11-09T21:07:49.206873Z","iopub.status.idle":"2025-11-09T21:07:49.211723Z","shell.execute_reply.started":"2025-11-09T21:07:49.206847Z","shell.execute_reply":"2025-11-09T21:07:49.211033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(sample_img)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.212472Z","iopub.execute_input":"2025-11-09T21:07:49.212821Z","iopub.status.idle":"2025-11-09T21:07:49.756401Z","shell.execute_reply.started":"2025-11-09T21:07:49.212795Z","shell.execute_reply":"2025-11-09T21:07:49.755642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"list(filter(lambda x: \"convnext_base\" in x.lower(), timm.list_models()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.760059Z","iopub.execute_input":"2025-11-09T21:07:49.760366Z","iopub.status.idle":"2025-11-09T21:07:49.771792Z","shell.execute_reply.started":"2025-11-09T21:07:49.760346Z","shell.execute_reply":"2025-11-09T21:07:49.771211Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"backbone = timm.create_model(\"convnext_base\", pretrained=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:49.772568Z","iopub.execute_input":"2025-11-09T21:07:49.772806Z","iopub.status.idle":"2025-11-09T21:07:53.996811Z","shell.execute_reply.started":"2025-11-09T21:07:49.772790Z","shell.execute_reply":"2025-11-09T21:07:53.996059Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_img_t = torch.permute(torch.tensor(sample_img), (2, 0, 1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:53.997721Z","iopub.execute_input":"2025-11-09T21:07:53.997989Z","iopub.status.idle":"2025-11-09T21:07:54.004231Z","shell.execute_reply.started":"2025-11-09T21:07:53.997969Z","shell.execute_reply":"2025-11-09T21:07:54.002839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"outs = []\nwith torch.no_grad():\n    for i in range(2):\n        for j in range(4):\n            outs.append(backbone(sample_img_t[:, i*500:(i+1)*500, j*500:(j+1)*500][None, ...] / 255.))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:07:54.005270Z","iopub.execute_input":"2025-11-09T21:07:54.005517Z","iopub.status.idle":"2025-11-09T21:08:02.356397Z","shell.execute_reply.started":"2025-11-09T21:07:54.005500Z","shell.execute_reply":"2025-11-09T21:08:02.355707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.concat(outs, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.357136Z","iopub.execute_input":"2025-11-09T21:08:02.357370Z","iopub.status.idle":"2025-11-09T21:08:02.373935Z","shell.execute_reply.started":"2025-11-09T21:08:02.357350Z","shell.execute_reply":"2025-11-09T21:08:02.373181Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.374730Z","iopub.execute_input":"2025-11-09T21:08:02.375062Z","iopub.status.idle":"2025-11-09T21:08:02.381578Z","shell.execute_reply.started":"2025-11-09T21:08:02.375040Z","shell.execute_reply":"2025-11-09T21:08:02.380944Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# make the target and image \ntrain_pivot_df = train_df.pivot(on=\"target_name\", index=\"image_path\", values=\"target\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.382144Z","iopub.execute_input":"2025-11-09T21:08:02.382298Z","iopub.status.idle":"2025-11-09T21:08:02.407257Z","shell.execute_reply.started":"2025-11-09T21:08:02.382286Z","shell.execute_reply":"2025-11-09T21:08:02.406558Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_pivot_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.408043Z","iopub.execute_input":"2025-11-09T21:08:02.408705Z","iopub.status.idle":"2025-11-09T21:08:02.414608Z","shell.execute_reply.started":"2025-11-09T21:08:02.408680Z","shell.execute_reply":"2025-11-09T21:08:02.413998Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Baseline","metadata":{}},{"cell_type":"code","source":"class Model(nn.Module):\n\n    def __init__(self, backbone):\n        super(Model, self).__init__()\n        self.backbone = backbone\n\n        self.clover_dropout = nn.Dropout(0.5)\n        self.clover = nn.Linear(8000, 1)\n\n        self.dead_dropout = nn.Dropout(0.5)\n        self.dead = nn.Linear(8000, 1)\n\n        self.green_dropout = nn.Dropout(0.5)\n        self.green = nn.Linear(8000, 1)\n\n        self.total_dropout = nn.Dropout(0.5)\n        self.total = nn.Linear(8000, 1)\n\n        self.gdm_dropout = nn.Dropout(0.5)\n        self.gdm = nn.Linear(8000, 1)\n    \n    def forward(self, x):\n        outs = []\n        with torch.no_grad():\n            for i in range(2):\n                for j in range(4):\n                    outs.append(backbone(x[..., i*500:(i+1)*500, j*500:(j+1)*500]))\n\n\n        x = torch.concat(outs, axis=1)\n\n        clover = self.clover_dropout(x)\n        clover = self.clover(x)\n\n        dead = self.dead_dropout(x)\n        dead = self.dead(x)\n\n        green = self.green_dropout(x)\n        green = self.green(x)\n\n        total = self.total_dropout(x)\n        total = self.total(x)\n\n        gdm = self.gdm_dropout(x)\n        gdm = self.gdm(x)\n\n        return torch.concat([clover, dead, green, total, gdm], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.417235Z","iopub.execute_input":"2025-11-09T21:08:02.417465Z","iopub.status.idle":"2025-11-09T21:08:02.429491Z","shell.execute_reply.started":"2025-11-09T21:08:02.417450Z","shell.execute_reply":"2025-11-09T21:08:02.428979Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Validataion Split","metadata":{}},{"cell_type":"code","source":"validation_df = train_df.filter(pl.col(\"State\") == \"WA\")\ntrain_df = train_df.filter(pl.col(\"State\") != \"WA\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.430183Z","iopub.execute_input":"2025-11-09T21:08:02.430424Z","iopub.status.idle":"2025-11-09T21:08:02.454914Z","shell.execute_reply.started":"2025-11-09T21:08:02.430404Z","shell.execute_reply":"2025-11-09T21:08:02.454157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_pivot_df = train_df.pivot(index=\"image_path\", on=\"target_name\", values=\"target\")\nvalidation_pivot_df = validation_df.pivot(index=\"image_path\", on=\"target_name\", values=\"target\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.455630Z","iopub.execute_input":"2025-11-09T21:08:02.455895Z","iopub.status.idle":"2025-11-09T21:08:02.461317Z","shell.execute_reply.started":"2025-11-09T21:08:02.455875Z","shell.execute_reply":"2025-11-09T21:08:02.460744Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_pivot_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.462002Z","iopub.execute_input":"2025-11-09T21:08:02.462228Z","iopub.status.idle":"2025-11-09T21:08:02.477409Z","shell.execute_reply.started":"2025-11-09T21:08:02.462206Z","shell.execute_reply":"2025-11-09T21:08:02.476695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"validation_pivot_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.478092Z","iopub.execute_input":"2025-11-09T21:08:02.478381Z","iopub.status.idle":"2025-11-09T21:08:02.493026Z","shell.execute_reply.started":"2025-11-09T21:08:02.478364Z","shell.execute_reply":"2025-11-09T21:08:02.492290Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Training Loop","metadata":{"execution":{"iopub.status.busy":"2025-11-05T02:50:50.847478Z","iopub.execute_input":"2025-11-05T02:50:50.847803Z","iopub.status.idle":"2025-11-05T02:50:50.855956Z","shell.execute_reply.started":"2025-11-05T02:50:50.847778Z","shell.execute_reply":"2025-11-05T02:50:50.854818Z"}}},{"cell_type":"markdown","source":"#### Overfitting Run","metadata":{}},{"cell_type":"code","source":"class Dataset(Dataset):\n\n    def __init__(self, data_dir: Path, df):\n        self.data_dir = data_dir\n        self.df = df\n\n    def __len__(self):\n        return len(self.df)\n\n    def __getitem__(self, idx):\n        img = self.df[idx].select(\"image_path\").item()\n        targets = self.df[idx].select([\"Dry_Clover_g\", \"Dry_Dead_g\", \"Dry_Green_g\", \"Dry_Total_g\", \"GDM_g\"]).to_torch().squeeze(0)\n\n        img = torch.permute(torch.tensor(plt.imread(self.data_dir / img) / 255.), (2, 0, 1))\n\n        return img, (targets + 1).log()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.493621Z","iopub.execute_input":"2025-11-09T21:08:02.493826Z","iopub.status.idle":"2025-11-09T21:08:02.508637Z","shell.execute_reply.started":"2025-11-09T21:08:02.493811Z","shell.execute_reply":"2025-11-09T21:08:02.507967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = Dataset(data_dir, train_pivot_df)\nval_ds = Dataset(data_dir, validation_pivot_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.509391Z","iopub.execute_input":"2025-11-09T21:08:02.509634Z","iopub.status.idle":"2025-11-09T21:08:02.524326Z","shell.execute_reply.started":"2025-11-09T21:08:02.509612Z","shell.execute_reply":"2025-11-09T21:08:02.523612Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dl_train = DataLoader(train_ds, batch_size=4)\ndl_test = DataLoader(val_ds, batch_size=4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.525077Z","iopub.execute_input":"2025-11-09T21:08:02.525297Z","iopub.status.idle":"2025-11-09T21:08:02.538201Z","shell.execute_reply.started":"2025-11-09T21:08:02.525282Z","shell.execute_reply":"2025-11-09T21:08:02.537487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Model(backbone)\nmodel.to(cfg.device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.538961Z","iopub.execute_input":"2025-11-09T21:08:02.539154Z","iopub.status.idle":"2025-11-09T21:08:02.827594Z","shell.execute_reply.started":"2025-11-09T21:08:02.539141Z","shell.execute_reply":"2025-11-09T21:08:02.826885Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = AdamW(model.parameters(), lr=3e-4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.828351Z","iopub.execute_input":"2025-11-09T21:08:02.828563Z","iopub.status.idle":"2025-11-09T21:08:02.833523Z","shell.execute_reply.started":"2025-11-09T21:08:02.828548Z","shell.execute_reply":"2025-11-09T21:08:02.832708Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x, y = next(iter(dl_train))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:02.834276Z","iopub.execute_input":"2025-11-09T21:08:02.834520Z","iopub.status.idle":"2025-11-09T21:08:03.996352Z","shell.execute_reply.started":"2025-11-09T21:08:02.834497Z","shell.execute_reply":"2025-11-09T21:08:03.995625Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in tqdm(range(100)):\n    optimizer.zero_grad()\n    out = model(x.float().to(cfg.device))\n    loss = F.mse_loss(out, y.float().to(cfg.device))\n    loss.backward()\n    print(loss.item())\n    optimizer.step()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:08:03.997265Z","iopub.execute_input":"2025-11-09T21:08:03.997509Z","iopub.status.idle":"2025-11-09T21:10:00.857551Z","shell.execute_reply.started":"2025-11-09T21:08:03.997489Z","shell.execute_reply":"2025-11-09T21:10:00.856731Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"#### Full Training Run","metadata":{}},{"cell_type":"code","source":"train_ds = Dataset(data_dir, train_pivot_df)\nval_ds = Dataset(data_dir, validation_pivot_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:10:00.858500Z","iopub.execute_input":"2025-11-09T21:10:00.858759Z","iopub.status.idle":"2025-11-09T21:10:00.862537Z","shell.execute_reply.started":"2025-11-09T21:10:00.858741Z","shell.execute_reply":"2025-11-09T21:10:00.861725Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dl_train = DataLoader(train_ds, batch_size=4)\ndl_test = DataLoader(val_ds, batch_size=4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:10:00.863221Z","iopub.execute_input":"2025-11-09T21:10:00.863453Z","iopub.status.idle":"2025-11-09T21:10:00.877908Z","shell.execute_reply.started":"2025-11-09T21:10:00.863436Z","shell.execute_reply":"2025-11-09T21:10:00.877157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Model(backbone)\nmodel.to(cfg.device)\nprint()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:10:00.878773Z","iopub.execute_input":"2025-11-09T21:10:00.879225Z","iopub.status.idle":"2025-11-09T21:10:00.897604Z","shell.execute_reply.started":"2025-11-09T21:10:00.879203Z","shell.execute_reply":"2025-11-09T21:10:00.896890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = AdamW(model.parameters(), lr=3e-4)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:10:00.898331Z","iopub.execute_input":"2025-11-09T21:10:00.898488Z","iopub.status.idle":"2025-11-09T21:10:00.903994Z","shell.execute_reply.started":"2025-11-09T21:10:00.898475Z","shell.execute_reply":"2025-11-09T21:10:00.903293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loss = []\ntrain_loss_epochs = []\nval_loss_epochs = []\n\nfor epoch in tqdm(range(cfg.epochs)):\n\n    model.train()\n    running_train_loss = 0.0\n    for x,y in tqdm(dl_train):\n        optimizer.zero_grad()\n        out = model(x.float().to(cfg.device))\n        loss = F.mse_loss(out, y.float().to(cfg.device))\n        loss.backward()\n        optimizer.step()\n        running_train_loss += loss.item()\n        train_loss.append(loss.item())\n\n    train_loss_epochs.append(running_train_loss / len(train_ds))\n\n    print(f\"TRAIN LOSS EPOCH {epoch+1}: {train_loss_epochs[-1]}\")\n\n    model.eval()\n    running_val_loss = 0.0\n    for x,y in tqdm(dl_test):\n        with torch.no_grad():\n            out = model(x.float().to(cfg.device))\n            loss = F.mse_loss(out, y.float().to(cfg.device))\n            running_val_loss += loss.item()\n\n    val_loss_epochs.append(running_val_loss / len(val_ds))\n\n    print(f\"VAL LOSS EPOCH {epoch+1}: {val_loss_epochs[-1]}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:10:00.904733Z","iopub.execute_input":"2025-11-09T21:10:00.905063Z","iopub.status.idle":"2025-11-09T21:22:30.692459Z","shell.execute_reply.started":"2025-11-09T21:10:00.905040Z","shell.execute_reply":"2025-11-09T21:22:30.691694Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"torch.save(model.state_dict(), \"/kaggle/working/baseline.pt\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-09T21:24:10.444735Z","iopub.execute_input":"2025-11-09T21:24:10.445012Z","iopub.status.idle":"2025-11-09T21:24:10.900051Z","shell.execute_reply.started":"2025-11-09T21:24:10.444995Z","shell.execute_reply":"2025-11-09T21:24:10.899413Z"}},"outputs":[],"execution_count":null}]}