open-mmlab/mmdetection · critical · ImportError

Please run "pip install scipy" to install scipy first.

Error message

Please run "pip install scipy" to install scipy first.

What it means

HungarianAssigner uses scipy.optimize.linear_sum_assignment for optimal bipartite matching; if scipy failed to import, assign() raises ImportError at matching time (not construction time), telling you to pip install scipy.

Source

Thrown at mmdet/models/task_modules/assigners/hungarian_assigner.py:128

                num_gts=num_gts,
                gt_inds=assigned_gt_inds,
                max_overlaps=None,
                labels=assigned_labels)

        # 2. compute weighted cost
        cost_list = []
        for match_cost in self.match_costs:
            cost = match_cost(
                pred_instances=pred_instances,
                gt_instances=gt_instances,
                img_meta=img_meta)
            cost_list.append(cost)
        cost = torch.stack(cost_list).sum(dim=0)

        # 3. do Hungarian matching on CPU using linear_sum_assignment
        cost = cost.detach().cpu()
        if linear_sum_assignment is None:
            raise ImportError('Please run "pip install scipy" '
                              'to install scipy first.')

        matched_row_inds, matched_col_inds = linear_sum_assignment(cost)
        matched_row_inds = torch.from_numpy(matched_row_inds).to(device)
        matched_col_inds = torch.from_numpy(matched_col_inds).to(device)

        # 4. assign backgrounds and foregrounds
        # assign all indices to backgrounds first
        assigned_gt_inds[:] = 0
        # assign foregrounds based on matching results
        assigned_gt_inds[matched_row_inds] = matched_col_inds + 1
        assigned_labels[matched_row_inds] = gt_labels[matched_col_inds]
        return AssignResult(
            num_gts=num_gts,
            gt_inds=assigned_gt_inds,
            max_overlaps=None,
            labels=assigned_labels)

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. pip install scipy
  2. If numpy/scipy ABI conflicts occur, upgrade both: pip install -U numpy scipy
  3. Verify: python -c "from scipy.optimize import linear_sum_assignment"

Example fix

# before
ImportError during HungarianAssigner.assign
# after
pip install scipy  # training runs
Defensive patterns

Strategy: validation

Validate before calling

try:
    from scipy.optimize import linear_sum_assignment  # noqa
    ok = True
except ImportError:
    ok = False
assert ok, 'scipy required for HungarianAssigner'

Try / catch

try:
    model.train()
except ImportError as e:
    raise SystemExit('Missing scipy: pip install scipy') from e

Prevention

When it happens

Trigger: Building a DETR/Deformable-DETR/DINO-style model (hungarian assigner) in an environment without scipy, then running training or loss computation.

Common situations: Minimal torch-only environments; scipy broken by a numpy version conflict so the guarded import returns None.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/494a4a6ddcc3b9b1. Report an issue: GitHub.