【Deep learning model】Dynamic Convolution: Attention over Convolution Kernels

解释:

https://blog.csdn.net/weixin_42096202/article/details/103494599

code:

https://blog.csdn.net/sunlanchang/article/details/103820811

 

 1 class DYconv(nn.Module):
 2 
 3     def __init__(self, inchannel,outchannel ,kernel_size,stride,reduction=4,K=4,t=30):
 4         super(DYconv, self).__init__()
 5 
 6         self.t=t
 7         self.K = K
 8         self.kernel_size = kernel_size
 9         self.stride = stride
10 
11         self.avg_pool = nn.AdaptiveAvgPool2d(1)
12         self.fc = nn.Sequential(
13             nn.Linear(inchannel, inchannel // reduction, bias=False),
14             nn.ReLU(inplace=True),
15             nn.Linear(inchannel // reduction, self.K, bias=False),
16 
17         )
18         self.conv = nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=1, padding=1)
19         self.conv1 = nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=1, padding=1,dilation=1)
20         self.conv2 = nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=1, padding=2,dilation=2)
21         self.conv3 = nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=1, padding=3,dilation=3)
22 
23         #self.convs = nn.ModuleList()
24 
25 
26         # for i in range(self.K):
27         #     l_conv = nn.Conv2d(inchannel, outchannel, kernel_size=kernel_size, stride=stride, padding=padding)
28         #     self.convs.append(l_conv)
29 
30 
31 
32     def forward(self, x):
33         a,b,c,d=x.shape
34 
35         y = self.avg_pool(x).view(a,b)
36         #print(x.shape,y.shape)
37         y = self.fc(y)
38         #print(y.shape)
39         ax = F.softmax(y/self.t,dim = 1)
40         #print(ax.shape,ax[:,0])
41         #out=[]
42         #print(self.conv(x).shape,ax[:,0].shape,ax)
43         # for i,conv in enumerate(self.convs):
44         #     out+=conv(x)*ax[:,i].view(a,1,1,1)
45         out = self.conv(x)*ax[:,0].view(a,1,1,1)+self.conv1(x)*ax[:,1].view(a,1,1,1)+self.conv2(x)*ax[:,2].view(a,1,1,1)+self.conv3(x)*ax[:,3].view(a,1,1,1)
46 
47         return out
48 ————————————————
49 版权声明:本文为CSDN博主「sunlanchang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
50 原文链接:https://blog.csdn.net/sunlanchang/java/article/details/103820811

 

posted on 2020-06-09 17:00  LocalMinima  阅读(346)  评论(0)    收藏  举报