{"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":117682,"databundleVersionId":15062069,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":14245247,"sourceType":"datasetVersion","datasetId":9088503},{"sourceId":14261774,"sourceType":"datasetVersion","datasetId":9100515},{"sourceId":14295835,"sourceType":"datasetVersion","datasetId":9125518}],"dockerImageVersionId":31193,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nOnlyInfKernel\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"### **ℹ️INFO**\n* First, I want to thank you for sharing such a strong baseline.\n    * [@INNAT, LB.507, Vesuvius Surface 3D Detection](https://www.kaggle.com/code/ipythonx/inference-vesuvius-surface-3d-detection?scriptVersionId=285843716)\n \n### **ℹ️[LB.454 2025/12/26]MyUpdate**(※Please note that this model was trained on data before the Dataset update on Dec 23, 2025)\n* Results of improving the models and train pipeline available in medic-ai.\n* Since good CV/LB was obtained, we will share the model weights together.\n    * **Model: TransUNet**\n    * **Encoder: SEResNeXt1013D**\n    * **Local Validation(DiceScore): .7685**\n    * **Public LB: .454**\n        * ↑My TrainModel Weight↑:https://www.kaggle.com/datasets/hideyukizushi/colab-a-162v5-gpu-transunet-seresnext101-x160\n        * ※Training was performed on Google Colaboratory using GPU A100 (80GB).\n\n### **ℹ️[LB.460 2025/12/31]MyUpdate**(※Please note that this model was trained on data before the Dataset update on Dec 23, 2025)\n* The Public LB dataset has been updated, reference I am publishing the model that produced my score.\n    * **Model: TransUNet**\n    * **Encoder: SEResNeXt1013D**\n    * **Local Validation(DiceScore): .7679**\n    * **Public LB: .460**\n        * ↑My TrainModel Weight↑:https://www.kaggle.com/datasets/hideyukizushi/colab-a-162v4-gpu-transunet-seresnext101-x160\n","metadata":{}},{"cell_type":"code","source":"var=\"/kaggle/input/vesuvius25-packages-offline-installer-v20251226/whls\"\n!pip install \\\n    \"$var\"/keras_nightly-3.12.0.dev2025100703-py3-none-any.whl \\\n    \"$var\"/tifffile-2025.10.16-py3-none-any.whl \\\n    \"$var\"/imagecodecs-2025.11.11-cp311-abi3-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl \\\n    \"$var\"/medicai-0.0.3-py3-none-any.whl \\\n    --no-index \\\n    --find-links \"$var\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:21:46.735167Z","iopub.execute_input":"2025-12-31T02:21:46.735356Z","iopub.status.idle":"2025-12-31T02:21:56.275917Z","shell.execute_reply.started":"2025-12-31T02:21:46.735338Z","shell.execute_reply":"2025-12-31T02:21:56.27523Z"},"_kg_hide-output":true,"scrolled":true,"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nModel Detail\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"* List of models available on MedicAI","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')\n\nimport medicai\nmedicai.models.list_models()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:21:56.277349Z","iopub.execute_input":"2025-12-31T02:21:56.277555Z","iopub.status.idle":"2025-12-31T02:22:17.417861Z","shell.execute_reply.started":"2025-12-31T02:21:56.277534Z","shell.execute_reply":"2025-12-31T02:22:17.417096Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"* Encoder SEResNeXt1013D Detail","metadata":{}},{"cell_type":"code","source":"from medicai.models import SEResNeXt101\n\ntmp_backbone = SEResNeXt101(\n    input_shape=(160, 160, 160) + (1,),\n    include_top=False\n)\ntmp_backbone.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:17.418684Z","iopub.execute_input":"2025-12-31T02:22:17.419196Z","iopub.status.idle":"2025-12-31T02:22:23.455324Z","shell.execute_reply.started":"2025-12-31T02:22:17.419176Z","shell.execute_reply":"2025-12-31T02:22:23.454709Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"<h1 style=\"color: #6cb4e4;  text-align: center;  padding: 0.25em;  border-top: solid 2.5px #6cb4e4;  border-bottom: solid 2.5px #6cb4e4;  background: -webkit-repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);  background: repeating-linear-gradient(-45deg, #f0f8ff, #f0f8ff 3px,#e9f4ff 3px, #e9f4ff 7px);height:45px;\">\n<b>\nMain\n</b></h1> ","metadata":{}},{"cell_type":"markdown","source":"# 》》》**Libs**","metadata":{}},{"cell_type":"code","source":"import os\nos.environ[\"KERAS_BACKEND\"] = \"jax\"\n\nimport keras\nfrom medicai.transforms import (\n    Compose,\n    ScaleIntensityRange,\n)\nfrom medicai.models import SegFormer, TransUNet\nfrom medicai.utils.inference import SlidingWindowInference\n\nimport numpy as np\nimport pandas as pd\nimport zipfile\nimport tifffile\nfrom matplotlib import pyplot as plt\n\nkeras.config.backend(), keras.version()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.456007Z","iopub.execute_input":"2025-12-31T02:22:23.456419Z","iopub.status.idle":"2025-12-31T02:22:23.47726Z","shell.execute_reply.started":"2025-12-31T02:22:23.456398Z","shell.execute_reply":"2025-12-31T02:22:23.476602Z"},"_kg_hide-output":true,"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 》》》**Dataset**","metadata":{}},{"cell_type":"code","source":"root_dir = \"/kaggle/input/vesuvius-challenge-surface-detection\"\ntest_dir = f\"{root_dir}/test_images\"\noutput_dir = \"/kaggle/working/submission_masks\"\nzip_path = \"/kaggle/working/submission.zip\"\nos.makedirs(output_dir, exist_ok=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.479131Z","iopub.execute_input":"2025-12-31T02:22:23.47941Z","iopub.status.idle":"2025-12-31T02:22:23.486395Z","shell.execute_reply.started":"2025-12-31T02:22:23.479393Z","shell.execute_reply":"2025-12-31T02:22:23.485851Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(f\"{root_dir}/test.csv\")\ntest_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.487052Z","iopub.execute_input":"2025-12-31T02:22:23.487698Z","iopub.status.idle":"2025-12-31T02:22:23.539837Z","shell.execute_reply.started":"2025-12-31T02:22:23.48768Z","shell.execute_reply":"2025-12-31T02:22:23.539082Z"},"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 》》》**Transformation**","metadata":{}},{"cell_type":"code","source":"def val_transformation(image):\n    data = {\"image\": image}\n    pipeline = Compose([\n        ScaleIntensityRange(\n            keys=[\"image\"],\n            a_min = 0,\n            a_max = 255,\n            b_min = 0,\n            b_max = 1,\n            clip = True,\n        ),\n    ])\n    result = pipeline(data)\n    return result[\"image\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.540713Z","iopub.execute_input":"2025-12-31T02:22:23.540928Z","iopub.status.idle":"2025-12-31T02:22:23.545106Z","shell.execute_reply.started":"2025-12-31T02:22:23.540905Z","shell.execute_reply":"2025-12-31T02:22:23.544379Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 》》》**Load Model**","metadata":{}},{"cell_type":"code","source":"def get_model():\n    model = TransUNet(\n        input_shape=(160, 160, 160, 1),\n        encoder_name='seresnext101',\n        classifier_activation='softmax',\n        num_classes=3,\n    )\n    model.load_weights(\n        f\"/kaggle/input/colab-a-162v4-gpu-transunet-seresnext101-x160/model.weights.h5\"\n    )\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.545866Z","iopub.execute_input":"2025-12-31T02:22:23.546469Z","iopub.status.idle":"2025-12-31T02:22:23.564843Z","shell.execute_reply.started":"2025-12-31T02:22:23.546444Z","shell.execute_reply":"2025-12-31T02:22:23.564362Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = get_model()\nmodel.count_params() / 1e6","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:23.56552Z","iopub.execute_input":"2025-12-31T02:22:23.565762Z","iopub.status.idle":"2025-12-31T02:22:35.485216Z","shell.execute_reply.started":"2025-12-31T02:22:23.565742Z","shell.execute_reply":"2025-12-31T02:22:35.484492Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 》》》**Util**","metadata":{}},{"cell_type":"code","source":"swi = SlidingWindowInference(\n    model,\n    num_classes=3,\n    roi_size=(160, 160, 160),\n    sw_batch_size=1,\n    mode='gaussian',\n    overlap=0.52,\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:35.485975Z","iopub.execute_input":"2025-12-31T02:22:35.486473Z","iopub.status.idle":"2025-12-31T02:22:35.490472Z","shell.execute_reply.started":"2025-12-31T02:22:35.486452Z","shell.execute_reply":"2025-12-31T02:22:35.489933Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_volume(path):\n    vol = tifffile.imread(path)\n    vol = vol.astype(np.float32)\n    vol = vol[None, ..., None]\n    return vol\n\ndef predict(sample):\n    mask = swi(sample)\n    output = mask.argmax(-1).astype(np.uint8).squeeze()\n    return output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:35.491166Z","iopub.execute_input":"2025-12-31T02:22:35.491574Z","iopub.status.idle":"2025-12-31T02:22:35.513136Z","shell.execute_reply.started":"2025-12-31T02:22:35.491547Z","shell.execute_reply":"2025-12-31T02:22:35.512353Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 》》》**Predict & Submission**","metadata":{}},{"cell_type":"code","source":"with zipfile.ZipFile(\n    zip_path, \"w\", compression=zipfile.ZIP_DEFLATED\n) as z:\n    for image_id in test_df[\"id\"]:\n        tif_path = f\"{test_dir}/{image_id}.tif\"\n        \n        volume = load_volume(tif_path)\n        volume = val_transformation(volume)\n        output = predict(volume) \n        \n        out_path = f\"{output_dir}/{image_id}.tif\"\n        tifffile.imwrite(out_path, output.astype(np.uint8))\n\n        z.write(out_path, arcname=f\"{image_id}.tif\")\n        os.remove(out_path)\n\nprint(\"Submission ZIP:\", zip_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-31T02:22:35.513954Z","iopub.execute_input":"2025-12-31T02:22:35.514244Z","iopub.status.idle":"2025-12-31T02:24:02.334972Z","shell.execute_reply.started":"2025-12-31T02:22:35.514218Z","shell.execute_reply":"2025-12-31T02:24:02.334264Z"},"scrolled":true,"_kg_hide-output":true},"outputs":[],"execution_count":null}]}