Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(applications): Improve validation and error handling for ConvNeXt weights and fix broadcasting in EfficientNetV2 #20785

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions keras/src/applications/convnext.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,30 @@ def ConvNeXt(

model = Functional(inputs=inputs, outputs=x, name=name)

# Validate weights before requesting them from the API
if weights == "imagenet":
expected_config = MODEL_CONFIGS[weights_name.split("convnext_")[-1]]
if (
depths != expected_config["depths"]
or projection_dims != expected_config["projection_dims"]
):
raise ValueError(
f"Architecture configuration does not match {weights_name} "
f"variant. When using pre-trained weights, the model "
f"architecture must match the pre-trained configuration "
f"exactly. Expected depths: {expected_config['depths']}, "
f"got: {depths}. Expected projection_dims: "
f"{expected_config['projection_dims']}, got: {projection_dims}."
)

if weights_name not in name:
raise ValueError(
f'Model name "{name}" does not match weights variant '
f'"{weights_name}". When using imagenet weights, model name '
f'must contain the weights variant (e.g., "convnext_'
f'{weights_name.split("convnext_")[-1]}").'
)

# Load weights.
if weights == "imagenet":
if include_top:
Expand Down
12 changes: 10 additions & 2 deletions keras/src/applications/efficientnet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,17 @@ def EfficientNetV2(
num_channels = input_shape[bn_axis - 1]
if name.split("-")[-1].startswith("b") and num_channels == 3:
x = layers.Rescaling(scale=1.0 / 255)(x)
if backend.image_data_format() == "channels_first":
mean = [[[[0.485]], [[0.456]], [[0.406]]]] # shape [1,3,1,1]
variance = [
[[[0.229**2]], [[0.224**2]], [[0.225**2]]]
] # shape [1,3,1,1]
else:
mean = [0.485, 0.456, 0.406]
variance = [0.229**2, 0.224**2, 0.225**2]
x = layers.Normalization(
mean=[0.485, 0.456, 0.406],
variance=[0.229**2, 0.224**2, 0.225**2],
mean=mean,
variance=variance,
axis=bn_axis,
)(x)
else:
Expand Down
Loading