-
Notifications
You must be signed in to change notification settings - Fork 1
/
residual_attention_network.py
54 lines (49 loc) · 2.05 KB
/
residual_attention_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import torch.nn as nn
from attention_module import AttentionModule
from basic_layers import ResidualBlock
class ResidualAttentionModel(nn.Module):
def __init__(self, num_class):
super(ResidualAttentionModel, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.mpool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.residual_block1 = ResidualBlock(64, 256)
self.attention_module1 = AttentionModule(256, 256, (56, 56), (28, 28), (14, 14))
self.residual_block2 = ResidualBlock(256, 512, 2)
self.attention_module2 = AttentionModule(512, 512, (28, 28), (14, 14), (7, 7))
self.residual_block3 = ResidualBlock(512, 1024, 2)
self.attention_module3 = AttentionModule(1024, 1024, (14, 14), (7, 7), (4, 4))
self.residual_block4 = ResidualBlock(1024, 2048, 2)
self.residual_block5 = ResidualBlock(2048, 2048)
self.residual_block6 = ResidualBlock(2048, 2048)
self.mpool2 = nn.Sequential(
nn.BatchNorm2d(2048),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=7, stride=1)
)
self.num_class = num_class
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(2048, num_class)
def forward(self, x):
out = self.conv1(x)
out = self.mpool1(out)
# print(out.data)
out = self.residual_block1(out)
out = self.attention_module1(out)
out = self.residual_block2(out)
out = self.attention_module2(out)
out = self.residual_block3(out)
# print(out.data)
out = self.attention_module3(out)
out = self.residual_block4(out)
out = self.residual_block5(out)
out = self.residual_block6(out)
out = self.mpool2(out)
out = self.avgpool(out)
# identique a x = torch.squeeze(x
out = out.view(out.size(0), -1)
out = self.fc(out)
return out