PaddlePaddle/PaddleOCR · error · TypeError

Type of target_size is invalid. Now is {}

Error message

Type of target_size is invalid. Now is {}

What it means

Pad is a preprocessing op that pads images to a fixed size or to a multiple of size_div (default 32). Its constructor validates that 'size', when given, is an int, list, or tuple; any other type raises TypeError with the actual type printed.

Source

Thrown at ppocr/data/imaug/operators.py:140

        data["fast_label"] = fast_label
        return data


class KeepKeys(object):
    def __init__(self, keep_keys, **kwargs):
        self.keep_keys = keep_keys

    def __call__(self, data):
        data_list = []
        for key in self.keep_keys:
            data_list.append(data[key])
        return data_list


class Pad(object):
    def __init__(self, size=None, size_div=32, **kwargs):
        if size is not None and not isinstance(size, (int, list, tuple)):
            raise TypeError(
                "Type of target_size is invalid. Now is {}".format(type(size))
            )
        if isinstance(size, int):
            size = [size, size]
        self.size = size
        self.size_div = size_div

    def __call__(self, data):
        img = data["image"]
        img_h, img_w = img.shape[0], img.shape[1]
        if self.size:
            resize_h2, resize_w2 = self.size
            assert (
                img_h < resize_h2 and img_w < resize_w2
            ), "(h, w) of target size should be greater than (img_h, img_w)"
        else:
            resize_h2 = max(
                int(math.ceil(img.shape[0] / self.size_div) * self.size_div),

View on GitHub (pinned to 2661c7c0ef)

Solutions

  1. Use an int (size: 640 means [640, 640]) or a two-element list (size: [640, 640])
  2. Remove quoting in YAML so the value parses as a number
  3. Convert numpy values in Python code: size=int(np_val) or size=np_val.tolist()
  4. Omit size entirely to pad only to the size_div multiple

Example fix

# before
- Pad:
    size: '640'
# after
- Pad:
    size: [640, 640]
Defensive patterns

Strategy: type-guard

Validate before calling

s = op_cfg.get('size')
if s is not None and not isinstance(s, (int, list, tuple)):
    raise SystemExit(f'Pad size must be int or [h, w], got {type(s).__name__}')

Type guard

import numbers
def is_valid_pad_size(s):
    return s is None or isinstance(s, int) or (
        isinstance(s, (list, tuple)) and len(s) == 2
        and all(isinstance(x, numbers.Number) for x in s)
    )

Try / catch

try:
    pad = Pad(**op_cfg)
except TypeError as e:
    raise ValueError(f'Invalid Pad config {op_cfg}: {e}') from e

Prevention

When it happens

Trigger: Adding a Pad op to a yml preprocess pipeline with size given as a string (size: '640'), a dict, or a numpy array.

Common situations: YAML quoting mistakes that turn 640 into '640'; passing a numpy int64 or np.array from custom code (numpy scalar is not a numbers-compatible int for this isinstance check unless converted); copy-pasting padding configs between ops with different signatures.

Related errors


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