keras-team/keras · error · ValueError

weights_path undefined

Error message

weights_path undefined

What it means

The cropping operation's compute_output_spec only accepts rank-3 or rank-4 image tensors; any other rank raises this ValueError before cropping math runs.

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)

Solutions

  1. Expand dims to (H,W,C) or (N,H,W,C)
  2. For rank-5 data, loop/reshape frames to rank-4
  3. 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

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


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/4d142341126666e9. Report an issue: GitHub.