{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":31192,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.549123Z","iopub.execute_input":"2025-12-02T19:35:20.54961Z","iopub.status.idle":"2025-12-02T19:35:20.55432Z","shell.execute_reply.started":"2025-12-02T19:35:20.549576Z","shell.execute_reply":"2025-12-02T19:35:20.55331Z"}},"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-12-02T19:35:20.556372Z","iopub.execute_input":"2025-12-02T19:35:20.556901Z","iopub.status.idle":"2025-12-02T19:35:20.58851Z","shell.execute_reply.started":"2025-12-02T19:35:20.55687Z","shell.execute_reply":"2025-12-02T19:35:20.587561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.589585Z","iopub.execute_input":"2025-12-02T19:35:20.589852Z","iopub.status.idle":"2025-12-02T19:35:20.606454Z","shell.execute_reply.started":"2025-12-02T19:35:20.58983Z","shell.execute_reply":"2025-12-02T19:35:20.60528Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Assume df has columns: sample_id, image_path, target_name, target\n\n# Extract image ID\ndf['image_id'] = df['sample_id'].apply(lambda x: x.split('__')[0])\n\n# Pivot to have one row per image and one column per target\ndf_pivot = df.pivot_table(index='image_id', \n                          columns='target_name', \n                          values='target').reset_index()\n\nprint(df_pivot.columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.607654Z","iopub.execute_input":"2025-12-02T19:35:20.607988Z","iopub.status.idle":"2025-12-02T19:35:20.633254Z","shell.execute_reply.started":"2025-12-02T19:35:20.607958Z","shell.execute_reply":"2025-12-02T19:35:20.632152Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport cv2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.635768Z","iopub.execute_input":"2025-12-02T19:35:20.636639Z","iopub.status.idle":"2025-12-02T19:35:20.644861Z","shell.execute_reply.started":"2025-12-02T19:35:20.636609Z","shell.execute_reply":"2025-12-02T19:35:20.64375Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfolder_path = r\"/kaggle/input/csiro-biomass/train\"\nprint(\"Files in folder\")\nimage=os.listdir(folder_path)\n# print(image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.646279Z","iopub.execute_input":"2025-12-02T19:35:20.646668Z","iopub.status.idle":"2025-12-02T19:35:20.669926Z","shell.execute_reply.started":"2025-12-02T19:35:20.646642Z","shell.execute_reply":"2025-12-02T19:35:20.668858Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"os.path.exists(\"/kaggle/input/csiro-biomass/train\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.670977Z","iopub.execute_input":"2025-12-02T19:35:20.671377Z","iopub.status.idle":"2025-12-02T19:35:20.69069Z","shell.execute_reply.started":"2025-12-02T19:35:20.671326Z","shell.execute_reply":"2025-12-02T19:35:20.689741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"images = []\nimage_ids = []\nfolder_path = r\"/kaggle/input/csiro-biomass/train\"\nfor filename in os.listdir(folder_path):\n    if filename.endswith(('.jpg', '.png')):\n        path = os.path.join(folder_path, filename)\n        img = cv2.imread(path)\n        img = cv2.resize(img, (256,256))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = img.astype('float32') / 255.0\n        images.append(img)\n        image_ids.append(filename.split('.')[0])\n\n\nX = np.array(images)\nprint(\"✅ Image data shape:\", X.shape)","metadata":{"scrolled":true,"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:20.691676Z","iopub.execute_input":"2025-12-02T19:35:20.692008Z","iopub.status.idle":"2025-12-02T19:35:39.594433Z","shell.execute_reply.started":"2025-12-02T19:35:20.691979Z","shell.execute_reply":"2025-12-02T19:35:39.593104Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Show the first image\nplt.imshow(X[0])\nplt.title(image_ids[0])\nplt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.595534Z","iopub.execute_input":"2025-12-02T19:35:39.595908Z","iopub.status.idle":"2025-12-02T19:35:39.774513Z","shell.execute_reply.started":"2025-12-02T19:35:39.595877Z","shell.execute_reply":"2025-12-02T19:35:39.773275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_sorted = df_pivot.sort_values(by='image_id').reset_index(drop=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.775564Z","iopub.execute_input":"2025-12-02T19:35:39.775904Z","iopub.status.idle":"2025-12-02T19:35:39.783868Z","shell.execute_reply.started":"2025-12-02T19:35:39.775877Z","shell.execute_reply":"2025-12-02T19:35:39.782453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df=df_sorted.drop([\"image_id\"],axis=1)\ny=df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.784931Z","iopub.execute_input":"2025-12-02T19:35:39.785258Z","iopub.status.idle":"2025-12-02T19:35:39.804429Z","shell.execute_reply.started":"2025-12-02T19:35:39.78523Z","shell.execute_reply":"2025-12-02T19:35:39.803395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.805507Z","iopub.execute_input":"2025-12-02T19:35:39.80581Z","iopub.status.idle":"2025-12-02T19:35:39.833444Z","shell.execute_reply.started":"2025-12-02T19:35:39.805789Z","shell.execute_reply":"2025-12-02T19:35:39.832469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.835976Z","iopub.execute_input":"2025-12-02T19:35:39.83678Z","iopub.status.idle":"2025-12-02T19:35:39.853796Z","shell.execute_reply.started":"2025-12-02T19:35:39.836745Z","shell.execute_reply":"2025-12-02T19:35:39.852777Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras import layers,models\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nimport os\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \nimport tensorflow as tf","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.855122Z","iopub.execute_input":"2025-12-02T19:35:39.855445Z","iopub.status.idle":"2025-12-02T19:35:39.876014Z","shell.execute_reply.started":"2025-12-02T19:35:39.855421Z","shell.execute_reply":"2025-12-02T19:35:39.874763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(256,256,3)))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.25))  # Dropout added here\n\nmodel.add(layers.Conv2D(64, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.25))\n\nmodel.add(layers.Conv2D(128, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.3))\n\nmodel.add(layers.Conv2D(256, (3,3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2,2)))\nmodel.add(layers.Dropout(0.4))\n\n# --- Regression Head ---\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dropout(0.4))\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0.3))\nmodel.add(layers.Dense(5, activation='linear'))  # 5 regression outputs\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:39.877494Z","iopub.execute_input":"2025-12-02T19:35:39.877834Z","iopub.status.idle":"2025-12-02T19:35:40.144095Z","shell.execute_reply.started":"2025-12-02T19:35:39.877806Z","shell.execute_reply":"2025-12-02T19:35:40.142765Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='mean_squared_error',  # good for regression\n    metrics=['mae']             # Mean Absolute Error\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:40.145572Z","iopub.execute_input":"2025-12-02T19:35:40.146237Z","iopub.status.idle":"2025-12-02T19:35:40.155685Z","shell.execute_reply.started":"2025-12-02T19:35:40.146206Z","shell.execute_reply":"2025-12-02T19:35:40.154454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X, y,\n    epochs=4,           # increase if loss is still high\n    batch_size=8,        # adjust based on your GPU memory  \n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:35:40.157072Z","iopub.execute_input":"2025-12-02T19:35:40.15742Z","iopub.status.idle":"2025-12-02T19:37:36.443926Z","shell.execute_reply.started":"2025-12-02T19:35:40.157387Z","shell.execute_reply":"2025-12-02T19:37:36.442821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"img_path = r\"/kaggle/input/csiro-biomass/test\"\n\nfor filename in os.listdir(folder_path):\n    if filename.endswith(('.jpg', '.png')):\n        path = os.path.join(folder_path, filename)\n        img = cv2.imread(path)\n        img = cv2.resize(img, (256, 256))\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = img.astype('float32') / 255.0\n\n\nimg = np.expand_dims(img, axis=0)   # shape becomes (1, 256, 256, 3)\n\n\nprediction = model.predict(img)\nprint(\"Predicted values:\", prediction)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:37:36.445441Z","iopub.execute_input":"2025-12-02T19:37:36.44576Z","iopub.status.idle":"2025-12-02T19:37:55.597662Z","shell.execute_reply.started":"2025-12-02T19:37:36.445721Z","shell.execute_reply":"2025-12-02T19:37:55.596498Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"folder_path = \"/kaggle/input/csiro-biomass/test\"\nimage_ids = [f for f in os.listdir(folder_path) \n             if f.lower().endswith(('.jpg', '.jpeg', '.png'))]\n\nprint(image_ids)\nname = [os.path.splitext(f)[0] for f in image_ids]\n\nprint(name)\nfor i in name:\n    print(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:37:55.598686Z","iopub.execute_input":"2025-12-02T19:37:55.599018Z","iopub.status.idle":"2025-12-02T19:37:55.606154Z","shell.execute_reply.started":"2025-12-02T19:37:55.598995Z","shell.execute_reply":"2025-12-02T19:37:55.605191Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"columns = ['Dry_Clover_g', 'Dry_Dead_g', 'Dry_Green_g', 'Dry_Total_g', 'GDM_g']\n\n# Create dictionary with file name prefix\ntable = {f\"{i}__{col}\": val for col, val in zip(columns, prediction[0])}\n\n# Convert dictionary to DataFrame (two columns)\ndf = pd.DataFrame((table.items()), columns=[\"sample_id\", \"target\"])\n\ndf = df[[\"sample_id\",\"target\"]]\n# Display result\nprint(df)\ndf.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:37:55.607099Z","iopub.execute_input":"2025-12-02T19:37:55.608164Z","iopub.status.idle":"2025-12-02T19:37:55.630855Z","shell.execute_reply.started":"2025-12-02T19:37:55.608134Z","shell.execute_reply":"2025-12-02T19:37:55.629891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.read_csv(\"submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T19:37:55.631627Z","iopub.execute_input":"2025-12-02T19:37:55.631938Z","iopub.status.idle":"2025-12-02T19:37:55.664757Z","shell.execute_reply.started":"2025-12-02T19:37:55.631916Z","shell.execute_reply":"2025-12-02T19:37:55.663669Z"}},"outputs":[],"execution_count":null}]}