forked from xmu-xiaoma666/External-Attention-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
361a74b
commit f0b0312
Showing
5 changed files
with
78 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import numpy as np | ||
import torch | ||
from torch import nn | ||
from torch.nn import init | ||
from collections import OrderedDict | ||
|
||
|
||
|
||
class ECAAttention(nn.Module): | ||
|
||
def __init__(self, kernel_size=3): | ||
super().__init__() | ||
self.gap=nn.AdaptiveAvgPool2d(1) | ||
self.conv=nn.Conv1d(1,1,kernel_size=kernel_size,padding=(kernel_size-1)//2) | ||
self.sigmoid=nn.Sigmoid() | ||
|
||
def init_weights(self): | ||
for m in self.modules(): | ||
if isinstance(m, nn.Conv2d): | ||
init.kaiming_normal_(m.weight, mode='fan_out') | ||
if m.bias is not None: | ||
init.constant_(m.bias, 0) | ||
elif isinstance(m, nn.BatchNorm2d): | ||
init.constant_(m.weight, 1) | ||
init.constant_(m.bias, 0) | ||
elif isinstance(m, nn.Linear): | ||
init.normal_(m.weight, std=0.001) | ||
if m.bias is not None: | ||
init.constant_(m.bias, 0) | ||
|
||
def forward(self, x): | ||
y=self.gap(x) #bs,c,1,1 | ||
y=y.squeeze(-1).permute(0,2,1) #bs,1,c | ||
y=self.conv(y) #bs,1,c | ||
y=self.sigmoid(y) #bs,1,c | ||
y=y.permute(0,2,1).unsqueeze(-1) #bs,c,1,1 | ||
return x*y.expand_as(x) | ||
|
||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
input=torch.randn(50,512,7,7) | ||
eca = ECAAttention(kernel_size=3) | ||
output=eca(input) | ||
print(output.shape) | ||
|
||
|
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1,8 +1,7 @@ | ||
from mlp.resmlp import ResMLP | ||
from attention.ECAAttention import ECAAttention | ||
import torch | ||
|
||
|
||
input=torch.randn(50,3,14,14) | ||
resmlp=ResMLP(dim=128,image_size=14,patch_size=7,class_num=1000) | ||
out=resmlp(input) | ||
print(out.shape) | ||
input=torch.randn(50,512,7,7) | ||
eca = ECAAttention(kernel_size=3) | ||
output=eca(input) | ||
print(output.shape) |