Vancy Agnes Fernandes commited on
Commit ·
feb2bea
1
Parent(s): cf76980
fix(models): load trusted checkpoints with weights_only disabled
Browse files- model_loader.py +10 -2
model_loader.py
CHANGED
|
@@ -196,6 +196,14 @@ def _extract_state_dict(checkpoint):
|
|
| 196 |
return checkpoint
|
| 197 |
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
def _looks_like_legacy_unet(state_dict):
|
| 200 |
"""Detect legacy UNet checkpoints by key namespace."""
|
| 201 |
if not isinstance(state_dict, dict):
|
|
@@ -266,7 +274,7 @@ def load_models():
|
|
| 266 |
# Load UNet
|
| 267 |
try:
|
| 268 |
if os.path.exists(UNET_PATH):
|
| 269 |
-
checkpoint =
|
| 270 |
unet_state = _extract_state_dict(checkpoint)
|
| 271 |
loaded_unet_model = None
|
| 272 |
|
|
@@ -290,7 +298,7 @@ def load_models():
|
|
| 290 |
# Load EfficientNet
|
| 291 |
try:
|
| 292 |
if os.path.exists(EFFICIENTNET_PATH):
|
| 293 |
-
checkpoint =
|
| 294 |
eff_state = _extract_state_dict(checkpoint)
|
| 295 |
nested_classifier = isinstance(eff_state, dict) and 'classifier.1.1.weight' in eff_state
|
| 296 |
loaded_efficientnet_model = create_efficientnet_model(num_classes=1, nested_classifier=nested_classifier)
|
|
|
|
| 196 |
return checkpoint
|
| 197 |
|
| 198 |
|
| 199 |
+
def _load_checkpoint(path):
|
| 200 |
+
"""Load trusted local checkpoints in a PyTorch-version-safe way."""
|
| 201 |
+
try:
|
| 202 |
+
return torch.load(path, map_location=device, weights_only=False)
|
| 203 |
+
except TypeError:
|
| 204 |
+
return torch.load(path, map_location=device)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
def _looks_like_legacy_unet(state_dict):
|
| 208 |
"""Detect legacy UNet checkpoints by key namespace."""
|
| 209 |
if not isinstance(state_dict, dict):
|
|
|
|
| 274 |
# Load UNet
|
| 275 |
try:
|
| 276 |
if os.path.exists(UNET_PATH):
|
| 277 |
+
checkpoint = _load_checkpoint(UNET_PATH)
|
| 278 |
unet_state = _extract_state_dict(checkpoint)
|
| 279 |
loaded_unet_model = None
|
| 280 |
|
|
|
|
| 298 |
# Load EfficientNet
|
| 299 |
try:
|
| 300 |
if os.path.exists(EFFICIENTNET_PATH):
|
| 301 |
+
checkpoint = _load_checkpoint(EFFICIENTNET_PATH)
|
| 302 |
eff_state = _extract_state_dict(checkpoint)
|
| 303 |
nested_classifier = isinstance(eff_state, dict) and 'classifier.1.1.weight' in eff_state
|
| 304 |
loaded_efficientnet_model = create_efficientnet_model(num_classes=1, nested_classifier=nested_classifier)
|