keras-team/keras · error · ValueError
The `weights` argument should be either `None` (random initi
Error message
The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.
What it means
With padding=same, reconstruct_patches tolerates output sizes smaller than grid*patch_size (down to just above (grid-1)*p, i.e. at most one patch worth of overlap trimmed), because same padding guarantees coverage. An output_size outside ((g-1)*p, g*p] per axis would leave pixels uncovered or over-cover them, so it is rejected.
Source
Thrown at keras/src/applications/densenet.py:192
classifier_activation: A `str` or callable.
The activation function to use
on the "top" layer. Ignored unless `include_top=True`. Set
`classifier_activation=None` to return the logits of the "top"
layer. When loading pretrained weights, `classifier_activation`
can only be `None` or `"softmax"`.
name: The name of the model (string).
Returns:
A model instance.
"""
if backend.image_data_format() == "channels_first":
raise ValueError(
"DenseNet does not support the `channels_first` image data "
"format. Switch to `channels_last` by editing your local "
"config file at ~/.keras/keras.json"
)
if not (weights in {"imagenet", None} or file_utils.exists(weights)):
raise ValueError(
"The `weights` argument should be either "
"`None` (random initialization), `imagenet` "
"(pre-training on ImageNet), "
"or the path to the weights file to be loaded."
)
if weights == "imagenet" and include_top and classes != 1000:
raise ValueError(
'If using `weights` as `"imagenet"` with `include_top`'
" as true, `classes` should be 1000"
)
# Determine proper input shape
input_shape = imagenet_utils.obtain_input_shape(
input_shape,
default_size=224,
min_size=32,
data_format=backend.image_data_format(),View on GitHub (pinned to 7a34a03db6)
Solutions
- Choose output_size per axis in the range ((grid-1)*p, grid*p]; the common correct choice is grid*p or the original pre-pad input size
- Drop output_size to let Keras infer grid*p
- If you truly need a smaller output, crop after reconstruction instead of requesting an invalid size
Example fix
before: reconstruct_patches(p, size=(8,8), padding="same", output_size=(24,24)) -> ValueError; after: out = reconstruct_patches(p, size=(8,8), padding="same"); out = out[:, :24, :24, :]
Defensive patterns
Strategy: validation
Validate before calling
for g, p, o in zip(grid_dims, patch_dims, output_size):
assert g * p - p < o <= g * p, f"same padding needs o in ({g*p-p}, {g*p}], got {o}" Type guard
def same_padding_ok(grid, patch, out) -> bool:
return all(g * p - p < o <= g * p for g, p, o in zip(grid, patch, out)) Prevention
- Pick output_size = grid*p or the pre-pad input size
- Crop after reconstruction instead of requesting sub-floor sizes
- Check each axis independently; one bad axis fails the whole call
When it happens
Trigger: output_size=(24, 24) with grid=4, patch=8 (24 <= (4-1)*8=24, boundary excluded); output_size larger than grid*p (e.g. 40 with grid=4, p=8); axis-wise mistakes where height is fine but width falls below the floor.
Common situations: Reconstructing to the original padded input size after cropping; mixing per-axis conventions (one axis valid, one same); assuming output_size can be arbitrary when padding=same.
Related errors
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
- If using `weights="imagenet"` with `include_top=True`, `clas
- DenseNet does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/6eda9c0d45c5be4f.
Report an issue: GitHub.