Skip to content

Allow custom activation in SqueezeExcitation of EfficientNet #4448

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

Merged
merged 4 commits into from
Sep 21, 2021
Merged
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
16 changes: 12 additions & 4 deletions torchvision/models/efficientnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@


class SqueezeExcitation(nn.Module):
def __init__(self, input_channels: int, squeeze_channels: int):
def __init__(
self,
input_channels: int,
squeeze_channels: int,
activation: Callable[..., nn.Module] = nn.ReLU,
scale_activation: Callable[..., nn.Module] = nn.Sigmoid,
) -> None:
super().__init__()
self.fc1 = nn.Conv2d(input_channels, squeeze_channels, 1)
self.fc2 = nn.Conv2d(squeeze_channels, input_channels, 1)
self.activation = activation()
self.scale_activation = scale_activation()

def _scale(self, input: Tensor) -> Tensor:
scale = F.adaptive_avg_pool2d(input, 1)
scale = self.fc1(scale)
scale = F.silu(scale, inplace=True)
scale = self.activation(scale)
scale = self.fc2(scale)
return scale.sigmoid()
return self.scale_activation(scale)

def forward(self, input: Tensor) -> Tensor:
scale = self._scale(input)
Expand Down Expand Up @@ -108,7 +116,7 @@ def __init__(self, cnf: MBConvConfig, stochastic_depth_prob: float, norm_layer:

# squeeze and excitation
squeeze_channels = max(1, cnf.input_channels // 4)
layers.append(se_layer(expanded_channels, squeeze_channels))
layers.append(se_layer(expanded_channels, squeeze_channels, activation=partial(nn.SiLU, inplace=True)))

# project
layers.append(ConvBNActivation(expanded_channels, cnf.out_channels, kernel_size=1, norm_layer=norm_layer,
Expand Down