{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":112509,"databundleVersionId":14254895,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":14019449,"sourceType":"datasetVersion","datasetId":8780493,"isSourceIdPinned":true}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, BatchNormalization, Flatten, Dense\nfrom tensorflow.keras import backend\nimport os\nimport joblib\nimport gc\n#import shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:00:51.878722Z","iopub.execute_input":"2025-12-12T04:00:51.879123Z","iopub.status.idle":"2025-12-12T04:01:08.178099Z","shell.execute_reply.started":"2025-12-12T04:00:51.879079Z","shell.execute_reply":"2025-12-12T04:01:08.177235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/csiro-biomass/test.csv')\ntargets = test['target_name'].unique()\nprint(targets)\ntest","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:08.179285Z","iopub.execute_input":"2025-12-12T04:01:08.179781Z","iopub.status.idle":"2025-12-12T04:01:08.217350Z","shell.execute_reply.started":"2025-12-12T04:01:08.179760Z","shell.execute_reply":"2025-12-12T04:01:08.216609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# only predict component values\ntargets2 = ['Dry_Clover_g','Dry_Dead_g','Dry_Green_g']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:08.218005Z","iopub.execute_input":"2025-12-12T04:01:08.218208Z","iopub.status.idle":"2025-12-12T04:01:08.221970Z","shell.execute_reply.started":"2025-12-12T04:01:08.218192Z","shell.execute_reply":"2025-12-12T04:01:08.221148Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_reformat = test[['image_path']].drop_duplicates()\ntest_reformat","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:08.223404Z","iopub.execute_input":"2025-12-12T04:01:08.223706Z","iopub.status.idle":"2025-12-12T04:01:08.249047Z","shell.execute_reply.started":"2025-12-12T04:01:08.223684Z","shell.execute_reply":"2025-12-12T04:01:08.248279Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = joblib.load('/kaggle/input/csiro-image2biomass-models-v02/model_all.joblib')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:08.249633Z","iopub.execute_input":"2025-12-12T04:01:08.249797Z","iopub.status.idle":"2025-12-12T04:01:12.620511Z","shell.execute_reply.started":"2025-12-12T04:01:08.249784Z","shell.execute_reply":"2025-12-12T04:01:12.619876Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_datagen = ImageDataGenerator(rescale=1./255)\n\ntest_loader = test_datagen.flow_from_dataframe(\n    dataframe = test_reformat,\n    directory = '/kaggle/input/csiro-biomass',\n    x_col = 'image_path',\n    batch_size = 32,\n    seed = 1,\n    shuffle = False,\n    class_mode = None,\n    target_size = (224,224)\n    )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:12.621618Z","iopub.execute_input":"2025-12-12T04:01:12.621875Z","iopub.status.idle":"2025-12-12T04:01:12.629630Z","shell.execute_reply.started":"2025-12-12T04:01:12.621853Z","shell.execute_reply":"2025-12-12T04:01:12.629070Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions = model.predict(test_loader)\nprint(predictions)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:12.630430Z","iopub.execute_input":"2025-12-12T04:01:12.630975Z","iopub.status.idle":"2025-12-12T04:01:20.487566Z","shell.execute_reply.started":"2025-12-12T04:01:12.630947Z","shell.execute_reply":"2025-12-12T04:01:20.486883Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Dry_Total_g = Dry_Clover_g + Dry_Dead_g + Dry_Green_g\n\nGDM_g = Dry_Clover_g + Dry_Green_g \n\nDry_Total_g = GDM_g + Dry_Dead_g","metadata":{}},{"cell_type":"code","source":"dry_clover_g_pred = [max(float(x),0.0) for x in predictions[:, 0]]\ndry_dead_g_pred = [max(float(x),0.0) for x in predictions[:, 1]]\ndry_green_g_pred = [max(float(x),0.0) for x in predictions[:, 2]]\ndry_total_g_pred = [x+y+z for x,y,z in zip(dry_clover_g_pred,dry_dead_g_pred,dry_green_g_pred)]\ngdm_g_pred = [x+y for x,y in zip(dry_clover_g_pred,dry_green_g_pred)]\n\npred_df = pd.DataFrame(\n    {\n        'image_path':test_reformat['image_path'],\n        'Dry_Clover_g':dry_clover_g_pred,\n        'Dry_Dead_g':dry_dead_g_pred,\n        'Dry_Green_g':dry_green_g_pred,\n        'Dry_Total_g':dry_total_g_pred,\n        'GDM_g':gdm_g_pred\n    }\n)\n\npred_df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:20.488371Z","iopub.execute_input":"2025-12-12T04:01:20.488936Z","iopub.status.idle":"2025-12-12T04:01:20.500370Z","shell.execute_reply.started":"2025-12-12T04:01:20.488886Z","shell.execute_reply":"2025-12-12T04:01:20.499733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame(columns=['sample_id','target'])\n\nfor t in targets:\n    test_target = test[test['target_name']==t]\n    pred_target = pred_df[['image_path',t]]\n    submission_target = test_target.merge(pred_target, on='image_path', how='left')\n    submission_target = submission_target[['sample_id',t]]\n    submission_target.rename(columns={t:'target'}, inplace=True)\n    submission = pd.concat([submission, submission_target], ignore_index=True)\n\nsubmission","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:20.501164Z","iopub.execute_input":"2025-12-12T04:01:20.501370Z","iopub.status.idle":"2025-12-12T04:01:20.540058Z","shell.execute_reply.started":"2025-12-12T04:01:20.501349Z","shell.execute_reply":"2025-12-12T04:01:20.539381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', header=True, index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-12T04:01:20.541577Z","iopub.execute_input":"2025-12-12T04:01:20.541816Z","iopub.status.idle":"2025-12-12T04:01:20.549107Z","shell.execute_reply.started":"2025-12-12T04:01:20.541801Z","shell.execute_reply":"2025-12-12T04:01:20.548442Z"}},"outputs":[],"execution_count":null}]}