open-mmlab/mmdetection · info

DeprecationWarning: `num_anchors` is deprecated, for consist

Error message

DeprecationWarning: `num_anchors` is deprecated, for consistency or also use `num_base_priors` instead

What it means

The AnchorHead.num_anchors property is a deprecated alias for prior_generator.num_base_priors[0]. Accessing it emits a DeprecationWarning each time. mmdet renamed anchors to base_priors for consistency across heads.

Source

Thrown at mmdet/models/dense_heads/anchor_head.py:108

            if train_cfg.get('sampler', None) is not None:
                self.sampler = TASK_UTILS.build(
                    self.train_cfg['sampler'], default_args=dict(context=self))
            else:
                self.sampler = PseudoSampler(context=self)

        self.fp16_enabled = False

        self.prior_generator = TASK_UTILS.build(anchor_generator)

        # Usually the numbers of anchors for each level are the same
        # except SSD detectors. So it is an int in the most dense
        # heads but a list of int in SSDHead
        self.num_base_priors = self.prior_generator.num_base_priors[0]
        self._init_layers()

    @property
    def num_anchors(self) -> int:
        warnings.warn('DeprecationWarning: `num_anchors` is deprecated, '
                      'for consistency or also use '
                      '`num_base_priors` instead')
        return self.prior_generator.num_base_priors[0]

    @property
    def anchor_generator(self) -> AnchorGenerator:
        warnings.warn('DeprecationWarning: anchor_generator is deprecated, '
                      'please use "prior_generator" instead')
        return self.prior_generator

    def _init_layers(self) -> None:
        """Initialize layers of the head."""
        self.conv_cls = nn.Conv2d(self.in_channels,
                                  self.num_base_priors * self.cls_out_channels,
                                  1)
        reg_dim = self.bbox_coder.encode_size
        self.conv_reg = nn.Conv2d(self.in_channels,
                                  self.num_base_priors * reg_dim, 1)

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Replace head.num_anchors with head.num_base_priors
  2. If a dependency triggers it and cannot be changed, suppress with warnings.filterwarnings ignoring the DeprecationWarning message

Example fix

# before
n = self.bbox_head.num_anchors
# after
n = self.bbox_head.num_base_priors
Defensive patterns

Strategy: validation

Validate before calling

assert hasattr(head, 'num_base_priors'), 'use num_base_priors'
n = head.num_base_priors  # instead of head.num_anchors

Type guard

def get_num_priors(head):
    if hasattr(head, 'num_base_priors'):
        return head.num_base_priors
    return head.num_anchors  # legacy fallback

Prevention

When it happens

Trigger: Reading head.num_anchors on AnchorHead (or subclasses like RPNHead, Faster R-CNN heads) in custom code or old plugins/tests.

Common situations: Custom dense heads, hooks, or visualization scripts written against older mmdet API; third-party projects relying on num_anchors.

Related errors


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