Skip to content

Commit 33901aa

Browse files
committed
Improve error handling in model loading for MonaiBundleInferenceOperator
- Enhanced exception handling by capturing the original exception in model loading functions to provide clearer error messages. - Updated the handling of network instantiation errors to include the original exception context for better debugging. Signed-off-by: Victor Chang <[email protected]>
1 parent eea6879 commit 33901aa

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

monai/deploy/operators/monai_bundle_inference_operator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ def _load_model_from_directory_bundle(bundle_path: Path, device: torch.device, p
9999
try:
100100
# Some .pt files may still be TorchScript; try jit first
101101
return torch.jit.load(str(model_path), map_location=device).eval()
102-
except Exception:
102+
except Exception as ex:
103103
# Fallback to eager model with loaded weights
104104
if parser is None:
105-
raise RuntimeError("Parser required for loading .pt checkpoint but not provided")
105+
raise RuntimeError("Parser required for loading .pt checkpoint but not provided") from ex
106106

107107
# Ensure bundle root is on sys.path so 'scripts.*' can be imported
108108
_ensure_bundle_in_sys_path(bundle_path)
@@ -114,7 +114,7 @@ def _load_model_from_directory_bundle(bundle_path: Path, device: torch.device, p
114114
if network is not None:
115115
network = network.to(device)
116116
if network is None:
117-
raise RuntimeError("Unable to instantiate network from bundle configs.") from None
117+
raise RuntimeError("Unable to instantiate network from bundle configs.") from ex
118118

119119
checkpoint = torch.load(str(model_path), map_location=device)
120120
# Determine the state dict layout
@@ -1112,7 +1112,7 @@ def _infer_spatial_dimensions_from_metadata(self, metadata: Dict) -> Optional[in
11121112
return 3
11131113

11141114
# Check for keys ending with dimension indicators
1115-
for key, value in metadata.items():
1115+
for key, _ in metadata.items():
11161116
key_lower = str(key).lower()
11171117
if key_lower.endswith("_2d") or "2d" in key_lower:
11181118
return 2

0 commit comments

Comments
 (0)