jax-ml/jax · error · ValueError
shape must have length equal to the number of dimensions of
Error message
shape must have length equal to the number of dimensions of x; {shape} vs {image.shape} What it means
jax.image.scale.scale_and_translate requires the shape argument's length to equal image.ndim (it describes the full output shape, not just spatial dims). After canonicalizing, a length mismatch raises ValueError comparing len(shape) with image.shape.
Source
Thrown at jax/_src/image/scale.py:374
passed scale and translation should be applied to.
scale: A [K] array with the same number of dimensions as image, containing
the scale to apply in each dimension.
translation: A [K] array with the same number of dimensions as image,
containing the translation to apply in each dimension.
method: the resizing method to use; either a ``ResizeMethod`` instance or a
string. Available methods are: ``LINEAR``, ``LANCZOS3``, ``LANCZOS5``,
``CUBIC``, ``CUBIC_PYTORCH``, ``AREA``.
antialias: Should an antialiasing filter be used when downsampling? Defaults
to ``True``. Has no effect when upsampling.
Returns:
The scale and translated image.
"""
shape = core.canonicalize_shape(shape)
if len(shape) != image.ndim:
msg = ('shape must have length equal to the number of dimensions of x; '
f' {shape} vs {image.shape}')
raise ValueError(msg)
if isinstance(method, str):
method = ResizeMethod.from_string(method)
if method == ResizeMethod.NEAREST:
# Nearest neighbor is currently special-cased for straight resize, so skip
# for now.
raise ValueError('Nearest neighbor resampling is not currently supported '
'for scale_and_translate.')
assert isinstance(method, ResizeMethod)
if method == ResizeMethod.CUBIC_PYTORCH and antialias:
method = ResizeMethod.CUBIC
radius, kernel = _kernels[method]
edge_padding = (method == ResizeMethod.CUBIC_PYTORCH and not antialias)
image, = promote_dtypes_inexact(image)
scale, translation = promote_dtypes_inexact(scale, translation)
return _scale_and_translate(
image, shape, spatial_dims, scale, translation, kernel, antialias,
precision, edge_padding=edge_padding, radius=radius)View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Pass the full output shape matching image.ndim, e.g. (H2, W2, C) for CHW-unaware arrays (channels included)
- Derive it programmatically: out_shape = list(img.shape); out_shape[-3:-1] = (h2, w2) for channel-last
- Prefer the higher-level jax.image.resize which handles this mapping from spatial dims
Example fix
# before img = jnp.zeros((224, 224, 3)) out = scale_and_translate(img, (112, 112), ...) # ValueError # after out = scale_and_translate(img, (112, 112, 3), ...) # or jax.image.resize(img, (112, 112), method='linear')
Defensive patterns
Strategy: validation
Validate before calling
assert len(shape) == image.ndim, f'need full output shape of length {image.ndim}'
scale_and_translate(image, shape, scale, translation, method) Type guard
# no static type narrowing available; runtime check only
Prevention
- Remember scale_and_translate takes the full shape including channels/batch
- Derive output shape from img.shape, overriding spatial axes
- Prefer jax.image.resize for spatial-only sizes
When it happens
Trigger: Passing a 2-element spatial shape for a 3D (H,W,C) image, or a 4-element shape for a batched (N,H,W,C) image, to scale_and_translate (and via _resize).
Common situations: Coming from cv2/torchvision where resize takes only (height, width); forgetting channel/batch dims; computing shape from a different tensor than the image passed in.
Related errors
- unexpected JAX type (e.g. shape/dtype) for argument to VJP f
- cotangent type does not match function output, expected {out
- Mismatched number of outputs from callback. Expected: {}, Ac
- Incorrect output shape for return value #{i}: Expected: {out
- Length of sharding.spec ({len(out_s.spec)}) must be equal to
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/8318635ae72efba3.
Report an issue: GitHub.