keras-team/keras · error · ValueError
weights_path undefined
Error message
weights_path undefined
What it means
In DenseNet construction (keras/src/applications/densenet.py) the if/elif chain over block configurations only assigns `weights_path` for the known DenseNet121/169/201 block layouts; for any other `blocks` pattern no branch matches and weights_path stays undefined when the code later tries to load weights.
Solutions
- Expand dims to (H,W,C) or (N,H,W,C)
- For rank-5 data, loop/reshape frames to rank-4
- Assert the rank before calling
Example fix
# before out = ops.image.crop_images(mask, ...) # (H, W) # after out = ops.image.crop_images(mask[..., None], ...) # (H, W, 1)
Defensive patterns
Strategy: type-guard
Validate before calling
shape = ops.shape(images)
if len(shape) == 2:
images = images[..., None] Type guard
def has_image_rank(images) -> bool:
return len(images.shape) in (3, 4) Try / catch
try:
out = ops.image.crop_images(images, ...)
except ValueError as e:
if 'Invalid images rank' in str(e):
images = images[..., None]
out = ops.image.crop_images(images, ...)
else:
raise Prevention
- Always carry a channel axis in image pipelines
- Assert rank before crop/pad ops
When it happens
Trigger: Calling ops.image.crop_images (or its layer) with rank-2 masks, rank-5 video tensors, or mis-shaped numpy inputs.
Common situations: Grayscale (H,W) masks without a channel axis; video batches; feeding flat feature vectors by mistake.
Related errors
- Expected data_format to be one of `channels_first` or…
- Expected mode to be one of `caffe`, `tf` or `torch`…
- Expected the input image to be rank 3 or 4. Received…
- If using `weights="imagenet"` as true, `classes` should be…
- If using `weights="imagenet"` as true, `classes` should be…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4d142341126666e9.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/applications/densenet.py:293
cache_subdir="models",
file_hash="9d60b8095a5708f2dcce2bca79d332c7",
)
elif blocks == [6, 12, 32, 32]:
weights_path = file_utils.get_file(
"densenet169_weights_tf_dim_ordering_tf_kernels.h5",
DENSENET169_WEIGHT_PATH,
cache_subdir="models",
file_hash="d699b8f76981ab1b30698df4c175e90b",
)
elif blocks == [6, 12, 48, 32]:
weights_path = file_utils.get_file(
"densenet201_weights_tf_dim_ordering_tf_kernels.h5",
DENSENET201_WEIGHT_PATH,
cache_subdir="models",
file_hash="1ceb130c1ea1b78c3bf6114dbdfd8807",
)
else:
raise ValueError("weights_path undefined")
else:
if blocks == [6, 12, 24, 16]:
weights_path = file_utils.get_file(
"densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5",
DENSENET121_WEIGHT_PATH_NO_TOP,
cache_subdir="models",
file_hash="30ee3e1110167f948a6b9946edeeb738",
)
elif blocks == [6, 12, 32, 32]:
weights_path = file_utils.get_file(
"densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5",
DENSENET169_WEIGHT_PATH_NO_TOP,
cache_subdir="models",
file_hash="b8c4d4c20dd625c148057b9ff1c1176b",
)
elif blocks == [6, 12, 48, 32]:
weights_path = file_utils.get_file(
"densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5",View on GitHub (pinned to 7a34a03db6)