open-mmlab/mmdetection · error · ValueError

center_offset should be in range [0, 1], {center_offset} is

Error message

center_offset should be in range [0, 1], {center_offset} is given.

What it means

AnchorGenerator.__init__ requires center_offset to be within [0, 1] (0.5 is the standard 'center' anchor placement). Values outside that range raise ValueError because anchor centers would fall outside feature cells.

Source

Thrown at mmdet/models/task_modules/prior_generators/anchor_generator.py:85

    """

    def __init__(self,
                 strides: Union[List[int], List[Tuple[int, int]]],
                 ratios: List[float],
                 scales: Optional[List[int]] = None,
                 base_sizes: Optional[List[int]] = None,
                 scale_major: bool = True,
                 octave_base_scale: Optional[int] = None,
                 scales_per_octave: Optional[int] = None,
                 centers: Optional[List[Tuple[float, float]]] = None,
                 center_offset: float = 0.,
                 use_box_type: bool = False) -> None:
        # check center and center_offset
        if center_offset != 0:
            assert centers is None, 'center cannot be set when center_offset' \
                                    f'!=0, {centers} is given.'
        if not (0 <= center_offset <= 1):
            raise ValueError('center_offset should be in range [0, 1], '
                             f'{center_offset} is given.')
        if centers is not None:
            assert len(centers) == len(strides), \
                'The number of strides should be the same as centers, got ' \
                f'{strides} and {centers}'

        # calculate base sizes of anchors
        self.strides = [_pair(stride) for stride in strides]
        self.base_sizes = [min(stride) for stride in self.strides
                           ] if base_sizes is None else base_sizes
        assert len(self.base_sizes) == len(self.strides), \
            'The number of strides should be the same as base sizes, got ' \
            f'{self.strides} and {self.base_sizes}'

        # calculate scales of anchors
        assert ((octave_base_scale is not None
                 and scales_per_octave is not None) ^ (scales is not None)), \
            'scales and octave_base_scale with scales_per_octave cannot' \

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Use center_offset=0.5 (default, pixel centers) or 0 (corner-aligned)
  2. If you also set `centers`, note center_offset must be 0 — remove one of them

Example fix

# before
anchor_generator=dict(..., center_offset=0.55)
# after
anchor_generator=dict(..., center_offset=0.5)
Defensive patterns

Strategy: validation

Validate before calling

co = cfg.get('center_offset', 0.5)
assert isinstance(co, (int, float)) and 0 <= co <= 1

Type guard

def valid_center_offset(v) -> bool: return isinstance(v,(int,float)) and 0 <= v <= 1

Prevention

When it happens

Trigger: anchor_generator=dict(type='AnchorGenerator', center_offset=1.5) or a negative value; also configs where center_offset is computed and overshoots the range.

Common situations: Tuning anchor centering for special heads (corner-aligned anchors), typos like center_offset=5 instead of 0.5.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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