{"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,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":4533,"sourceType":"modelInstanceVersion","isSourceIdPinned":false,"modelInstanceId":3325,"modelId":986}],"dockerImageVersionId":31192,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The notebook below contains some of the data visualization and data-preprocessing steps which could be taken into consideration while making predictions. At the end of the notebook, DINOv2 small model is used to generate feature maps of each individual images.\n\nPlease upvote the notebook if you find it helpful !","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport os\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport cv2\nfrom transformers import AutoImageProcessor, AutoModel\nfrom PIL import Image\nimport torch\nimport requests\nimport datetime\n\nMAIN_PATH = \"/kaggle/input/csiro-biomass\"\ndevice = \"cpu\"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:40.511614Z","iopub.execute_input":"2025-12-07T20:05:40.511949Z","iopub.status.idle":"2025-12-07T20:05:40.517485Z","shell.execute_reply.started":"2025-12-07T20:05:40.511921Z","shell.execute_reply":"2025-12-07T20:05:40.516311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def tensor_to_cv2(tensor):\n    if tensor.ndimension() == 4:\n        tensor = tensor.squeeze(0)\n    image = tensor.cpu().detach().numpy()\n    image = np.transpose(image, (1, 2, 0))\n    image = (image * 255).clip(0, 255).astype(np.uint8)\n    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n\n    return image","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:40.528397Z","iopub.execute_input":"2025-12-07T20:05:40.529412Z","iopub.status.idle":"2025-12-07T20:05:40.534828Z","shell.execute_reply.started":"2025-12-07T20:05:40.52938Z","shell.execute_reply":"2025-12-07T20:05:40.533939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/csiro-biomass/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/csiro-biomass/test.csv\")\nsample_data = pd.read_csv(\"/kaggle/input/csiro-biomass/sample_submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:40.544892Z","iopub.execute_input":"2025-12-07T20:05:40.545965Z","iopub.status.idle":"2025-12-07T20:05:40.565888Z","shell.execute_reply.started":"2025-12-07T20:05:40.545938Z","shell.execute_reply":"2025-12-07T20:05:40.565239Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, _ in train_data.iterrows():\n    train_data.loc[i, \"sample_id\"] = train_data.loc[i, \"sample_id\"].split(\"_\")[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:40.567136Z","iopub.execute_input":"2025-12-07T20:05:40.567396Z","iopub.status.idle":"2025-12-07T20:05:41.062688Z","shell.execute_reply.started":"2025-12-07T20:05:40.567376Z","shell.execute_reply":"2025-12-07T20:05:41.061762Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.head(20)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.063895Z","iopub.execute_input":"2025-12-07T20:05:41.064415Z","iopub.status.idle":"2025-12-07T20:05:41.080143Z","shell.execute_reply.started":"2025-12-07T20:05:41.06439Z","shell.execute_reply":"2025-12-07T20:05:41.078914Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.081288Z","iopub.execute_input":"2025-12-07T20:05:41.081696Z","iopub.status.idle":"2025-12-07T20:05:41.097546Z","shell.execute_reply.started":"2025-12-07T20:05:41.081668Z","shell.execute_reply":"2025-12-07T20:05:41.09655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.099559Z","iopub.execute_input":"2025-12-07T20:05:41.099801Z","iopub.status.idle":"2025-12-07T20:05:41.11785Z","shell.execute_reply.started":"2025-12-07T20:05:41.099782Z","shell.execute_reply":"2025-12-07T20:05:41.117081Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.11864Z","iopub.execute_input":"2025-12-07T20:05:41.118898Z","iopub.status.idle":"2025-12-07T20:05:41.138122Z","shell.execute_reply.started":"2025-12-07T20:05:41.118865Z","shell.execute_reply":"2025-12-07T20:05:41.137204Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.139049Z","iopub.execute_input":"2025-12-07T20:05:41.139439Z","iopub.status.idle":"2025-12-07T20:05:41.166497Z","shell.execute_reply.started":"2025-12-07T20:05:41.13941Z","shell.execute_reply":"2025-12-07T20:05:41.165707Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.histplot(train_data[\"Height_Ave_cm\"], kde=True, bins=30, color='blue')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.167623Z","iopub.execute_input":"2025-12-07T20:05:41.168098Z","iopub.status.idle":"2025-12-07T20:05:41.43179Z","shell.execute_reply.started":"2025-12-07T20:05:41.168078Z","shell.execute_reply":"2025-12-07T20:05:41.430899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.histplot(train_data[\"Pre_GSHH_NDVI\"], kde=True, bins=30, color='red')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.432684Z","iopub.execute_input":"2025-12-07T20:05:41.433022Z","iopub.status.idle":"2025-12-07T20:05:41.702503Z","shell.execute_reply.started":"2025-12-07T20:05:41.432974Z","shell.execute_reply":"2025-12-07T20:05:41.70167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_data[\"State\"].value_counts())\nprint(train_data[\"Species\"].value_counts())\nprint(train_data[\"target_name\"].value_counts())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.703389Z","iopub.execute_input":"2025-12-07T20:05:41.703644Z","iopub.status.idle":"2025-12-07T20:05:41.712099Z","shell.execute_reply.started":"2025-12-07T20:05:41.703626Z","shell.execute_reply":"2025-12-07T20:05:41.711078Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data[\"Sampling_Date\"] = pd.to_datetime(train_data[\"Sampling_Date\"], dayfirst=True, errors='coerce')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.714442Z","iopub.execute_input":"2025-12-07T20:05:41.714688Z","iopub.status.idle":"2025-12-07T20:05:41.729115Z","shell.execute_reply.started":"2025-12-07T20:05:41.714669Z","shell.execute_reply":"2025-12-07T20:05:41.72813Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.histplot(train_data[\"Sampling_Date\"].dt.month, kde=True, discrete=True, color='steelblue')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.730224Z","iopub.execute_input":"2025-12-07T20:05:41.730631Z","iopub.status.idle":"2025-12-07T20:05:41.976453Z","shell.execute_reply.started":"2025-12-07T20:05:41.73061Z","shell.execute_reply":"2025-12-07T20:05:41.975702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"processor = AutoImageProcessor.from_pretrained('/kaggle/input/dinov2/pytorch/small/1/')\nmodel = AutoModel.from_pretrained('/kaggle/input/dinov2/pytorch/small/1/')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:41.977193Z","iopub.execute_input":"2025-12-07T20:05:41.977458Z","iopub.status.idle":"2025-12-07T20:05:42.129408Z","shell.execute_reply.started":"2025-12-07T20:05:41.977431Z","shell.execute_reply":"2025-12-07T20:05:42.128455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.eval()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:42.13054Z","iopub.execute_input":"2025-12-07T20:05:42.13082Z","iopub.status.idle":"2025-12-07T20:05:42.138961Z","shell.execute_reply.started":"2025-12-07T20:05:42.1308Z","shell.execute_reply":"2025-12-07T20:05:42.138252Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cols = [c for c in train_data.columns if c not in [\"target_name\", \"target\"]]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:42.140112Z","iopub.execute_input":"2025-12-07T20:05:42.140838Z","iopub.status.idle":"2025-12-07T20:05:42.154373Z","shell.execute_reply.started":"2025-12-07T20:05:42.140807Z","shell.execute_reply":"2025-12-07T20:05:42.153412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_static = (\n    train_data[cols]\n      .drop_duplicates(subset=\"sample_id\")\n)\n\ndf_pivot = (\n    train_data.pivot(index=\"sample_id\", columns=\"target_name\", values=\"target\")\n      .rename(columns=lambda x: f\"{x}_target\")\n      .reset_index()\n)\n\ndf_final = df_static.merge(df_pivot, on=\"sample_id\", how=\"left\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:42.155266Z","iopub.execute_input":"2025-12-07T20:05:42.155583Z","iopub.status.idle":"2025-12-07T20:05:42.177919Z","shell.execute_reply.started":"2025-12-07T20:05:42.155557Z","shell.execute_reply":"2025-12-07T20:05:42.177174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_final","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:05:42.178879Z","iopub.execute_input":"2025-12-07T20:05:42.179172Z","iopub.status.idle":"2025-12-07T20:05:42.197892Z","shell.execute_reply.started":"2025-12-07T20:05:42.179152Z","shell.execute_reply":"2025-12-07T20:05:42.197131Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i, row in tqdm(df_final.iterrows(), total=len(df_final)):\n    if i == 4:\n        break # The following code can be manipulated to see more feature maps\n    img_path = os.path.join(MAIN_PATH, row[\"image_path\"])\n    print(img_path)\n    img = cv2.imread(img_path)\n    print(row)\n    image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    print(image.shape)\n    image = Image.fromarray(image)\n    with torch.no_grad():\n        inputs = processor(images=image, return_tensors=\"pt\").to(device)\n        outputs = model(**inputs, output_attentions=True)\n        attention = outputs.attentions[-1]\n        nh = attention.shape[1]\n\n        attentions_cls = attention[0, :, 0, 1:].mean(dim=0)\n    \n        h_featmap = inputs.pixel_values.shape[2] // 14\n        w_featmap = inputs.pixel_values.shape[3] // 14\n    \n        attentions_cls = attentions_cls.reshape(h_featmap, w_featmap)\n\n\n        attentions_cls = torch.nn.functional.interpolate(\n            attentions_cls.unsqueeze(0).unsqueeze(0), \n            size=(image.height, image.width), \n            mode=\"bicubic\"\n        ).squeeze().cpu().numpy()\n\n    plt.figure(figsize=(12, 6))\n    plt.subplot(1, 2, 1)\n    plt.title(\"Original Image\")\n    plt.imshow(image)\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.title(\"DINOv2 Attention Heatmap\")\n    plt.imshow(image)\n    plt.imshow(attentions_cls, cmap='inferno', alpha=0.6)\n    plt.axis('off')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-07T20:07:39.363328Z","iopub.execute_input":"2025-12-07T20:07:39.364208Z","iopub.status.idle":"2025-12-07T20:07:43.511178Z","shell.execute_reply.started":"2025-12-07T20:07:39.364179Z","shell.execute_reply":"2025-12-07T20:07:43.510068Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}