{"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":31155,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Loading Data\n**in this section we will import some basic library's and load the dataset.**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:23:53.903401Z","iopub.execute_input":"2025-10-30T10:23:53.903910Z","iopub.status.idle":"2025-10-30T10:23:57.416449Z","shell.execute_reply.started":"2025-10-30T10:23:53.903885Z","shell.execute_reply":"2025-10-30T10:23:57.415928Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/csiro-biomass/train.csv')\ntest_df = pd.read_csv('/kaggle/input/csiro-biomass/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:00.530963Z","iopub.execute_input":"2025-10-30T10:24:00.531355Z","iopub.status.idle":"2025-10-30T10:24:00.559570Z","shell.execute_reply.started":"2025-10-30T10:24:00.531332Z","shell.execute_reply":"2025-10-30T10:24:00.558721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:01.040782Z","iopub.execute_input":"2025-10-30T10:24:01.041370Z","iopub.status.idle":"2025-10-30T10:24:01.067885Z","shell.execute_reply.started":"2025-10-30T10:24:01.041344Z","shell.execute_reply":"2025-10-30T10:24:01.067199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:03.587038Z","iopub.execute_input":"2025-10-30T10:24:03.587610Z","iopub.status.idle":"2025-10-30T10:24:03.595526Z","shell.execute_reply.started":"2025-10-30T10:24:03.587588Z","shell.execute_reply":"2025-10-30T10:24:03.594667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:03.750473Z","iopub.execute_input":"2025-10-30T10:24:03.750725Z","iopub.status.idle":"2025-10-30T10:24:03.755640Z","shell.execute_reply.started":"2025-10-30T10:24:03.750705Z","shell.execute_reply":"2025-10-30T10:24:03.754918Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:26:25.992465Z","iopub.execute_input":"2025-10-30T10:26:25.993054Z","iopub.status.idle":"2025-10-30T10:26:25.997859Z","shell.execute_reply.started":"2025-10-30T10:26:25.993031Z","shell.execute_reply":"2025-10-30T10:26:25.997108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:26:40.727225Z","iopub.execute_input":"2025-10-30T10:26:40.727974Z","iopub.status.idle":"2025-10-30T10:26:40.743135Z","shell.execute_reply.started":"2025-10-30T10:26:40.727949Z","shell.execute_reply":"2025-10-30T10:26:40.742146Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:27:13.024119Z","iopub.execute_input":"2025-10-30T10:27:13.024922Z","iopub.status.idle":"2025-10-30T10:27:13.040799Z","shell.execute_reply.started":"2025-10-30T10:27:13.024887Z","shell.execute_reply":"2025-10-30T10:27:13.040174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df['image_path'] = train_df['image_path'].apply(lambda x: os.path.join('/kaggle/input/csiro-biomass/', x))\ntest_df['image_path'] = test_df['image_path'].apply(lambda x: os.path.join('/kaggle/input/csiro-biomass/', x))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:05.840890Z","iopub.execute_input":"2025-10-30T10:24:05.841416Z","iopub.status.idle":"2025-10-30T10:24:05.850185Z","shell.execute_reply.started":"2025-10-30T10:24:05.841392Z","shell.execute_reply":"2025-10-30T10:24:05.849274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(train_df['image_path'].apply(os.path.exists).value_counts())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:06.100993Z","iopub.execute_input":"2025-10-30T10:24:06.101556Z","iopub.status.idle":"2025-10-30T10:24:06.469157Z","shell.execute_reply.started":"2025-10-30T10:24:06.101536Z","shell.execute_reply":"2025-10-30T10:24:06.468437Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from PIL import Image\nsample_img = train_df.iloc[0]['image_path']\nimg = Image.open(sample_img)\nplt.imshow(img)\nplt.title(train_df.iloc[0]['target_name'])\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:24:08.720679Z","iopub.execute_input":"2025-10-30T10:24:08.721355Z","iopub.status.idle":"2025-10-30T10:24:09.334050Z","shell.execute_reply.started":"2025-10-30T10:24:08.721333Z","shell.execute_reply":"2025-10-30T10:24:09.333251Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Visualizing the Data","metadata":{}},{"cell_type":"code","source":"sns.histplot(train_df['target'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:25:50.160699Z","iopub.execute_input":"2025-10-30T10:25:50.161440Z","iopub.status.idle":"2025-10-30T10:25:50.375685Z","shell.execute_reply.started":"2025-10-30T10:25:50.161415Z","shell.execute_reply":"2025-10-30T10:25:50.374977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.pairplot(data = train_df[['Pre_GSHH_NDVI','Height_Ave_cm','target']])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:35:37.580062Z","iopub.execute_input":"2025-10-30T10:35:37.580622Z","iopub.status.idle":"2025-10-30T10:35:39.763312Z","shell.execute_reply.started":"2025-10-30T10:35:37.580602Z","shell.execute_reply":"2025-10-30T10:35:39.762576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.heatmap(train_df.corr(numeric_only=True),annot=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:37:05.412554Z","iopub.execute_input":"2025-10-30T10:37:05.412952Z","iopub.status.idle":"2025-10-30T10:37:05.634781Z","shell.execute_reply.started":"2025-10-30T10:37:05.412925Z","shell.execute_reply":"2025-10-30T10:37:05.634010Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\n\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=20,        \n    width_shift_range=0.15,\n    height_shift_range=0.15,  \n    zoom_range=0.2,       \n    shear_range=0.15,          \n    horizontal_flip=True,      \n    brightness_range=[0.8, 1.2], \n    fill_mode='nearest',      \n    validation_split=0.2      \n)\n\n# Training generator\ntrain_gen = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='image_path',\n    y_col='target',\n    target_size=(128, 128),\n    batch_size=32,\n    class_mode='raw',\n    subset='training',\n    shuffle=True\n)\n\nval_datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)\n\nval_gen = val_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    x_col='image_path',\n    y_col='target',\n    target_size=(128, 128),\n    batch_size=32,\n    class_mode='raw',\n    subset='validation',\n    shuffle=False\n)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T11:02:18.275828Z","iopub.execute_input":"2025-10-30T11:02:18.276084Z","iopub.status.idle":"2025-10-30T11:02:18.521443Z","shell.execute_reply.started":"2025-10-30T11:02:18.276066Z","shell.execute_reply":"2025-10-30T11:02:18.520795Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Training The Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,Flatten,Dropout,MaxPooling2D,Dense\nmodel = Sequential([\n    Conv2D(32,kernel_size=(3,3),activation='relu',input_shape=(128,128,3)),\n    MaxPooling2D(pool_size=(2,2)),\n\n    Conv2D(32,kernel_size=(3,3),activation='relu'),\n    MaxPooling2D(pool_size=(2,2)),\n\n    Flatten(),\n    Dense(64,activation='relu'),\n    Dropout(0.1),\n    Dense(1)\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:38:32.848062Z","iopub.execute_input":"2025-10-30T10:38:32.848666Z","iopub.status.idle":"2025-10-30T10:38:35.308018Z","shell.execute_reply.started":"2025-10-30T10:38:32.848645Z","shell.execute_reply":"2025-10-30T10:38:35.307440Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='mse',metrics=[tf.keras.metrics.R2Score()])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:39:02.821974Z","iopub.execute_input":"2025-10-30T10:39:02.822241Z","iopub.status.idle":"2025-10-30T10:39:02.837974Z","shell.execute_reply.started":"2025-10-30T10:39:02.822222Z","shell.execute_reply":"2025-10-30T10:39:02.837136Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:39:05.992237Z","iopub.execute_input":"2025-10-30T10:39:05.992996Z","iopub.status.idle":"2025-10-30T10:39:06.010383Z","shell.execute_reply.started":"2025-10-30T10:39:05.992973Z","shell.execute_reply":"2025-10-30T10:39:06.009547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(train_gen,validation_data=val_gen,epochs=20,batch_size=32,verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:39:09.512910Z","iopub.execute_input":"2025-10-30T10:39:09.513661Z","iopub.status.idle":"2025-10-30T10:53:08.428564Z","shell.execute_reply.started":"2025-10-30T10:39:09.513626Z","shell.execute_reply":"2025-10-30T10:53:08.427977Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Prediction for test file","metadata":{}},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_gen = test_datagen.flow_from_dataframe(\n    dataframe=test_df,\n    x_col='image_path',\n    y_col=None,\n    target_size=(128, 128),\n    batch_size=32,\n    class_mode=None,\n    shuffle=False\n)\n\npreds = model.predict(test_gen)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:55:21.621715Z","iopub.execute_input":"2025-10-30T10:55:21.622354Z","iopub.status.idle":"2025-10-30T10:55:22.500669Z","shell.execute_reply.started":"2025-10-30T10:55:21.622332Z","shell.execute_reply":"2025-10-30T10:55:22.500094Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Saving the CSV file","metadata":{}},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'sample_id':test_df['sample_id'],\n    'target': preds.flatten()\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:58:25.202596Z","iopub.execute_input":"2025-10-30T10:58:25.203199Z","iopub.status.idle":"2025-10-30T10:58:25.207185Z","shell.execute_reply.started":"2025-10-30T10:58:25.203176Z","shell.execute_reply":"2025-10-30T10:58:25.206497Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-30T10:59:39.219695Z","iopub.execute_input":"2025-10-30T10:59:39.219981Z","iopub.status.idle":"2025-10-30T10:59:39.230626Z","shell.execute_reply.started":"2025-10-30T10:59:39.219962Z","shell.execute_reply":"2025-10-30T10:59:39.229964Z"}},"outputs":[],"execution_count":null}]}