keras-team/keras · error · ValueError
Expected the input image to be rank 3 or 4. Received…
Error message
Expected the input image to be rank 3 or 4. Received: inputs.shape={images_shape} What it means
RandomPosterization.get_random_transformation draws the number of bits to keep per sample and needs the batch size, which it infers only from rank-3 (single image, height/width/channels) or rank-4 (batched) inputs. This ValueError fires when the images tensor has any other rank.
Solutions
- Pass a rank-3 tensor (height, width, channels) for a single image
- Pass a rank-4 tensor (batch_size, height, width, channels) for a batch
- Add the missing axis with images[None, ...] or images[..., None]
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/layers/preprocessing/image_preprocessing/random_posterization.py:80 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2d9c0d6252a59218.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/image_preprocessing/random_posterization.py:80
raise ValueError(
self._VALUE_RANGE_VALIDATION_ERROR
+ f"Received: value_range={value_range}"
)
self.value_range = sorted(value_range)
def get_random_transformation(self, data, training=True, seed=None):
if isinstance(data, dict):
images = data["images"]
else:
images = data
images_shape = self.backend.shape(images)
rank = len(images_shape)
if rank == 3:
batch_size = 1
elif rank == 4:
batch_size = images_shape[0]
else:
raise ValueError(
"Expected the input image to be rank 3 or 4. Received: "
f"inputs.shape={images_shape}"
)
if seed is None:
seed = self._get_seed_generator(self.backend._backend)
if self.factor[0] != self.factor[1]:
factor = self.backend.random.randint(
(batch_size,),
minval=self.factor[0],
maxval=self.factor[1],
seed=seed,
dtype="uint8",
)
else:
factor = (
self.backend.numpy.ones((batch_size,), dtype="uint8")View on GitHub (pinned to 7a34a03db6)