{"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"},{"sourceId":14247045,"sourceType":"datasetVersion","datasetId":9089876},{"sourceId":694473,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":526644,"modelId":540688}],"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        if '.csv' in filename:\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":"2025-12-26T15:14:40.879044Z","iopub.execute_input":"2025-12-26T15:14:40.879473Z","iopub.status.idle":"2025-12-26T15:14:41.208673Z","shell.execute_reply.started":"2025-12-26T15:14:40.879439Z","shell.execute_reply":"2025-12-26T15:14:41.207438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import timm\nimport torch\nimport torch.nn as nn\n\nfrom torchvision import transforms\nfrom PIL import Image\n\nfrom xgboost import XGBRegressor\nfrom sklearn.multioutput import MultiOutputRegressor\nfrom sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:42.623897Z","iopub.execute_input":"2025-12-26T15:14:42.624353Z","iopub.status.idle":"2025-12-26T15:14:42.630668Z","shell.execute_reply.started":"2025-12-26T15:14:42.624317Z","shell.execute_reply":"2025-12-26T15:14:42.629264Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/csiro-biomass/train.csv')\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:44.281884Z","iopub.execute_input":"2025-12-26T15:14:44.283029Z","iopub.status.idle":"2025-12-26T15:14:44.310603Z","shell.execute_reply.started":"2025-12-26T15:14:44.282903Z","shell.execute_reply":"2025-12-26T15:14:44.309338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Unique Target Names: ', df['target_name'].unique())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:48.248720Z","iopub.execute_input":"2025-12-26T15:14:48.249783Z","iopub.status.idle":"2025-12-26T15:14:48.257491Z","shell.execute_reply.started":"2025-12-26T15:14:48.249733Z","shell.execute_reply":"2025-12-26T15:14:48.256418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['image_id'] = df['image_path'].str.replace('train/', '')\ndf['image_id'] = df['image_id'].str.replace('.jpg', '')\ndf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:50.392679Z","iopub.execute_input":"2025-12-26T15:14:50.393083Z","iopub.status.idle":"2025-12-26T15:14:50.417076Z","shell.execute_reply.started":"2025-12-26T15:14:50.393048Z","shell.execute_reply":"2025-12-26T15:14:50.415811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"alter_df = df.pivot(\n    index = 'image_id',\n    columns = 'target_name',\n    values = 'target'\n)\nalter_df = alter_df.reset_index()\nalter_df = alter_df.rename(columns={'target_name': 'idx'})\nalter_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:53.181408Z","iopub.execute_input":"2025-12-26T15:14:53.181810Z","iopub.status.idle":"2025-12-26T15:14:53.206903Z","shell.execute_reply.started":"2025-12-26T15:14:53.181778Z","shell.execute_reply":"2025-12-26T15:14:53.205684Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:14:56.899490Z","iopub.execute_input":"2025-12-26T15:14:56.900048Z","iopub.status.idle":"2025-12-26T15:14:56.906463Z","shell.execute_reply.started":"2025-12-26T15:14:56.899901Z","shell.execute_reply":"2025-12-26T15:14:56.905008Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = timm.create_model(\n    \"efficientnet_b0\",\n    pretrained=False,\n    num_classes=0\n)\n\nweights_path = \"/kaggle/input/efficientnet-b0-pth/efficientnet_b0.pth\"\n\nstate_dict = torch.load(weights_path, map_location=\"cpu\")\nmodel.load_state_dict(state_dict)\n\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:15:04.701133Z","iopub.execute_input":"2025-12-26T15:15:04.701592Z","iopub.status.idle":"2025-12-26T15:15:04.887176Z","shell.execute_reply.started":"2025-12-26T15:15:04.701556Z","shell.execute_reply":"2025-12-26T15:15:04.885986Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()\nmodel.to(device)\n\n# Freeze all parameters\nfor p in model.parameters():\n    p.requires_grad = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:15:12.678565Z","iopub.execute_input":"2025-12-26T15:15:12.679835Z","iopub.status.idle":"2025-12-26T15:15:12.694160Z","shell.execute_reply.started":"2025-12-26T15:15:12.679779Z","shell.execute_reply":"2025-12-26T15:15:12.692917Z"}},"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])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:15:16.790535Z","iopub.execute_input":"2025-12-26T15:15:16.791514Z","iopub.status.idle":"2025-12-26T15:15:16.796869Z","shell.execute_reply.started":"2025-12-26T15:15:16.791479Z","shell.execute_reply":"2025-12-26T15:15:16.795530Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_embedding(image_path):\n    img = Image.open(image_path).convert(\"RGB\")\n    img = transform(img).unsqueeze(0).to(device)\n\n    with torch.no_grad():\n        emb = model(img)\n\n    return emb.cpu().numpy().squeeze()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:15:18.376881Z","iopub.execute_input":"2025-12-26T15:15:18.378446Z","iopub.status.idle":"2025-12-26T15:15:18.384721Z","shell.execute_reply.started":"2025-12-26T15:15:18.378362Z","shell.execute_reply":"2025-12-26T15:15:18.383350Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_dir = \"/kaggle/input/csiro-biomass/train\"\n\nX_embeddings = []\ny_targets = []\n\ntarget_cols = [\"Dry_Clover_g\", \"Dry_Dead_g\", \"Dry_Green_g\", \"Dry_Total_g\", \"GDM_g\"]\n\nfor _, row in alter_df.iterrows():\n    image_path = os.path.join(image_dir, row[\"image_id\"] + \".jpg\")\n\n    emb = extract_embedding(image_path)\n    X_embeddings.append(emb)\n\n    y_targets.append(row[target_cols].values)\n\nX_train = np.array(X_embeddings)        # shape → (N, 1280)\ny_train = np.array(y_targets)           # shape → (N, 5)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T14:59:27.039208Z","iopub.execute_input":"2025-12-26T14:59:27.040317Z","iopub.status.idle":"2025-12-26T15:00:06.107404Z","shell.execute_reply.started":"2025-12-26T14:59:27.040270Z","shell.execute_reply":"2025-12-26T15:00:06.106305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xgb = XGBRegressor(\n    n_estimators=300,\n    max_depth=5,\n    learning_rate=0.05,\n    subsample=0.8,\n    colsample_bytree=0.8,\n    objective=\"reg:squarederror\",\n    random_state=42\n)\nmodel_xgb = MultiOutputRegressor(xgb)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:16:14.534556Z","iopub.execute_input":"2025-12-26T15:16:14.534970Z","iopub.status.idle":"2025-12-26T15:16:14.540926Z","shell.execute_reply.started":"2025-12-26T15:16:14.534933Z","shell.execute_reply":"2025-12-26T15:16:14.539936Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model_xgb.fit(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:16:17.917591Z","iopub.execute_input":"2025-12-26T15:16:17.918369Z","iopub.status.idle":"2025-12-26T15:18:20.319978Z","shell.execute_reply.started":"2025-12-26T15:16:17.918333Z","shell.execute_reply":"2025-12-26T15:18:20.318111Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_csv = \"/kaggle/input/csiro-biomass/test.csv\"\nimage_dir = \"/kaggle/input/csiro-biomass/\"\ntest_df = pd.read_csv(test_csv)\ntest_df[\"full_image_path\"] = image_dir + test_df[\"image_path\"]\ntest_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:44.596902Z","iopub.execute_input":"2025-12-26T15:19:44.598062Z","iopub.status.idle":"2025-12-26T15:19:44.617822Z","shell.execute_reply.started":"2025-12-26T15:19:44.598009Z","shell.execute_reply":"2025-12-26T15:19:44.616487Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_embeddings = []\nX_map = {}\nX_ids = []\n\nfor row in test_df.itertuples(index=False):\n    if not row.full_image_path in X_map:\n        emb = extract_embedding(row.full_image_path)\n        X_map[row.full_image_path] = emb\n        X_embeddings.append(emb)\n        X_ids.append(row.sample_id.split(\"__\")[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:45.967250Z","iopub.execute_input":"2025-12-26T15:19:45.967698Z","iopub.status.idle":"2025-12-26T15:19:46.115414Z","shell.execute_reply.started":"2025-12-26T15:19:45.967661Z","shell.execute_reply":"2025-12-26T15:19:46.114280Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = np.array(X_embeddings)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:50.356920Z","iopub.execute_input":"2025-12-26T15:19:50.358039Z","iopub.status.idle":"2025-12-26T15:19:50.363202Z","shell.execute_reply.started":"2025-12-26T15:19:50.357982Z","shell.execute_reply":"2025-12-26T15:19:50.362092Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_preds = model_xgb.predict(X_test)\ny_preds = np.maximum(y_preds, 0)\ny_preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:51.314982Z","iopub.execute_input":"2025-12-26T15:19:51.315433Z","iopub.status.idle":"2025-12-26T15:19:51.333546Z","shell.execute_reply.started":"2025-12-26T15:19:51.315363Z","shell.execute_reply":"2025-12-26T15:19:51.332020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_rows = []\nfor idx, y_pred in enumerate(y_preds):\n    target_cols = [\"Dry_Clover_g\", \"Dry_Dead_g\", \"Dry_Green_g\", \"Dry_Total_g\", \"GDM_g\"]\n    image_id = X_ids[idx]\n    for target_col, value in zip(target_cols, y_pred):\n        submission_rows.append({\n            'sample_id': image_id + '__' + target_col,\n            'target': value\n        })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:55.607976Z","iopub.execute_input":"2025-12-26T15:19:55.608371Z","iopub.status.idle":"2025-12-26T15:19:55.616515Z","shell.execute_reply.started":"2025-12-26T15:19:55.608339Z","shell.execute_reply":"2025-12-26T15:19:55.615097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df = pd.DataFrame(submission_rows)\nprint('submission_df-------------------', submission_df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:58.462553Z","iopub.execute_input":"2025-12-26T15:19:58.462939Z","iopub.status.idle":"2025-12-26T15:19:58.472051Z","shell.execute_reply.started":"2025-12-26T15:19:58.462907Z","shell.execute_reply":"2025-12-26T15:19:58.470642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:19:59.182541Z","iopub.execute_input":"2025-12-26T15:19:59.183114Z","iopub.status.idle":"2025-12-26T15:19:59.191583Z","shell.execute_reply.started":"2025-12-26T15:19:59.183040Z","shell.execute_reply":"2025-12-26T15:19:59.190535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('------------------Ready for submission-------------------')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:20:01.630313Z","iopub.execute_input":"2025-12-26T15:20:01.630737Z","iopub.status.idle":"2025-12-26T15:20:01.636647Z","shell.execute_reply.started":"2025-12-26T15:20:01.630703Z","shell.execute_reply":"2025-12-26T15:20:01.635447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-26T15:20:02.174097Z","iopub.execute_input":"2025-12-26T15:20:02.174562Z","iopub.status.idle":"2025-12-26T15:20:02.188704Z","shell.execute_reply.started":"2025-12-26T15:20:02.174524Z","shell.execute_reply":"2025-12-26T15:20:02.187433Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}