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 277
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
Summary: 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: bda97a6723182dc7503ee139aa8e688b87a41d1a
- Loading branch information
1 parent
636740b
commit c182823
Showing
4 changed files
with
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/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. | ||
|
||
from pathlib import Path | ||
|
||
from classy_vision.generic.registry_utils import import_all_modules | ||
|
||
|
||
FILE_ROOT = Path(__file__).parent | ||
|
||
|
||
# automatically import any Python files in the models/common directory | ||
import_all_modules(FILE_ROOT, "classy_vision.models.common") | ||
|
||
from .squeeze_and_excitation_layer import SqueezeAndExcitationLayer # isort:skip | ||
|
||
|
||
__all__ = ["SqueezeAndExcitationLayer"] |
29 changes: 29 additions & 0 deletions
29
classy_vision/models/common/squeeze_and_excitation_layer.py
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 |
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
Oops, something went wrong.