{"record":{"id":"8318635ae72efba3","repo":"jax-ml/jax","slug":"shape-must-have-length-equal-to-the-number-of-dime","errorCode":null,"errorMessage":"shape must have length equal to the number of dimensions of x;  {shape} vs {image.shape}","messagePattern":"shape must have length equal to the number of dimensions of x;  (.+?) vs (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/image/scale.py","lineNumber":374,"sourceCode":"      passed scale and translation should be applied to.\n    scale: A [K] array with the same number of dimensions as image, containing\n      the scale to apply in each dimension.\n    translation: A [K] array with the same number of dimensions as image,\n      containing the translation to apply in each dimension.\n    method: the resizing method to use; either a ``ResizeMethod`` instance or a\n      string. Available methods are: ``LINEAR``, ``LANCZOS3``, ``LANCZOS5``,\n      ``CUBIC``, ``CUBIC_PYTORCH``, ``AREA``.\n    antialias: Should an antialiasing filter be used when downsampling? Defaults\n      to ``True``. Has no effect when upsampling.\n\n  Returns:\n    The scale and translated image.\n  \"\"\"\n  shape = core.canonicalize_shape(shape)\n  if len(shape) != image.ndim:\n    msg = ('shape must have length equal to the number of dimensions of x; '\n           f' {shape} vs {image.shape}')\n    raise ValueError(msg)\n  if isinstance(method, str):\n    method = ResizeMethod.from_string(method)\n  if method == ResizeMethod.NEAREST:\n    # Nearest neighbor is currently special-cased for straight resize, so skip\n    # for now.\n    raise ValueError('Nearest neighbor resampling is not currently supported '\n                     'for scale_and_translate.')\n  assert isinstance(method, ResizeMethod)\n\n  if method == ResizeMethod.CUBIC_PYTORCH and antialias:\n    method = ResizeMethod.CUBIC\n  radius, kernel = _kernels[method]\n  edge_padding = (method == ResizeMethod.CUBIC_PYTORCH and not antialias)\n  image, = promote_dtypes_inexact(image)\n  scale, translation = promote_dtypes_inexact(scale, translation)\n  return _scale_and_translate(\n     image, shape, spatial_dims, scale, translation, kernel, antialias,\n     precision, edge_padding=edge_padding, radius=radius)","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/image/scale.py#L356-L392","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"# before\nimg = jnp.zeros((224, 224, 3))\nout = scale_and_translate(img, (112, 112), ...)  # ValueError\n# after\nout = scale_and_translate(img, (112, 112, 3), ...)\n# or jax.image.resize(img, (112, 112), method='linear')","handlingStrategy":"validation","validationCode":"assert len(shape) == image.ndim, f'need full output shape of length {image.ndim}'\nscale_and_translate(image, shape, scale, translation, method)","typeGuard":"# no static type narrowing available; runtime check only","tryCatchPattern":null,"preventionTips":["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"],"tags":["jax","image-resize","shape-mismatch"],"backgroundTag":"image-shape-dimension-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}