{"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"}],"dockerImageVersionId":31153,"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":"2025-11-01T05:52:11.528315Z","iopub.execute_input":"2025-11-01T05:52:11.528671Z","iopub.status.idle":"2025-11-01T05:52:12.220579Z","shell.execute_reply.started":"2025-11-01T05:52:11.528643Z","shell.execute_reply":"2025-11-01T05:52:12.219571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/csiro-biomass/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:53:55.153524Z","iopub.execute_input":"2025-11-01T05:53:55.154032Z","iopub.status.idle":"2025-11-01T05:53:55.183744Z","shell.execute_reply.started":"2025-11-01T05:53:55.154008Z","shell.execute_reply":"2025-11-01T05:53:55.182456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-31T15:45:41.794696Z","iopub.execute_input":"2025-10-31T15:45:41.795028Z","iopub.status.idle":"2025-10-31T15:45:41.834030Z","shell.execute_reply.started":"2025-10-31T15:45:41.795003Z","shell.execute_reply":"2025-10-31T15:45:41.832859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-31T15:48:14.203905Z","iopub.execute_input":"2025-10-31T15:48:14.204284Z","iopub.status.idle":"2025-10-31T15:48:14.531835Z","shell.execute_reply.started":"2025-10-31T15:48:14.204258Z","shell.execute_reply":"2025-10-31T15:48:14.530964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img=cv2.imread('/kaggle/input/csiro-biomass/train/ID1052620238.jpg')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-31T15:48:18.123822Z","iopub.execute_input":"2025-10-31T15:48:18.124119Z","iopub.status.idle":"2025-10-31T15:48:18.237714Z","shell.execute_reply.started":"2025-10-31T15:48:18.124097Z","shell.execute_reply":"2025-10-31T15:48:18.236731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\nplt.imshow(img_rgb)\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-31T15:48:39.564228Z","iopub.execute_input":"2025-10-31T15:48:39.564540Z","iopub.status.idle":"2025-10-31T15:48:40.138291Z","shell.execute_reply.started":"2025-10-31T15:48:39.564517Z","shell.execute_reply":"2025-10-31T15:48:40.137024Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_clover=df[df['target_name']==\"Dry_Clover_g\"]\ndf_dry_dead=df[df['target_name']==\"Dry_Dead_g\"]\ndf_dry_green=df[df['target_name']==\"Dry_Green_g\"]\ndf_dry_total=df[df['target_name']==\"Dry_Clover_g\"]\ndf_gdm=df[df['target_name']==\"GDM_g\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:18.003044Z","iopub.execute_input":"2025-11-01T05:54:18.003736Z","iopub.status.idle":"2025-11-01T05:54:18.022847Z","shell.execute_reply.started":"2025-11-01T05:54:18.003702Z","shell.execute_reply":"2025-11-01T05:54:18.021616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_clover[\"State\"].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-31T15:57:07.656387Z","iopub.execute_input":"2025-10-31T15:57:07.657109Z","iopub.status.idle":"2025-10-31T15:57:07.663940Z","shell.execute_reply.started":"2025-10-31T15:57:07.657080Z","shell.execute_reply":"2025-10-31T15:57:07.662960Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OneHotEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:29.654112Z","iopub.execute_input":"2025-11-01T05:54:29.654425Z","iopub.status.idle":"2025-11-01T05:54:30.274238Z","shell.execute_reply.started":"2025-11-01T05:54:29.654402Z","shell.execute_reply":"2025-11-01T05:54:30.273327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ohe=OneHotEncoder(drop='first',sparse_output=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:31.682673Z","iopub.execute_input":"2025-11-01T05:54:31.683983Z","iopub.status.idle":"2025-11-01T05:54:31.688786Z","shell.execute_reply.started":"2025-11-01T05:54:31.683945Z","shell.execute_reply":"2025-11-01T05:54:31.687661Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_clover_arr=ohe.fit_transform(df_dry_clover[[\"Species\",\"State\"]])\ndf_dry_clover_encoded=pd.DataFrame(\n    df_dry_clover_arr,\n    columns=ohe.get_feature_names_out([\"Species\",\"State\"])\n)\ndf_dry_clover_trans=pd.concat([df_dry_clover_encoded.reset_index(drop=True),\n           df_dry_clover[[\"Pre_GSHH_NDVI\",\"Height_Ave_cm\",\"image_path\",\"target\",]].reset_index(drop=True)],\n          axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:32.708440Z","iopub.execute_input":"2025-11-01T05:54:32.708777Z","iopub.status.idle":"2025-11-01T05:54:32.724890Z","shell.execute_reply.started":"2025-11-01T05:54:32.708755Z","shell.execute_reply":"2025-11-01T05:54:32.723845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_dead_arr=ohe.fit_transform(df_dry_dead[[\"Species\",\"State\"]])\ndf_dry_dead_encoded=pd.DataFrame(\n    df_dry_dead_arr,\n    columns=ohe.get_feature_names_out([\"Species\",\"State\"])\n)\ndf_dry_dead_trans=pd.concat([df_dry_dead_encoded.reset_index(drop=True),\n           df_dry_dead[[\"Pre_GSHH_NDVI\",\"Height_Ave_cm\",\"image_path\",\"target\",]].reset_index(drop=True)],\n          axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:37.503258Z","iopub.execute_input":"2025-11-01T05:54:37.503568Z","iopub.status.idle":"2025-11-01T05:54:37.516211Z","shell.execute_reply.started":"2025-11-01T05:54:37.503546Z","shell.execute_reply":"2025-11-01T05:54:37.515064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_green_arr=ohe.fit_transform(df_dry_green[[\"Species\",\"State\"]])\ndf_dry_green_encoded=pd.DataFrame(\n    df_dry_green_arr,\n    columns=ohe.get_feature_names_out([\"Species\",\"State\"])\n)\ndf_dry_green_trans=pd.concat([df_dry_green_encoded.reset_index(drop=True),\n           df_dry_green[[\"Pre_GSHH_NDVI\",\"Height_Ave_cm\",\"image_path\",\"target\",]].reset_index(drop=True)],\n          axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:39.718839Z","iopub.execute_input":"2025-11-01T05:54:39.719265Z","iopub.status.idle":"2025-11-01T05:54:39.732984Z","shell.execute_reply.started":"2025-11-01T05:54:39.719232Z","shell.execute_reply":"2025-11-01T05:54:39.731859Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_dry_total_arr=ohe.fit_transform(df_dry_total[[\"Species\",\"State\"]])\ndf_dry_total_encoded=pd.DataFrame(\n    df_dry_total_arr,\n    columns=ohe.get_feature_names_out([\"Species\",\"State\"])\n)\ndf_dry_total_trans=pd.concat([df_dry_total_encoded.reset_index(drop=True),\n           df_dry_total[[\"Pre_GSHH_NDVI\",\"Height_Ave_cm\",\"image_path\",\"target\",]].reset_index(drop=True)],\n          axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:46.043723Z","iopub.execute_input":"2025-11-01T05:54:46.044035Z","iopub.status.idle":"2025-11-01T05:54:46.055256Z","shell.execute_reply.started":"2025-11-01T05:54:46.044011Z","shell.execute_reply":"2025-11-01T05:54:46.054287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_gdm_arr=ohe.fit_transform(df_gdm[[\"Species\",\"State\"]])\ndf_gdm_encoded=pd.DataFrame(\n    df_gdm_arr,\n    columns=ohe.get_feature_names_out([\"Species\",\"State\"])\n)\ndf_gdm_trans=pd.concat([df_gdm_encoded.reset_index(drop=True),\n           df_gdm[[\"Pre_GSHH_NDVI\",\"Height_Ave_cm\",\"image_path\",\"target\",]].reset_index(drop=True)],\n          axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T05:54:48.302790Z","iopub.execute_input":"2025-11-01T05:54:48.303090Z","iopub.status.idle":"2025-11-01T05:54:48.315062Z","shell.execute_reply.started":"2025-11-01T05:54:48.303070Z","shell.execute_reply":"2025-11-01T05:54:48.313981Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torchvision import models, transforms\nfrom torchvision.models import EfficientNet_B0_Weights\nfrom PIL import Image\nimport tqdm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T06:44:01.346700Z","iopub.execute_input":"2025-11-01T06:44:01.347016Z","iopub.status.idle":"2025-11-01T06:44:01.351969Z","shell.execute_reply.started":"2025-11-01T06:44:01.346985Z","shell.execute_reply":"2025-11-01T06:44:01.350901Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights = EfficientNet_B0_Weights.IMAGENET1K_V1\nmodel = models.efficientnet_b0(weights=weights)\nmodel.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T06:10:46.103809Z","iopub.execute_input":"2025-11-01T06:10:46.104156Z","iopub.status.idle":"2025-11-01T06:10:46.288612Z","shell.execute_reply.started":"2025-11-01T06:10:46.104131Z","shell.execute_reply":"2025-11-01T06:10:46.287701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"extractor=torch.nn.Sequential(*(list(model.children())[:-1]))\npreprocessor=weights.transforms()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T06:17:02.171080Z","iopub.execute_input":"2025-11-01T06:17:02.171403Z","iopub.status.idle":"2025-11-01T06:17:02.176817Z","shell.execute_reply.started":"2025-11-01T06:17:02.171384Z","shell.execute_reply":"2025-11-01T06:17:02.175674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"features_list=[]\nfor path in df_dry_clover[\"image_path\"]:\n    img=Image.open(f\"/kaggle/input/csiro-biomass/{path}\").convert(\"RGB\")\n    input_tensor = preprocessor(img).unsqueeze(0)\n    with torch.no_grad():\n        features = extractor(input_tensor)\n    features=features.flatten(1)\n    features.numpy()\n    features_list.append(features.numpy())\nimg_features=np.vstack(features_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T07:18:33.217777Z","iopub.execute_input":"2025-11-01T07:18:33.218104Z","iopub.status.idle":"2025-11-01T07:19:14.158688Z","shell.execute_reply.started":"2025-11-01T07:18:33.218083Z","shell.execute_reply":"2025-11-01T07:19:14.157654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_features.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T07:19:34.717887Z","iopub.execute_input":"2025-11-01T07:19:34.718224Z","iopub.status.idle":"2025-11-01T07:19:34.725127Z","shell.execute_reply.started":"2025-11-01T07:19:34.718200Z","shell.execute_reply":"2025-11-01T07:19:34.723709Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dry_clover_final_features=np.hstack([img_features,df_dry_clover_trans.drop([\"target\",\"image_path\"],axis=1).to_numpy()])\ndry_clover_final_labels=df_dry_clover_trans[\"target\"].to_numpy().reshape(357,1)\n\ndry_dead_final_features=np.hstack([img_features,df_dry_dead_trans.drop([\"target\",\"image_path\"],axis=1).to_numpy()])\ndry_dead_final_labels=df_dry_dead_trans[\"target\"].to_numpy().reshape(357,1)\n\ndry_green_final_features=np.hstack([img_features,df_dry_green_trans.drop([\"target\",\"image_path\"],axis=1).to_numpy()])\ndry_green_final_labels=df_dry_green_trans[\"target\"].to_numpy().reshape(357,1)\n\ndry_total_final_features=np.hstack([img_features,df_dry_total_trans.drop([\"target\",\"image_path\"],axis=1).to_numpy()])\ndry_total_final_labels=df_dry_total_trans[\"target\"].to_numpy().reshape(357,1)\n\ngdm_final_features=np.hstack([img_features,df_gdm_trans.drop([\"target\",\"image_path\"],axis=1).to_numpy()])\ngdm_final_labels=df_gdm_trans[\"target\"].to_numpy().reshape(357,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-01T07:49:28.388742Z","iopub.execute_input":"2025-11-01T07:49:28.389073Z","iopub.status.idle":"2025-11-01T07:49:28.407892Z","shell.execute_reply.started":"2025-11-01T07:49:28.389049Z","shell.execute_reply":"2025-11-01T07:49:28.406699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}