-
Notifications
You must be signed in to change notification settings - Fork 35
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
Implement SK Module in Glasses #283
Open
rentainhe
wants to merge
6
commits into
FrancescoSaverioZuppichini:develop
Choose a base branch
from
rentainhe:add_skmodule
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
26a37a6
add reimplement SKAttn Module
rentainhe ffea808
complete skmodule
rentainhe 49f091b
update test
rentainhe 71fce91
update SKModule
rentainhe 61df784
Merge branch 'develop' into add_skmodule
e3f0645
Merge branch 'develop' into add_skmodule
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,98 @@ | ||
import torch | ||
import torch.nn as nn | ||
from typing import Union, List | ||
|
||
from glasses.nn.att.utils import make_divisible | ||
from ..blocks import ConvBnAct | ||
from einops.layers.torch import Rearrange, Reduce | ||
|
||
def _kernel_valid(k): | ||
if isinstance(k, (list, tuple)): | ||
for ki in k: | ||
return _kernel_valid(ki) | ||
assert k >=3 and k % 2 | ||
|
||
class SelectiveKernelAtt(nn.Module): | ||
def __init__( | ||
self, | ||
features: int, | ||
num_paths: int = 2, | ||
mid_features: int = 32, | ||
act_layer: nn.Module = nn.ReLU, | ||
norm_layer: nn.Module = nn.BatchNorm2d, | ||
): | ||
super().__init__() | ||
self.num_paths = num_paths | ||
self.att = nn.Sequential( | ||
Reduce("b n c h w -> b c h w", reduction="sum"), | ||
Reduce("b c h w -> b c 1 1", reduction="mean"), | ||
nn.Conv2d(features, mid_features, kernel_size=1, bias=False), | ||
norm_layer(mid_features), | ||
act_layer(inplace=True), | ||
nn.Conv2d(mid_features, features * num_paths, kernel_size=1, bias=False), | ||
Rearrange('b (n c) h w -> b n c h w', n=num_paths, c=features), | ||
nn.Softmax(dim=1), | ||
) | ||
|
||
def forward(self, x): | ||
assert x.shape[1] == self.num_paths | ||
x = self.att(x) | ||
return x | ||
|
||
|
||
class SelectiveKernel(nn.Module): | ||
def __init__( | ||
self, | ||
in_features: int, | ||
out_features: int = None, | ||
kernel_size: Union[List, int] = None, | ||
stride: int = 1, | ||
dilation: int = 1, | ||
groups: int = 1, | ||
reduction: int = 16, | ||
reduction_divisor: int = 8, | ||
reduced_features: int = None, | ||
keep_3x3: bool = True, | ||
activation: nn.Module = nn.ReLU, | ||
normalization: nn.Module = nn.BatchNorm2d, | ||
): | ||
super().__init__() | ||
out_features = out_features or in_features | ||
kernel_size = kernel_size or [3, 5] | ||
_kernel_valid(kernel_size) | ||
if not isinstance(kernel_size, list): | ||
kernel_size = [kernel_size] * 2 | ||
if keep_3x3: | ||
dilation = [dilation * (k - 1) // 2 for k in kernel_size] | ||
kernel_size = [3] * len(kernel_size) | ||
else: | ||
dilation = [dilation] * len(kernel_size) | ||
self.num_paths = len(kernel_size) | ||
self.in_features = in_features | ||
self.out_features = out_features, | ||
groups = min(out_features, groups) | ||
|
||
self.paths = nn.ModuleList([ | ||
ConvBnAct(in_features = in_features, | ||
out_features = out_features, | ||
activation = activation, | ||
normalization=normalization, | ||
mode = "same", | ||
stride=stride, | ||
kernel_size=k, | ||
dilation=d) | ||
for k, d in zip(kernel_size, dilation) | ||
]) | ||
|
||
attn_features = reduced_features or make_divisible(out_features // reduction, divisor=reduction_divisor) | ||
self.attn = SelectiveKernelAtt(out_features, self.num_paths, attn_features) | ||
|
||
def forward(self, x): | ||
x_paths = [op(x) for op in self.paths] | ||
x = torch.stack(x_paths, dim=1) | ||
x_attn = self.attn(x) | ||
x = x * x_attn | ||
return torch.sum(x, dim=1) | ||
|
||
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cuz there is BatchNorm2d in SelectiveKernel, it will turn out some error with batch=1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! put the module in
.eval()
mode