-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
executable file
·62 lines (47 loc) · 2.31 KB
/
model.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
55
56
57
58
59
60
61
62
import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
class SimAM(nn.Module):
def __init__(self, eps=1e-4):
super(SimAM, self).__init__()
self.eps = eps
def forward(self, x):
b, c, h, w = x.size()
n = h * w
d = (x - x.mean(dim=[2, 3], keepdim=True)).pow(2)
y = d / (4 * (d.sum(dim=[2, 3], keepdim=True) / n + self.eps)) + 0.5
return torch.sigmoid(y)
class EnergyAttention(nn.Module):
def __init__(self, low_dim, high_dim):
super(EnergyAttention, self).__init__()
self.conv = nn.Conv2d(high_dim, low_dim, kernel_size=3, padding=1)
self.atte = SimAM()
def forward(self, low_feat, high_feat):
high_feat = F.interpolate(self.conv(high_feat), low_feat.size()[-2:], mode='bilinear', align_corners=False)
atte = self.atte(torch.relu(low_feat + high_feat))
low_feat = atte * low_feat
return atte, low_feat
class Model(nn.Module):
def __init__(self, backbone_type, proj_dim):
super(Model, self).__init__()
# backbone
self.backbone = timm.create_model('seresnet50' if backbone_type == 'resnet50' else 'vgg16_bn',
features_only=True, out_indices=(2, 3, 4), pretrained=True)
dims = [512, 1024, 2048] if backbone_type == 'resnet50' else [256, 512, 512]
# atte
self.energy_1 = EnergyAttention(dims[0], dims[2])
self.energy_2 = EnergyAttention(dims[1], dims[2])
# proj
self.proj = nn.Linear(sum(dims), proj_dim)
def forward(self, img):
block_1_feat, block_2_feat, block_3_feat = self.backbone(img)
block_1_atte, block_1_feat = self.energy_1(block_1_feat, block_3_feat)
block_2_atte, block_2_feat = self.energy_2(block_2_feat, block_3_feat)
block_3_atte = torch.sigmoid(block_3_feat)
block_1_feat = torch.flatten(F.adaptive_max_pool2d(block_1_feat, (1, 1)), start_dim=1)
block_2_feat = torch.flatten(F.adaptive_max_pool2d(block_2_feat, (1, 1)), start_dim=1)
block_3_feat = torch.flatten(F.adaptive_max_pool2d(block_3_feat, (1, 1)), start_dim=1)
feat = torch.cat((block_1_feat, block_2_feat, block_3_feat), dim=-1)
proj = self.proj(feat)
return block_1_atte, block_2_atte, block_3_atte, F.normalize(proj, dim=-1)