{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.12.12"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":112509,"databundleVersionId":14254895,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":14623760,"sourceType":"datasetVersion","datasetId":9340966},{"sourceId":14624410,"sourceType":"datasetVersion","datasetId":9341446},{"sourceId":14643586,"sourceType":"datasetVersion","datasetId":9354385},{"sourceId":14647206,"sourceType":"datasetVersion","datasetId":9356737},{"sourceId":14649539,"sourceType":"datasetVersion","datasetId":9358346}],"dockerImageVersionId":31260,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"comp = \"csiro-biomass\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:03.333496Z","iopub.execute_input":"2026-01-28T09:41:03.333733Z","iopub.status.idle":"2026-01-28T09:41:03.340471Z","shell.execute_reply.started":"2026-01-28T09:41:03.333694Z","shell.execute_reply":"2026-01-28T09:41:03.339503Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls /kaggle/input/fastkaggle-offline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:03.341889Z","iopub.execute_input":"2026-01-28T09:41:03.342166Z","iopub.status.idle":"2026-01-28T09:41:03.483760Z","shell.execute_reply.started":"2026-01-28T09:41:03.342145Z","shell.execute_reply":"2026-01-28T09:41:03.483072Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --no-index --find-links /kaggle/input/fastkaggle-offline fastkaggle","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:03.484911Z","iopub.execute_input":"2026-01-28T09:41:03.485176Z","iopub.status.idle":"2026-01-28T09:41:08.050749Z","shell.execute_reply.started":"2026-01-28T09:41:03.485151Z","shell.execute_reply":"2026-01-28T09:41:08.049915Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastkaggle import *\nfrom pathlib import Path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:08.052070Z","iopub.execute_input":"2026-01-28T09:41:08.052389Z","iopub.status.idle":"2026-01-28T09:41:08.153852Z","shell.execute_reply.started":"2026-01-28T09:41:08.052342Z","shell.execute_reply":"2026-01-28T09:41:08.153080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dset = setup_comp(comp)\ndset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:08.155005Z","iopub.execute_input":"2026-01-28T09:41:08.155434Z","iopub.status.idle":"2026-01-28T09:41:08.161550Z","shell.execute_reply.started":"2026-01-28T09:41:08.155405Z","shell.execute_reply":"2026-01-28T09:41:08.160712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !pip install -Uqq fastai","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:08.163100Z","iopub.execute_input":"2026-01-28T09:41:08.163314Z","iopub.status.idle":"2026-01-28T09:41:08.175505Z","shell.execute_reply.started":"2026-01-28T09:41:08.163293Z","shell.execute_reply":"2026-01-28T09:41:08.174784Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.vision.all import *","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:08.178265Z","iopub.execute_input":"2026-01-28T09:41:08.178571Z","iopub.status.idle":"2026-01-28T09:41:22.633339Z","shell.execute_reply.started":"2026-01-28T09:41:08.178549Z","shell.execute_reply":"2026-01-28T09:41:22.632738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"trn_path = dset/\"train\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.634158Z","iopub.execute_input":"2026-01-28T09:41:22.634383Z","iopub.status.idle":"2026-01-28T09:41:22.638182Z","shell.execute_reply.started":"2026-01-28T09:41:22.634349Z","shell.execute_reply":"2026-01-28T09:41:22.637511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(dset/'train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.639041Z","iopub.execute_input":"2026-01-28T09:41:22.639343Z","iopub.status.idle":"2026-01-28T09:41:22.702008Z","shell.execute_reply.started":"2026-01-28T09:41:22.639301Z","shell.execute_reply":"2026-01-28T09:41:22.701397Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head(20)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.702972Z","iopub.execute_input":"2026-01-28T09:41:22.703290Z","iopub.status.idle":"2026-01-28T09:41:22.731529Z","shell.execute_reply.started":"2026-01-28T09:41:22.703249Z","shell.execute_reply":"2026-01-28T09:41:22.730842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df[\"image_path\"] = str(dset)+\"/\"+train_df[\"image_path\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.732439Z","iopub.execute_input":"2026-01-28T09:41:22.732783Z","iopub.status.idle":"2026-01-28T09:41:22.738924Z","shell.execute_reply.started":"2026-01-28T09:41:22.732754Z","shell.execute_reply":"2026-01-28T09:41:22.738268Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df = train_df.groupby(\"image_path\").agg(list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.739834Z","iopub.execute_input":"2026-01-28T09:41:22.740160Z","iopub.status.idle":"2026-01-28T09:41:22.804844Z","shell.execute_reply.started":"2026-01-28T09:41:22.740132Z","shell.execute_reply":"2026-01-28T09:41:22.804009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df.head(5).iloc[0].target[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.805889Z","iopub.execute_input":"2026-01-28T09:41:22.806216Z","iopub.status.idle":"2026-01-28T09:41:22.811375Z","shell.execute_reply.started":"2026-01-28T09:41:22.806183Z","shell.execute_reply":"2026-01-28T09:41:22.810698Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df.reset_index().image_path","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.812340Z","iopub.execute_input":"2026-01-28T09:41:22.812654Z","iopub.status.idle":"2026-01-28T09:41:22.829398Z","shell.execute_reply.started":"2026-01-28T09:41:22.812625Z","shell.execute_reply":"2026-01-28T09:41:22.828622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df = agg_df.reset_index()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.830378Z","iopub.execute_input":"2026-01-28T09:41:22.830661Z","iopub.status.idle":"2026-01-28T09:41:22.844000Z","shell.execute_reply.started":"2026-01-28T09:41:22.830632Z","shell.execute_reply":"2026-01-28T09:41:22.843363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df['image_path']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.844757Z","iopub.execute_input":"2026-01-28T09:41:22.845046Z","iopub.status.idle":"2026-01-28T09:41:22.861510Z","shell.execute_reply.started":"2026-01-28T09:41:22.845018Z","shell.execute_reply":"2026-01-28T09:41:22.860896Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"agg_df.head(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.862403Z","iopub.execute_input":"2026-01-28T09:41:22.862802Z","iopub.status.idle":"2026-01-28T09:41:22.887533Z","shell.execute_reply.started":"2026-01-28T09:41:22.862770Z","shell.execute_reply":"2026-01-28T09:41:22.886891Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls = DataBlock(\n    # blocks=(ImageBlock,RegressionBlock,RegressionBlock,RegressionBlock,RegressionBlock,RegressionBlock),\n      blocks=(ImageBlock,RegressionBlock(n_out=5)),\n    n_inp=1,\n    get_items=lambda df: df.apply(lambda r: Path(r.image_path), axis=1),\n    get_y = lambda r: agg_df[agg_df.image_path == str(r)].target.item(),\n    # get_y = [\n    #     lambda r: agg_df[agg_df.image_path == str(r)].target.item()[0],\n    #     lambda r: agg_df[agg_df.image_path == str(r)].target.item()[1],\n    #     lambda r: agg_df[agg_df.image_path == str(r)].target.item()[2],\n    #     lambda r: agg_df[agg_df.image_path == str(r)].target.item()[3],\n    #     lambda r: agg_df[agg_df.image_path == str(r)].target.item()[4]\n    # ],\n    splitter=RandomSplitter(0.1, seed=42),\n    item_tfms=Resize(512, method='squish'),\n    # batch_tfms=aug_transforms(size=128, min_scale=0.75)\n).dataloaders(agg_df, bs = 16)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:22.888387Z","iopub.execute_input":"2026-01-28T09:41:22.888636Z","iopub.status.idle":"2026-01-28T09:41:23.424839Z","shell.execute_reply.started":"2026-01-28T09:41:22.888607Z","shell.execute_reply":"2026-01-28T09:41:23.424179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls.show_batch(max_n=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:23.425842Z","iopub.execute_input":"2026-01-28T09:41:23.426199Z","iopub.status.idle":"2026-01-28T09:41:25.100474Z","shell.execute_reply.started":"2026-01-28T09:41:23.426157Z","shell.execute_reply":"2026-01-28T09:41:25.099688Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch.nn.functional as F","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:25.101325Z","iopub.execute_input":"2026-01-28T09:41:25.101634Z","iopub.status.idle":"2026-01-28T09:41:25.105121Z","shell.execute_reply.started":"2026-01-28T09:41:25.101604Z","shell.execute_reply":"2026-01-28T09:41:25.104484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# def clfn(inp, r1, r2, r3, r4, r5):\n#     outp = torch.stack((r1,r2,r3,r4,r5), dim=1)\n#     return F.mse_loss(inp, outp)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:25.105896Z","iopub.execute_input":"2026-01-28T09:41:25.106233Z","iopub.status.idle":"2026-01-28T09:41:25.119041Z","shell.execute_reply.started":"2026-01-28T09:41:25.106203Z","shell.execute_reply":"2026-01-28T09:41:25.118391Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !ls /kaggle/input/convnext-fastai-pkl-csiro-biomass","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:25.121875Z","iopub.execute_input":"2026-01-28T09:41:25.122244Z","iopub.status.idle":"2026-01-28T09:41:25.132655Z","shell.execute_reply.started":"2026-01-28T09:41:25.122209Z","shell.execute_reply":"2026-01-28T09:41:25.132011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# class PrintProg(Callback):\n#     def after_epoch(self):\n#         print(f\"{self.epoch} finished\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:25.133417Z","iopub.execute_input":"2026-01-28T09:41:25.133638Z","iopub.status.idle":"2026-01-28T09:41:25.146477Z","shell.execute_reply.started":"2026-01-28T09:41:25.133613Z","shell.execute_reply":"2026-01-28T09:41:25.145738Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# old name\n# arch = 'convnext_small_in22k'\n# new name\n# arch = 'convnext_small.fb_in22k'\n# arch = 'vit_small_patch14_reg4_dinov2.lvd142m'\ncb = GradientAccumulation(32)\n# pp = PrintProg()\n# arch = 'vit_huge_plus_patch16_dinov3.lvd1689m'\n# arch = 'vit_small_patch16_dinov3.lvd1689m'\n# learn = load_learner(\"/kaggle/input/dinov2-fastai-pkl-csiro-biomass/dinov2-model.pkl\")\n# learn = load_learner(\"/kaggle/input/convnext-fastai-pkl-csiro-biomass/convnext-model.pkl\")\n# learn = load_learner(\"/kaggle/input/dinov3-fastai-pkl-csiro-biomass/dinov3-model.pkl\")\n# learn.cbs.append(pp)\narch = 'vit_base_patch16_dinov3.lvd1689m'\n# learn = vision_learner(dls, arch, metrics=rmse, n_out=5, cbs=[cb]).to_fp16()\n\nlearn = load_learner(\"/kaggle/input/dinov3-base-fastai-pkl-csiro-biomass/dinov3-base.pkl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:44.056224Z","iopub.execute_input":"2026-01-28T09:41:44.057000Z","iopub.status.idle":"2026-01-28T09:41:47.884900Z","shell.execute_reply.started":"2026-01-28T09:41:44.056924Z","shell.execute_reply":"2026-01-28T09:41:47.884108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learn.export(\"/kaggle/working/dinov3-base.pkl\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:46:39.811895Z","iopub.execute_input":"2026-01-28T09:46:39.812261Z","iopub.status.idle":"2026-01-28T09:46:40.280479Z","shell.execute_reply.started":"2026-01-28T09:46:39.812226Z","shell.execute_reply":"2026-01-28T09:46:40.279633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.dls = dls","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:50.563345Z","iopub.execute_input":"2026-01-28T09:41:50.563665Z","iopub.status.idle":"2026-01-28T09:41:50.567384Z","shell.execute_reply.started":"2026-01-28T09:41:50.563634Z","shell.execute_reply":"2026-01-28T09:41:50.566578Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls.bs","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:57.940787Z","iopub.execute_input":"2026-01-28T09:41:57.941429Z","iopub.status.idle":"2026-01-28T09:41:57.946484Z","shell.execute_reply.started":"2026-01-28T09:41:57.941394Z","shell.execute_reply":"2026-01-28T09:41:57.945798Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(dls.train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:41:59.021784Z","iopub.execute_input":"2026-01-28T09:41:59.022642Z","iopub.status.idle":"2026-01-28T09:41:59.027930Z","shell.execute_reply.started":"2026-01-28T09:41:59.022587Z","shell.execute_reply":"2026-01-28T09:41:59.027172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# learn.lr_find()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:42:01.572502Z","iopub.execute_input":"2026-01-28T09:42:01.572809Z","iopub.status.idle":"2026-01-28T09:42:01.576212Z","shell.execute_reply.started":"2026-01-28T09:42:01.572778Z","shell.execute_reply":"2026-01-28T09:42:01.575291Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.lr = 0.07","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:42:01.748912Z","iopub.execute_input":"2026-01-28T09:42:01.749729Z","iopub.status.idle":"2026-01-28T09:42:01.753173Z","shell.execute_reply.started":"2026-01-28T09:42:01.749692Z","shell.execute_reply":"2026-01-28T09:42:01.752485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn.fine_tune(100)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-01-28T09:42:01.896707Z","iopub.execute_input":"2026-01-28T09:42:01.897571Z","iopub.status.idle":"2026-01-28T09:43:25.689216Z","shell.execute_reply.started":"2026-01-28T09:42:01.897519Z","shell.execute_reply":"2026-01-28T09:43:25.688270Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test = pd.read_csv(dset/'test.csv')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test['image_path'] = str(dset)+\"/\"+test['image_path']","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_g = test.groupby(\"image_path\").agg(list).reset_index()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tdl = learn.dls.test_dl(test_g['image_path'])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds = learn.get_preds(dl=tdl)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### output is of the format (sample_id, target)","metadata":{}},{"cell_type":"code","source":"preds","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preds[0].flatten().shape","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test[\"target\"] = preds[0].flatten()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_path = Path(\"/kaggle/working\") if iskaggle else dset","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test[[\"sample_id\", \"target\"]].to_csv(sub_path/\"submission.csv\", index=False)","metadata":{"trusted":true},"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}]}