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
7c083fc
commit 80c92b3
Showing
5 changed files
with
87 additions
and
11 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,59 @@ | ||
import numpy as np | ||
import torch | ||
from torch import nn | ||
from torch.nn import init | ||
from .SelfAttention import ScaledDotProductAttention | ||
from .SimplifiedSelfAttention import SimplifiedScaledDotProductAttention | ||
|
||
class PositionAttentionModule(nn.Module): | ||
|
||
def __init__(self,d_model=512,kernel_size=3,H=7,W=7): | ||
super().__init__() | ||
self.cnn=nn.Conv2d(d_model,d_model,kernel_size=kernel_size,padding=(kernel_size-1)//2) | ||
self.pa=ScaledDotProductAttention(d_model,d_k=d_model,d_v=d_model,h=1) | ||
|
||
def forward(self,x): | ||
bs,c,h,w=x.shape | ||
y=self.cnn(x) | ||
y=y.view(bs,c,-1).permute(0,2,1) #bs,h*w,c | ||
y=self.pa(y,y,y) #bs,h*w,c | ||
return y | ||
|
||
|
||
class ChannelAttentionModule(nn.Module): | ||
|
||
def __init__(self,d_model=512,kernel_size=3,H=7,W=7): | ||
super().__init__() | ||
self.cnn=nn.Conv2d(d_model,d_model,kernel_size=kernel_size,padding=(kernel_size-1)//2) | ||
self.pa=SimplifiedScaledDotProductAttention(H*W,h=1) | ||
|
||
def forward(self,x): | ||
bs,c,h,w=x.shape | ||
y=self.cnn(x) | ||
y=y.view(bs,c,-1) #bs,c,h*w | ||
y=self.pa(y,y,y) #bs,c,h*w | ||
return y | ||
|
||
|
||
|
||
|
||
class DAModule(nn.Module): | ||
|
||
def __init__(self,d_model=512,kernel_size=3,H=7,W=7): | ||
super().__init__() | ||
self.position_attention_module=PositionAttentionModule(d_model=512,kernel_size=3,H=7,W=7) | ||
self.channel_attention_module=ChannelAttentionModule(d_model=512,kernel_size=3,H=7,W=7) | ||
|
||
def forward(self,input): | ||
bs,c,h,w=input.shape | ||
p_out=self.position_attention_module(input) | ||
c_out=self.channel_attention_module(input) | ||
p_out=p_out.permute(0,2,1).view(bs,c,h,w) | ||
c_out=c_out.view(bs,c,h,w) | ||
return p_out+c_out | ||
|
||
|
||
if __name__ == '__main__': | ||
input=torch.randn(50,512,7,7) | ||
danet=DAModule(d_model=512,kernel_size=3,H=7,W=7) | ||
print(danet(input).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,13 +1,7 @@ | ||
from mlp.g_mlp import gMLP | ||
from attention.DANet import DAModule | ||
import torch | ||
|
||
if __name__ == '__main__': | ||
|
||
num_tokens=10000 | ||
bs=50 | ||
len_sen=49 | ||
num_layers=6 | ||
input=torch.randint(num_tokens,(bs,len_sen)) #bs,len_sen | ||
gmlp = gMLP(num_tokens=num_tokens,len_sen=len_sen,dim=512,d_ff=1024) | ||
output=gmlp(input) | ||
print(output.shape) | ||
input=torch.randn(50,512,7,7) | ||
danet=DAModule(d_model=512,kernel_size=3,H=7,W=7) | ||
print(danet(input).shape) |