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

Allow dropout overwrites on EfficientNet #7031

Merged
merged 2 commits into from
Dec 12, 2022
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
8 changes: 5 additions & 3 deletions test/smoke_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def smoke_test_torchvision() -> None:
all(x is not None for x in [torch.ops.image.decode_png, torch.ops.torchvision.roi_align]),
)


pmeier marked this conversation as resolved.
Show resolved Hide resolved
def smoke_test_torchvision_read_decode() -> None:
img_jpg = read_image(str(SCRIPT_DIR / "assets" / "encode_jpeg" / "grace_hopper_517x606.jpg"))
if img_jpg.ndim != 3 or img_jpg.numel() < 100:
Expand All @@ -25,6 +26,7 @@ def smoke_test_torchvision_read_decode() -> None:
if img_png.ndim != 3 or img_png.numel() < 100:
raise RuntimeError(f"Unexpected shape of img_png: {img_png.shape}")


def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None:
img = read_image(str(SCRIPT_DIR / ".." / "gallery" / "assets" / "dog2.jpg")).to(device)

Expand All @@ -47,9 +49,8 @@ def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None:
expected_category = "German shepherd"
print(f"{category_name} ({device}): {100 * score:.1f}%")
if category_name != expected_category:
raise RuntimeError(
f"Failed ResNet50 classify {category_name} Expected: {expected_category}"
)
raise RuntimeError(f"Failed ResNet50 classify {category_name} Expected: {expected_category}")


def main() -> None:
print(f"torchvision: {torchvision.__version__}")
Expand All @@ -59,5 +60,6 @@ def main() -> None:
if torch.cuda.is_available():
smoke_test_torchvision_resnet50_classify("cuda")


if __name__ == "__main__":
main()
42 changes: 31 additions & 11 deletions torchvision/models/efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,9 @@ def efficientnet_b0(
weights = EfficientNet_B0_Weights.verify(weights)

inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b0", width_mult=1.0, depth_mult=1.0)
return _efficientnet(inverted_residual_setting, 0.2, last_channel, weights, progress, **kwargs)
return _efficientnet(
inverted_residual_setting, kwargs.pop("dropout", 0.2), last_channel, weights, progress, **kwargs
pmeier marked this conversation as resolved.
Show resolved Hide resolved
)


@register_model()
Expand Down Expand Up @@ -808,7 +810,9 @@ def efficientnet_b1(
weights = EfficientNet_B1_Weights.verify(weights)

inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b1", width_mult=1.0, depth_mult=1.1)
return _efficientnet(inverted_residual_setting, 0.2, last_channel, weights, progress, **kwargs)
return _efficientnet(
inverted_residual_setting, kwargs.pop("dropout", 0.2), last_channel, weights, progress, **kwargs
)


@register_model()
Expand Down Expand Up @@ -837,7 +841,9 @@ def efficientnet_b2(
weights = EfficientNet_B2_Weights.verify(weights)

inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b2", width_mult=1.1, depth_mult=1.2)
return _efficientnet(inverted_residual_setting, 0.3, last_channel, weights, progress, **kwargs)
return _efficientnet(
inverted_residual_setting, kwargs.pop("dropout", 0.3), last_channel, weights, progress, **kwargs
)


@register_model()
Expand Down Expand Up @@ -866,7 +872,14 @@ def efficientnet_b3(
weights = EfficientNet_B3_Weights.verify(weights)

inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b3", width_mult=1.2, depth_mult=1.4)
return _efficientnet(inverted_residual_setting, 0.3, last_channel, weights, progress, **kwargs)
return _efficientnet(
inverted_residual_setting,
kwargs.pop("dropout", 0.3),
last_channel,
weights,
progress,
**kwargs,
)


@register_model()
Expand Down Expand Up @@ -895,7 +908,14 @@ def efficientnet_b4(
weights = EfficientNet_B4_Weights.verify(weights)

inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b4", width_mult=1.4, depth_mult=1.8)
return _efficientnet(inverted_residual_setting, 0.4, last_channel, weights, progress, **kwargs)
return _efficientnet(
inverted_residual_setting,
kwargs.pop("dropout", 0.4),
last_channel,
weights,
progress,
**kwargs,
)


@register_model()
Expand Down Expand Up @@ -926,7 +946,7 @@ def efficientnet_b5(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b5", width_mult=1.6, depth_mult=2.2)
return _efficientnet(
inverted_residual_setting,
0.4,
kwargs.pop("dropout", 0.4),
last_channel,
weights,
progress,
Expand Down Expand Up @@ -963,7 +983,7 @@ def efficientnet_b6(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b6", width_mult=1.8, depth_mult=2.6)
return _efficientnet(
inverted_residual_setting,
0.5,
kwargs.pop("dropout", 0.5),
last_channel,
weights,
progress,
Expand Down Expand Up @@ -1000,7 +1020,7 @@ def efficientnet_b7(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_b7", width_mult=2.0, depth_mult=3.1)
return _efficientnet(
inverted_residual_setting,
0.5,
kwargs.pop("dropout", 0.5),
last_channel,
weights,
progress,
Expand Down Expand Up @@ -1038,7 +1058,7 @@ def efficientnet_v2_s(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_v2_s")
return _efficientnet(
inverted_residual_setting,
0.2,
kwargs.pop("dropout", 0.2),
last_channel,
weights,
progress,
Expand Down Expand Up @@ -1076,7 +1096,7 @@ def efficientnet_v2_m(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_v2_m")
return _efficientnet(
inverted_residual_setting,
0.3,
kwargs.pop("dropout", 0.3),
last_channel,
weights,
progress,
Expand Down Expand Up @@ -1114,7 +1134,7 @@ def efficientnet_v2_l(
inverted_residual_setting, last_channel = _efficientnet_conf("efficientnet_v2_l")
return _efficientnet(
inverted_residual_setting,
0.4,
kwargs.pop("dropout", 0.4),
last_channel,
weights,
progress,
Expand Down