|
20 | 20 | from copy import deepcopy |
21 | 21 | from pathlib import Path |
22 | 22 | from threading import Lock |
23 | | -from typing import Any, Dict, List, Optional, Tuple, Type, Union |
| 23 | +from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union, cast |
24 | 24 |
|
25 | 25 | import numpy as np |
26 | 26 |
|
|
29 | 29 |
|
30 | 30 | from .inference_operator import InferenceOperator |
31 | 31 |
|
| 32 | +if TYPE_CHECKING: |
| 33 | + import torch |
| 34 | + |
32 | 35 | MONAI_UTILS = "monai.utils" |
33 | 36 | nibabel, _ = optional_import("nibabel", "3.2.1") |
34 | 37 | torch, _ = optional_import("torch", "1.10.2") |
@@ -93,12 +96,12 @@ def _load_model_from_directory_bundle(bundle_path: Path, device: torch.device, p |
93 | 96 | # Load model based on file type |
94 | 97 | if model_path.suffix == ".ts": |
95 | 98 | # TorchScript bundle |
96 | | - return torch.jit.load(str(model_path), map_location=device).eval() |
| 99 | + return cast("torch.nn.Module", torch.jit.load(str(model_path), map_location=device).eval()) |
97 | 100 | else: |
98 | 101 | # .pt checkpoint: instantiate network from config and load state dict |
99 | 102 | try: |
100 | 103 | # Some .pt files may still be TorchScript; try jit first |
101 | | - return torch.jit.load(str(model_path), map_location=device).eval() |
| 104 | + return cast("torch.nn.Module", torch.jit.load(str(model_path), map_location=device).eval()) |
102 | 105 | except Exception as ex: |
103 | 106 | # Fallback to eager model with loaded weights |
104 | 107 | if parser is None: |
@@ -128,7 +131,7 @@ def _load_model_from_directory_bundle(bundle_path: Path, device: torch.device, p |
128 | 131 | # Assume raw state dict |
129 | 132 | state_dict = checkpoint |
130 | 133 | network.load_state_dict(state_dict, strict=True) |
131 | | - return network.eval() |
| 134 | + return cast("torch.nn.Module", network.eval()) |
132 | 135 |
|
133 | 136 |
|
134 | 137 | def _read_directory_bundle_config(bundle_path_obj: Path, config_names: List[str]) -> ConfigParser: |
|
0 commit comments