Skip to content

Issue : Silent Exception Swallowing in Model Loading #520

Description

@priyan17singh

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

  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

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions