huggingface/transformers · error · ValueError

Depending on the model, `size_divisor` or `pad_size` or `siz

Error message

Depending on the model, `size_divisor` or `pad_size` or `size` must be specified if `do_pad` is `True`.

What it means

Raised by `transformers.image_utils.validate_preprocess_arguments` when `do_pad=True` but `pad_size` is None. The in-code comment states this check is kept only for backwards compatibility and is 'pointless' because different processors pad via different knobs — `pad_size`/`size` (pad to specific values), `size_divisor` (pad to a multiple), or nothing (pad to the batch max). Nevertheless, when this validator runs with do_pad enabled and no pad_size, it raises unconditionally.

Source

Thrown at src/transformers/image_utils.py:614

    """
    Checks validity of typically used arguments in an `ImageProcessor` `preprocess` method.
    Raises `ValueError` if arguments incompatibility is caught.
    Many incompatibilities are model-specific. `do_pad` sometimes needs `size_divisor`,
    sometimes `size_divisibility`, and sometimes `size`. New models and processors added should follow
    existing arguments when possible.

    """
    if do_rescale and rescale_factor is None:
        raise ValueError("`rescale_factor` must be specified if `do_rescale` is `True`.")

    if do_pad and pad_size is None:
        # Processors pad images using different args depending on the model, so the below check is pointless
        # but we keep it for BC for now. TODO: remove in v5
        # Usually padding can be called with:
        #   - "pad_size/size" if we're padding to specific values
        #   - "size_divisor" if we're padding to any value divisible by X
        #   - "None" if we're padding to the maximum size image in batch
        raise ValueError(
            "Depending on the model, `size_divisor` or `pad_size` or `size` must be specified if `do_pad` is `True`."
        )

    if do_normalize and (image_mean is None or image_std is None):
        raise ValueError("`image_mean` and `image_std` must both be specified if `do_normalize` is `True`.")

    if do_center_crop and crop_size is None:
        raise ValueError("`crop_size` must be specified if `do_center_crop` is `True`.")

    if do_resize and not (size is not None and resample is not None):
        raise ValueError("`size` and `resample` must be specified if `do_resize` is `True`.")


class ImageFeatureExtractionMixin:
    """
    Mixin that contain utilities for preparing image features.
    """

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass a pad size with the flag: `processor(images, do_pad=True, pad_size={'height': H, 'width': W})` (match your processor's pad_size schema — some take dicts, some ints).
  2. Or disable padding and pad manually with `transformers.image_transforms.pad` / `torch.nn.functional.pad`.
  3. For size_divisor-style processors, pass the divisor argument their preprocess defines so their internal path supplies pad_size, or set `do_pad=False` and pad to the divisor yourself.
  4. If the flag comes from a saved preprocessor_config.json, update the config to include pad_size and re-save.

Example fix

// before
inputs = processor(images=imgs, do_pad=True, return_tensors="pt")  # ValueError

// after
inputs = processor(images=imgs, do_pad=True, pad_size={"height": 640, "width": 640}, return_tensors="pt")
// or pad manually:
from transformers.image_transforms import pad
padded = [pad(np.array(i), 0) for i in imgs]
Defensive patterns

Strategy: validation

Validate before calling

if do_pad:
    assert pad_size is not None, "do_pad=True requires pad_size (dict or int, per processor)"

inputs = processor(images=images, do_pad=do_pad, pad_size=pad_size or {"height": 640, "width": 640})

Prevention

When it happens

Trigger: Calling `processor.preprocess(images, do_pad=True)` without `pad_size`; enabling do_pad on a processor whose padding is driven by `size_divisor` or by batch-max logic but whose preprocess path still calls this validator with pad_size=None.

Common situations: Per-call flag toggling that forgets the paired padding argument; older/other-repo processor configs where do_pad=True is saved but pad_size is not; custom processors that pass do_pad through to the shared validator while intending size_divisor semantics.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/0f8f53e2e3061321. Report an issue: GitHub.