{"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,"isSourceIdPinned":false},{"sourceType":"datasetVersion","sourceId":14961616,"datasetId":9576554,"databundleVersionId":15832903}],"dockerImageVersionId":31287,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install dynamic_network_architectures==0.4.3","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:35:15.587027Z","iopub.execute_input":"2026-02-25T19:35:15.587269Z","iopub.status.idle":"2026-02-25T19:35:28.927757Z","shell.execute_reply.started":"2026-02-25T19:35:15.587248Z","shell.execute_reply":"2026-02-25T19:35:28.926916Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install tensorrt==10.12.0.36","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:35:28.929407Z","iopub.execute_input":"2026-02-25T19:35:28.929714Z","iopub.status.idle":"2026-02-25T19:37:01.688006Z","shell.execute_reply.started":"2026-02-25T19:35:28.929681Z","shell.execute_reply":"2026-02-25T19:37:01.687203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorrt\nprint(tensorrt.__version__)\nimport tensorrt as trt\nimport os\n\nimport os\nimport sys\nimport subprocess\nfrom pathlib import Path\nfrom collections import OrderedDict\n\nimport torch\nimport torch.nn as nn\nimport pytorch_lightning as pl\nimport tensorrt as trt\nimport os\nimport sys\nimport subprocess\nfrom pathlib import Path\nfrom collections import OrderedDict\n\nimport torch\nimport torch.nn as nn\nimport pytorch_lightning as pl\nimport tensorrt as trt\n\nfrom dynamic_network_architectures.architectures.unet import ResidualEncoderUNet\nfrom dynamic_network_architectures.architectures.primus import PrimusB","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:37:01.689235Z","iopub.execute_input":"2026-02-25T19:37:01.689543Z","iopub.status.idle":"2026-02-25T19:37:38.873167Z","shell.execute_reply.started":"2026-02-25T19:37:01.689507Z","shell.execute_reply":"2026-02-25T19:37:38.872324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ROI_SIZE = (160, 160, 160)\n\nOUT_DIR = Path(\"./trt_models\")\nOUT_DIR.mkdir(exist_ok=True, parents=True)\n\nDEVICE = \"cuda\"\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:37:38.877217Z","iopub.execute_input":"2026-02-25T19:37:38.877448Z","iopub.status.idle":"2026-02-25T19:37:38.881919Z","shell.execute_reply.started":"2026-02-25T19:37:38.877421Z","shell.execute_reply":"2026-02-25T19:37:38.881069Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def build_engine(onnx_file_path, engine_file_path):\n    # 1. Setup Logger and Builder\n    logger = trt.Logger(trt.Logger.INFO)\n    builder = trt.Builder(logger)\n    \n    # 2. Create Network and Parser\n    # 1 << 0 (Explicit Batch) is required for ONNX importers\n    network = builder.create_network(1 << int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH))\n    parser = trt.OnnxParser(network, logger)\n    \n    # 3. Create Configuration\n    config = builder.create_builder_config()\n    \n    # Memory pool limit (workspace size). Give it plenty of RAM (e.g., 4GB)\n    config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, 8 * (1 << 30))\n    \n    # Enable FP16 (Half Precision) for speed\n    if builder.platform_has_fast_fp16:\n        print(\"Enabling FP16...\")\n        config.set_flag(trt.BuilderFlag.FP16)\n    \n    # 4. Parse ONNX File\n    print(f\"Parsing {onnx_file_path}...\")\n    with open(onnx_file_path, 'rb') as model:\n        if not parser.parse(model.read()):\n            print(\"ERROR: Failed to parse the ONNX file.\")\n            for error in range(parser.num_errors):\n                print(parser.get_error(error))\n            return None\n\n    # 5. Build and Serialize Engine\n    print(\"Building TensorRT Engine... (This may take a few minutes)\")\n    serialized_engine = builder.build_serialized_network(network, config)\n    \n    if serialized_engine:\n        with open(engine_file_path, \"wb\") as f:\n            f.write(serialized_engine)\n        print(f\"Engine saved to {engine_file_path}\")\n    else:\n        print(\"Failed to build engine.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:37:38.882775Z","iopub.execute_input":"2026-02-25T19:37:38.883023Z","iopub.status.idle":"2026-02-25T19:37:38.902321Z","shell.execute_reply.started":"2026-02-25T19:37:38.882996Z","shell.execute_reply":"2026-02-25T19:37:38.901634Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir refinenewV1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:37:38.903205Z","iopub.execute_input":"2026-02-25T19:37:38.903526Z","iopub.status.idle":"2026-02-25T19:37:39.055729Z","shell.execute_reply.started":"2026-02-25T19:37:38.903501Z","shell.execute_reply":"2026-02-25T19:37:39.054590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n\nrefineV1New   = [(x.stem, Path(\"./refinenewV1\"), x) for x in Path(\"/kaggle/input/datasets/iamparadox/vesuvius-refinev1-new-onnx\").glob(\"*.onnx\")]\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:38:54.598117Z","iopub.execute_input":"2026-02-25T19:38:54.598973Z","iopub.status.idle":"2026-02-25T19:38:54.604750Z","shell.execute_reply.started":"2026-02-25T19:38:54.598933Z","shell.execute_reply":"2026-02-25T19:38:54.604120Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"onnx_models = refineV1New","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:38:55.062406Z","iopub.execute_input":"2026-02-25T19:38:55.063130Z","iopub.status.idle":"2026-02-25T19:38:55.066516Z","shell.execute_reply.started":"2026-02-25T19:38:55.063087Z","shell.execute_reply":"2026-02-25T19:38:55.065826Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for (name, mdir, ONNX_FILE_PATH) in onnx_models:\n\n    ENGINE_FILE_PATH = f\"./{mdir}/{name}.engine\"\n    print(ENGINE_FILE_PATH)\n    build_engine(ONNX_FILE_PATH, ENGINE_FILE_PATH)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T19:39:05.873100Z","iopub.execute_input":"2026-02-25T19:39:05.873554Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}