tensorflow/models · error · ValueError
image_size must be a positive multiple of
Error message
image_size must be a positive multiple of {patch_size}, got {image_size}. What it means
Raised by validate_image_size, a guard run before model construction. DINOv3 ViT backbones split the image into non-overlapping 16x16 patches (DINOV3_PATCH_SIZE); an image_size that is not a positive multiple of patch_size would leave a partial patch that the patch embedding cannot tile, so the check fails fast. Fires when a config passes e.g. 224 (not divisible by 16) or a non-positive value.
Solutions
- Choose an image_size that is a positive multiple of 16, such as 256, 512, or 1024
- Fix invalid values like 0 or negative sizes in the experiment config
- Derive image_size programmatically: image_size = patch_size * k for integer k
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at official/projects/waste_identification_ml/fine_tuning/Dinov3_image_classifier/models.py:66 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/6a0dca7e7665c954.
Report an issue: GitHub.
Appendix: source
Thrown at official/projects/waste_identification_ml/fine_tuning/Dinov3_image_classifier/models.py:66
DEFAULT_NUMBER_OF_CLASSES = 2
# DINOv3 ViT backbones use 16x16 patches. Input image sizes must be a
# multiple of this value so the patch embedding tiles cleanly.
DINOV3_PATCH_SIZE = 16
def validate_image_size(image_size: int, patch_size: int) -> None:
"""Verifies that the chosen image size is a multiple of the patch size.
Args:
image_size: Side length in pixels of the square input image.
patch_size: Side length in pixels of the backbone's patch embedding.
Raises:
ValueError: If `image_size` is not a positive multiple of `patch_size`.
"""
if image_size <= 0 or image_size % patch_size != 0:
raise ValueError(
f"image_size must be a positive multiple of {patch_size}, "
f"got {image_size}."
)
def load_model(
model_name: str,
repo_dir: pathlib.Path,
weights: pathlib.Path | None = None,
) -> nn.Module:
"""Loads a DINOv3 backbone via torch.hub from a local repository.
Args:
model_name: Name of the DINOv3 model variant (e.g., 'dinov3_vits16').
repo_dir: Path to the cloned Facebook DINOv3 repository.
weights: Optional path to a pretrained weights file. If None, the model is
loaded with random weights.
View on GitHub (pinned to e006f5f0d5)