{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceType":"competition","sourceId":117682,"databundleVersionId":15062069},{"sourceType":"datasetVersion","sourceId":14664660,"datasetId":9252923,"databundleVersionId":15506299},{"sourceType":"datasetVersion","sourceId":14989903,"datasetId":9147150,"databundleVersionId":15863985},{"sourceType":"datasetVersion","sourceId":14519097,"datasetId":9175808,"databundleVersionId":15346649},{"sourceType":"kernelVersion","sourceId":296766768},{"sourceType":"kernelVersion","sourceId":300182367}],"dockerImageVersionId":31236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"! pip install -q nnunetv2 --no-index --find-links=/kaggle/input/nnunetv2-libs","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:55:36.090055Z","iopub.execute_input":"2026-02-16T13:55:36.090309Z","iopub.status.idle":"2026-02-16T13:55:59.502353Z","shell.execute_reply.started":"2026-02-16T13:55:36.090289Z","shell.execute_reply":"2026-02-16T13:55:59.501454Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! cp -r /kaggle/input/vesuvius-src/kaggle-vesuvius-main .\n! ln -s /kaggle/input/vesuvius-challenge-surface-detection kaggle-vesuvius-main/input","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:55:59.503775Z","iopub.execute_input":"2026-02-16T13:55:59.504159Z","iopub.status.idle":"2026-02-16T13:56:00.132639Z","shell.execute_reply.started":"2026-02-16T13:55:59.504116Z","shell.execute_reply":"2026-02-16T13:56:00.131742Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# nnunetv2","metadata":{}},{"cell_type":"code","source":"! bash kaggle-vesuvius-main/nnunet/06_setup_custom_trainer.sh","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:56:00.133891Z","iopub.execute_input":"2026-02-16T13:56:00.134161Z","iopub.status.idle":"2026-02-16T13:56:00.375418Z","shell.execute_reply.started":"2026-02-16T13:56:00.134134Z","shell.execute_reply":"2026-02-16T13:56:00.374779Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! python kaggle-vesuvius-main/nnunet/07_prepare_test_input.py","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:56:00.376618Z","iopub.execute_input":"2026-02-16T13:56:00.376943Z","iopub.status.idle":"2026-02-16T13:56:01.049148Z","shell.execute_reply.started":"2026-02-16T13:56:00.376904Z","shell.execute_reply":"2026-02-16T13:56:01.048486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%env nnUNet_results=/kaggle/input/vesuvius-nnunetv2-models/nnUNet_results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:56:01.050228Z","iopub.execute_input":"2026-02-16T13:56:01.050518Z","iopub.status.idle":"2026-02-16T13:56:01.05519Z","shell.execute_reply.started":"2026-02-16T13:56:01.05048Z","shell.execute_reply":"2026-02-16T13:56:01.054484Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%bash\nset -u\n\nCUDA_VISIBLE_DEVICES=0 nnUNetv2_predict -i kaggle-vesuvius-main/nnunet/test_input -o /tmp/nnunetv2_results \\\n    -d Dataset001_Vesuvius \\\n    -c 3d_fullres -f all -p nnUNetResEncUNetXLPlans \\\n    -tr nnUNetTrainerMedialSurfaceRecallMixupNoDSRot90 -chk checkpoint_final.pth --save_probabilities \\\n    -part_id 0 -num_parts 2 > /tmp/nnunet_gpu0.log 2>&1 &\npid0=$!\n\nCUDA_VISIBLE_DEVICES=1 nnUNetv2_predict -i kaggle-vesuvius-main/nnunet/test_input -o /tmp/nnunetv2_results \\\n    -d Dataset001_Vesuvius \\\n    -c 3d_fullres -f all -p nnUNetResEncUNetXLPlans \\\n    -tr nnUNetTrainerMedialSurfaceRecallMixupNoDSRot90 -chk checkpoint_final.pth --save_probabilities \\\n    -part_id 1 -num_parts 2 > /tmp/nnunet_gpu1.log 2>&1 &\npid1=$!\n\nwait \"$pid0\"; s0=$?\nwait \"$pid1\"; s1=$?\n\necho \"exit: gpu0=$s0 gpu1=$s1\"\nif [ \"$s0\" -ne 0 ] || [ \"$s1\" -ne 0 ]; then\n  echo \"Logs: /tmp/nnunet_gpu0.log /tmp/nnunet_gpu1.log\" >&2\n  exit 1\nfi\necho \"done\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:56:01.066353Z","iopub.execute_input":"2026-02-16T13:56:01.066552Z","iopub.status.idle":"2026-02-16T13:58:08.735654Z","shell.execute_reply.started":"2026-02-16T13:56:01.066534Z","shell.execute_reply":"2026-02-16T13:58:08.735028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! python kaggle-vesuvius-main/49_npz_to_tif.py --input_dir /tmp/nnunetv2_results --output_dir /tmp/nnunetv2_results_tif\n! rm -rf /tmp/nnunetv2_results","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:58:08.736752Z","iopub.execute_input":"2026-02-16T13:58:08.737023Z","iopub.status.idle":"2026-02-16T13:58:10.941168Z","shell.execute_reply.started":"2026-02-16T13:58:08.737Z","shell.execute_reply":"2026-02-16T13:58:10.940417Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! python kaggle-vesuvius-main/nnunet/13_prepare_rot90_test_input.py","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T13:58:35.225142Z","iopub.execute_input":"2026-02-16T13:58:35.225451Z","iopub.status.idle":"2026-02-16T13:58:36.167733Z","shell.execute_reply.started":"2026-02-16T13:58:35.225421Z","shell.execute_reply":"2026-02-16T13:58:36.167097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%bash\nset -u\n\nCUDA_VISIBLE_DEVICES=0 nnUNetv2_predict -i kaggle-vesuvius-main/nnunet/test_input_rot90 -o /tmp/nnunetv2_results_rot90 \\\n    -d Dataset001_Vesuvius \\\n    -c 3d_fullres -f all -p nnUNetResEncUNetXLPlans \\\n    -tr nnUNetTrainerMedialSurfaceRecallMixupNoDSRot90 -chk checkpoint_final.pth --save_probabilities \\\n    -part_id 0 -num_parts 2 > /tmp/nnunet_gpu0.log 2>&1 &\npid0=$!\n\nCUDA_VISIBLE_DEVICES=1 nnUNetv2_predict -i kaggle-vesuvius-main/nnunet/test_input_rot90 -o /tmp/nnunetv2_results_rot90 \\\n    -d Dataset001_Vesuvius \\\n    -c 3d_fullres -f all -p nnUNetResEncUNetXLPlans \\\n    -tr nnUNetTrainerMedialSurfaceRecallMixupNoDSRot90 -chk checkpoint_final.pth --save_probabilities \\\n    -part_id 1 -num_parts 2 > /tmp/nnunet_gpu1.log 2>&1 &\npid1=$!\n\nwait \"$pid0\"; s0=$?\nwait \"$pid1\"; s1=$?\n\necho \"exit: gpu0=$s0 gpu1=$s1\"\nif [ \"$s0\" -ne 0 ] || [ \"$s1\" -ne 0 ]; then\n  echo \"Logs: /tmp/nnunet_gpu0.log /tmp/nnunet_gpu1.log\" >&2\n  exit 1\nfi\necho \"done\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:00:31.760718Z","iopub.execute_input":"2026-02-16T14:00:31.761406Z","iopub.status.idle":"2026-02-16T14:03:17.695318Z","shell.execute_reply.started":"2026-02-16T14:00:31.761373Z","shell.execute_reply":"2026-02-16T14:03:17.694751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! python kaggle-vesuvius-main/49_reverse_rot90_predictions.py --input_dir /tmp/nnunetv2_results_rot90 --output_dir /tmp/nnunetv2_results2_tif","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:04:00.47826Z","iopub.execute_input":"2026-02-16T14:04:00.478965Z","iopub.status.idle":"2026-02-16T14:04:02.580168Z","shell.execute_reply.started":"2026-02-16T14:04:00.478927Z","shell.execute_reply":"2026-02-16T14:04:02.579455Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! rm -rf /tmp/nnunetv2_results_rot90","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:05:13.558797Z","iopub.execute_input":"2026-02-16T14:05:13.559632Z","iopub.status.idle":"2026-02-16T14:05:13.695994Z","shell.execute_reply.started":"2026-02-16T14:05:13.559595Z","shell.execute_reply":"2026-02-16T14:05:13.694839Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from pathlib import Path\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport tifffile as tiff\n\nroot = Path(\"/tmp/nnunetv2_results_tif\")\ntif_path = sorted(root.glob(\"*.tif\"))[0]\nvol = tiff.imread(str(tif_path))\nimg = vol[vol.shape[0] // 2]\nplt.figure(figsize=(6, 6))\nplt.imshow(img, cmap=\"gray\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:05:54.060941Z","iopub.execute_input":"2026-02-16T14:05:54.061263Z","iopub.status.idle":"2026-02-16T14:05:54.167743Z","shell.execute_reply.started":"2026-02-16T14:05:54.061237Z","shell.execute_reply":"2026-02-16T14:05:54.167128Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Ensemble","metadata":{}},{"cell_type":"code","source":"! python kaggle-vesuvius-main/44_ensemble_multi.py \\\n    --pred_dirs /tmp/nnunetv2_results_tif /tmp/nnunetv2_results2_tif \\\n    --weights 0.5 0.5 \\\n    --output_dir /tmp/ensemble_preds \\\n    --threshold 0.7 --median_filter_iters 7","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:12.802947Z","iopub.execute_input":"2026-02-16T14:06:12.803605Z","iopub.status.idle":"2026-02-16T14:06:13.708483Z","shell.execute_reply.started":"2026-02-16T14:06:12.80358Z","shell.execute_reply":"2026-02-16T14:06:13.706972Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root = Path(\"/tmp/ensemble_preds\")\ntif_path = sorted(root.glob(\"*.tif\"))[0]\nvol = tiff.imread(tif_path)\nimg = vol[vol.shape[0] // 2]\nplt.figure(figsize=(6, 6))\nplt.imshow(img, cmap=\"gray\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:15.584246Z","iopub.execute_input":"2026-02-16T14:06:15.584993Z","iopub.status.idle":"2026-02-16T14:06:15.677432Z","shell.execute_reply.started":"2026-02-16T14:06:15.58496Z","shell.execute_reply":"2026-02-16T14:06:15.676857Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Postprocess","metadata":{}},{"cell_type":"code","source":"! python kaggle-vesuvius-main/45_postprocess.py --input_dir /tmp/ensemble_preds --output_dir /tmp/postprocessed_preds --min_size 1000 --fill_holes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:25.483545Z","iopub.execute_input":"2026-02-16T14:06:25.484126Z","iopub.status.idle":"2026-02-16T14:06:26.681764Z","shell.execute_reply.started":"2026-02-16T14:06:25.484093Z","shell.execute_reply":"2026-02-16T14:06:26.68109Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"root = Path(\"/tmp/postprocessed_preds\")\ntif_path = sorted(root.glob(\"*.tif\"))[0]\nvol = tiff.imread(tif_path)\nimg = vol[vol.shape[0] // 2]\nplt.figure(figsize=(6, 6))\nplt.imshow(img, cmap=\"gray\")\nplt.axis(\"off\")\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:36.060611Z","iopub.execute_input":"2026-02-16T14:06:36.061418Z","iopub.status.idle":"2026-02-16T14:06:36.153656Z","shell.execute_reply.started":"2026-02-16T14:06:36.061381Z","shell.execute_reply":"2026-02-16T14:06:36.15309Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Create Submission","metadata":{}},{"cell_type":"code","source":"from pathlib import Path\nimport zipfile\n\nzip_path = Path(\"/kaggle/working/submission.zip\")\ntarget_dir = Path(\"/tmp/postprocessed_preds\")  # ここを変更\n\ntif_paths = sorted(target_dir.glob(\"*.tif\"))\n\nwith zipfile.ZipFile(zip_path, \"w\", compression=zipfile.ZIP_DEFLATED) as z:\n    for p in tif_paths:\n        z.write(p, arcname=p.name)  # ZIP内はフラットにファイル名のみ\n\nprint(\"Submission ZIP:\", zip_path)\nprint(\"Number of files zipped:\", len(tif_paths))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:44.170152Z","iopub.execute_input":"2026-02-16T14:06:44.170742Z","iopub.status.idle":"2026-02-16T14:06:44.517572Z","shell.execute_reply.started":"2026-02-16T14:06:44.170716Z","shell.execute_reply":"2026-02-16T14:06:44.516987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"! rm -rf /tmp/nnunetv2_results_tif\n! rm -rf /tmp/nnunetv2_results2_tif\n! rm -rf /tmp/ensemble_preds\n! rm -rf /tmp/postprocessed_preds\n! rm -rf kaggle-vesuvius-main\n! rm /tmp/nnunet_*.log","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-16T14:06:50.575554Z","iopub.execute_input":"2026-02-16T14:06:50.575809Z","iopub.status.idle":"2026-02-16T14:06:51.269079Z","shell.execute_reply.started":"2026-02-16T14:06:50.57579Z","shell.execute_reply":"2026-02-16T14:06:51.268374Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}