PaddlePaddle/PaddleOCR · error · Exception

factor must be number or list with length 2

Error message

factor must be number or list with length 2

What it means

CVImagePyramid's constructor (ppocr/data/imaug/abinet_aug.py) accepts factor as either a number (sample uniformly in [0, factor]) or a 2-element tuple/list (sample uniformly in that range); anything else raises a bare Exception('factor must be number or list with length 2'). This controls how many gaussian-pyramid downscalings are applied for ABINet training crops.

Source

Thrown at ppocr/data/imaug/abinet_aug.py:281

        )
        img = img[min_y:, min_x:]
        return img


class CVRescale(object):
    def __init__(self, factor=4, base_size=(128, 512)):
        """Define image scales using gaussian pyramid and rescale image to target scale.

        Args:
            factor: the decayed factor from base size, factor=4 keeps target scale by default.
            base_size: base size the build the bottom layer of pyramid
        """
        if isinstance(factor, numbers.Number):
            self.factor = round(sample_uniform(0, factor))
        elif isinstance(factor, (tuple, list)) and len(factor) == 2:
            self.factor = round(sample_uniform(factor[0], factor[1]))
        else:
            raise Exception("factor must be number or list with length 2")
        # assert factor is valid
        self.base_h, self.base_w = base_size[:2]

    def __call__(self, img):
        if self.factor == 0:
            return img
        src_h, src_w = img.shape[:2]
        cur_w, cur_h = self.base_w, self.base_h
        scale_img = cv2.resize(img, (cur_w, cur_h), interpolation=get_interpolation())
        for _ in range(self.factor):
            scale_img = cv2.pyrDown(scale_img)
        scale_img = cv2.resize(
            scale_img, (src_w, src_h), interpolation=get_interpolation()
        )
        return scale_img


class CVGaussianNoise(object):

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Pass an int/float (e.g. factor=4) or an exact 2-element list/tuple like factor=(2, 5).
  2. If loading from YAML/JSON, cast: factor=int(cfg['factor']) when it is a single value.
  3. Validate length == 2 for range forms before constructing the transform.

Example fix

# before
CVImagePyramid(factor=str(cfg['factor']))  # Exception: factor must be number or list with length 2
# after
raw = cfg['factor']
factor = int(raw) if isinstance(raw, (int, float)) else tuple(raw[:2])
CVImagePyramid(factor=factor)
Defensive patterns

Strategy: validation

Validate before calling

import numbers

def valid_factor(f) -> bool:
    return (isinstance(f, numbers.Number)
            or (isinstance(f, (tuple, list)) and len(f) == 2))

Type guard

import numbers

def is_valid_factor(v) -> bool:
    return isinstance(v, numbers.Number) or (isinstance(v, (tuple, list)) and len(v) == 2)

Prevention

When it happens

Trigger: CVImagePyramid(factor='4'), factor=[1, 2, 3] (length 3), factor={'min':1,'max':4}, or a 1-element list.

Common situations: YAML configs parsing numbers as strings ('4' stays a str); over-specified config dicts; expecting torchvision-style (min, max) plus extra keys.

Related errors


AI-assisted analysis of PaddlePaddle/PaddleOCR@2661c7c0ef (2026-08-14). Data as JSON: /api/errors/ab1a427e190b6312. Report an issue: GitHub.