This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Squeeze and Excitation to ResNeXt models (#426)
Summary: Pull Request resolved: #426 Added a `SqueezeAndExcitation` layer to a new sub-package, `models.common` (open to other suggestions, I didn't want to have a `generic.py` or `util.py` as that is too vague and broad). Plugged in the layer to `ResNeXt` models. Differential Revision: D20283172 fbshipit-source-id: 21d5183a61d7aa13fca094afe95ecb0aa18f1632
- Loading branch information
1 parent
636740b
commit ea6a56b
Showing
4 changed files
with
150 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Facebook, Inc. and its affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
|
||
import torch.nn as nn | ||
|
||
|
||
class SqueezeAndExcitationLayer(nn.Module): | ||
"""Squeeze and excitation layer, as per https://arxiv.org/pdf/1709.01507.pdf""" | ||
|
||
def __init__(self, in_planes, reduction_ratio=16): | ||
super().__init__() | ||
self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) | ||
reduced_planes = in_planes // reduction_ratio | ||
self.excitation = nn.Sequential( | ||
nn.Conv2d(in_planes, reduced_planes, kernel_size=1, stride=1, bias=True), | ||
nn.ReLU(), | ||
nn.Conv2d(reduced_planes, in_planes, kernel_size=1, stride=1, bias=True), | ||
nn.Sigmoid(), | ||
) | ||
|
||
def forward(self, x): | ||
x_squeezed = self.avgpool(x) | ||
x_excited = self.excitation(x_squeezed) | ||
x_scaled = x * x_excited | ||
return x_scaled |
Oops, something went wrong.