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
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
try:
from open3d._ml3d.datasets.utils import DataProcessing
except Exception: # Catches ALL exceptions
print("Open3D-ML3D not available")
Why This Is a Problem
-
Catches all exceptions including: FileNotFoundError, PermissionError, MemoryError, Corrupted model files
-
Misleading Behavior like no clear error message and no root cause visibility.
-
Using bare except: is discouraged (PEP 8).
-
Catching Exception hides real bugs.
Proposed Solution
Replace assert
if not os.path.isfile(model):
raise FileNotFoundError(f"Model file not found: {model}")
Improve exception handling
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
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
Problem
The model loading code in
perceptionmetrics/models/uses bareexcept Exception:blocks andassertstatements 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
FileNotFoundError,RuntimeError).Current Problematic Code
Segmentation / Detection
Optional Imports
Why This Is a Problem
Catches all exceptions including:
FileNotFoundError,PermissionError,MemoryError,Corrupted model filesMisleading Behavior like no clear error message and no root cause visibility.
Using bare
except:is discouraged (PEP 8).Catching
Exceptionhides real bugs.Proposed Solution
Replace assert
Improve exception handling
Fix optional imports
🧪 Testing Plan
Impact