JdeRobot / JdeRobot/PerceptionMetrics

Issue : Silent Exception Swallowing in Model Loading

Open
#520 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
112
Forks
109
PR merge metrics
No merged PRs in 30d

Description

## Problem

The model loading code in `perceptionmetrics/models/` uses bare `except Exception:` blocks and `assert` statements that silently swallow all exceptions, making debugging very difficult.

---

## 📍 Locations with Issue

- `perceptionmetrics/models/torch_segmentation.py` (Lines ~217–230, ~548–561)
- `perceptionmetrics/models/torch_detection.py` (Lines ~265–275)
- `perceptionmetrics/models/utils/o3d/randlanet.py` (Lines ~5–8)
- `perceptionmetrics/models/utils/o3d/kpconv.py` (Lines ~5–8)

---

## Expected Behavior

- Raise specific exceptions (`FileNotFoundError`, `RuntimeError`).
- Preserve original error context.
- Provide clear and actionable error messages.
- Avoid misleading fallback behavior.

---

## Current Problematic Code

### Segmentation / Detection

```python
if isinstance(model, str):
assert os.path.isfile(model), "Torch model file not found" # Uses assert
model_fname = model
try:
model = torch.jit.load(model, map_location=self.device)
model_type = "compiled"
except Exception: # Catches ALL exceptions
...

```

### Optional Imports

```python
try:
from open3d._ml3d.datasets.utils import DataProcessing
except Exception: # Catches ALL exceptions
print("Open3D-ML3D not available")
```

---

## Why This Is a Problem

1. Catches all exceptions including: `FileNotFoundError`, `PermissionError`, `MemoryError`, `Corrupted model files`

2. Misleading Behavior like no clear error message and no root cause visibility.

3. Using bare `except:` is discouraged (PEP 8).
4. Catching `Exception` hides real bugs.

---

## Proposed Solution

### Replace assert

```python
if not os.path.isfile(model):
raise FileNotFoundError(f"Model file not found: {model}")
```

---

### Improve exception handling

```python
try:
model = torch.jit.load(model, map_location=self.device)
model_type = "compiled"

except (RuntimeError, EOFError) as jit_err:
try:
model = torch.load(model, map_location=self.device)
model_type = "native"

except Exception as load_err:
raise RuntimeError(
...
```

---

### Fix optional imports

```python
try:
from open3d._ml3d.datasets.utils import DataProcessing
except ImportError:
...
```

---

## 🧪 Testing Plan

- Valid TorchScript model → loads correctly
- Valid PyTorch model → fallback works
- Invalid path → raises FileNotFoundError
- Corrupted file → raises RuntimeError
- Missing dependency → handled via ImportError

---

## Impact

- Better debugging
- No silent failures
- Improved robustness
- Aligns with Python best practices

---

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.