Skip to content

fix: replace bare except with extension-based dispatch in TorchScript model loading#537

Open
AdityaX18 wants to merge 1 commit intoJdeRobot:masterfrom
AdityaX18:fix/torchscript-model-loading-419
Open

fix: replace bare except with extension-based dispatch in TorchScript model loading#537
AdityaX18 wants to merge 1 commit intoJdeRobot:masterfrom
AdityaX18:fix/torchscript-model-loading-419

Conversation

@AdityaX18
Copy link
Copy Markdown

@AdityaX18 AdityaX18 commented Mar 30, 2026

Body:
Fixes #419


## Bugs Fixed (3 total across 2 files)

| File | Class | Bug |
|---|---|---|
| `torch_detection.py` | `TorchImageDetectionModel` | `except Exception` catches warnings, prints false message |
| `torch_segmentation.py` | `TorchImageSegmentationModel` | bare `except:` catches everything + missing `weights_only=False` |
| `torch_segmentation.py` | `TorchLiDARSegmentationModel` | `except Exception` + missing `weights_only=False` |

## Root Cause
All three used try/except to detect model format. `torch.jit.load()` on 
a valid `.torchscript` file can raise an internal `UserWarning` before 
succeeding. The bare except catches this, prints a false diagnostic, and 
loads correctly via fallback anyway — misleading the user.

The bare `except:` in `TorchImageSegmentationModel` is additionally unsafe: 
it catches `KeyboardInterrupt`, `SystemExit`, and `GeneratorExit`.

## Fix
Primary dispatch by file extension. Fallback probe only for unknown 
extensions, catching only `RuntimeError`, emitting `warnings.warn()` 
(not `print()`) with actionable guidance.

## Tests

pytest tests/test_torch_model_loading.py -v
4 passed


> Note: I am a GSoC 2026 applicant for JdeRobot. This fix directly 
> supports robust model loading as a prerequisite for the robustness 
> evaluation module proposed in my application.Fixes #419

Three bugs fixed across two files:

1. torch_detection.py: bare 'except Exception' caused valid .torchscript files to trigger the except block on an internal PyTorch UserWarning, printing 'Model is not a TorchScript model' even for valid TorchScript.

2. torch_segmentation.py TorchImageSegmentationModel: bare 'except:' with no exception type — catches KeyboardInterrupt, SystemExit, everything. Also missing weights_only=False on torch.load().

3. torch_segmentation.py TorchLiDARSegmentationModel: same bare except Exception pattern + missing weights_only=False.

Fix: primary dispatch by file extension (.torchscript -> torch.jit.load, .pt/.pth -> torch.load with weights_only=False). Unknown extensions probe TorchScript first, catch only RuntimeError, fall back to torch.load() and emit warnings.warn() with actionable guidance instead of print().

Added tests/test_torch_model_loading.py covering all 4 loading paths.

Fixes JdeRobot#419

Three bugs fixed across two files:

1. torch_detection.py: bare 'except Exception' caused valid .torchscript
   files to trigger the except block on an internal PyTorch UserWarning,
   printing 'Model is not a TorchScript model' even for valid TorchScript.

2. torch_segmentation.py TorchImageSegmentationModel: bare 'except:' with
   no exception type — catches KeyboardInterrupt, SystemExit, everything.
   Also missing weights_only=False on torch.load().

3. torch_segmentation.py TorchLiDARSegmentationModel: same bare except
   Exception pattern + missing weights_only=False.

Fix: primary dispatch by file extension (.torchscript -> torch.jit.load,
.pt/.pth -> torch.load with weights_only=False). Unknown extensions probe
TorchScript first, catch only RuntimeError, fall back to torch.load() and
emit warnings.warn() with actionable guidance instead of print().

Added tests/test_torch_model_loading.py covering all 4 loading paths.
@AdityaX18 AdityaX18 changed the title fix: replace bare except with extension-based dispatch in model loading fix: replace bare except with extension-based dispatch in TorchScript model loading Mar 30, 2026
@dpascualhe
Copy link
Copy Markdown
Collaborator

Closing since #419 has already been resolved. Thaks for your interest!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Inconsistent model loading behavior causes misleading warning for TorchScript models

2 participants