-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
190 lines (136 loc) · 6.03 KB
/
models.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
import config
import utils
import torch.nn as nn
import torchvision.models as models
import torch.nn.functional as F
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
self.model = models.vgg16(weights='DEFAULT')
self.features = self.model.features
self.features1 = self.features[:6]
self.features2 = self.features[6:10]
self.features3 = self.features[10:17]
self.features4 = self.features[17:30]
def forward(self, images):
with torch.no_grad():
f1 = self.features1(images)
f2 = self.features2(f1)
f3 = self.features3(f2)
f4 = self.features4(f3)
return f2, f3, f4
class Classifier(nn.Module):
def __init__(self, num_classes=9):
super(Classifier, self).__init__()
self.num_classes = num_classes
self.conv1 = nn.Sequential(
nn.Conv2d(512, 256, kernel_size=(3, 3), padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=(3, 3), padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.conv3 = nn.Sequential(
nn.Conv2d(256, 128, kernel_size=(1, 1), padding=0),
nn.ReLU(),
)
self.fc = nn.Linear(128, self.num_classes)
def forward(self, f2, f3, f4):
features = self.conv1(f4)
features = self.conv2(features)
features = F.interpolate(features, scale_factor=2, mode='bilinear') + f3
features = self.conv3(features)
features = F.interpolate(features, scale_factor=2, mode='bilinear') + f2
# GAP section
fc = torch.flatten(F.adaptive_avg_pool2d(features, 1), start_dim=1)
scores = F.softmax(self.fc(fc), dim=1)
# Compute CAMs and KCMs
with torch.no_grad():
# Compute CAMs
batch, composition, height, width = features.shape
features = features.permute(0, 1, 2, 3)
features = features.view(batch, composition, height * width)
w = self.fc.weight.data.unsqueeze(0).repeat(batch, 1, 1)
cams = torch.matmul(w, features) # (batch, compisitions, height * width)
# Compute KCMs
cams = self._normalize_cams(cams)
cams = cams.view(batch, self.num_classes, height, width) # (batch, 9, height, width)
kcms = torch.sum(scores[:, :, None, None] * cams, dim=1, keepdim=True)
kcms = F.interpolate(kcms, scale_factor=4, mode='bilinear', align_corners=True)
return fc, cams, kcms
def _normalize_cams(self, cam):
cam = cam - cam.min(dim=-1)[0].unsqueeze(-1)
cam = cam / cam.max(dim=-1)[0].unsqueeze(-1)
return cam
class Cropper(nn.Module):
def __init__(self, anchor_stride):
super(Cropper, self).__init__()
self.out_features = int((16 / anchor_stride)**2 * 4)
self.num_anchors = (16 // anchor_stride)**2
self.upscale_factor = self.num_anchors // 2
self.conv1 = nn.Sequential(
nn.Conv2d(512, 256, kernel_size=(3, 3), padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.conv2 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=(3, 3), padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.conv3 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=(3, 3), padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
)
self.output = nn.Conv2d(
256,
self.out_features,
kernel_size=(3, 3),
padding=1
)
anchors = utils.generate_anchors(anchor_stride)
feat_shape = (config.IMAGE_SIZE // 16, config.IMAGE_SIZE // 16)
all_anchors = utils.shift(feat_shape, 16, anchors)
all_anchors = all_anchors.float().unsqueeze(0)
anchors_x = F.pixel_shuffle(all_anchors[..., 0], upscale_factor=self.upscale_factor)
anchors_y = F.pixel_shuffle(all_anchors[..., 1], upscale_factor=self.upscale_factor)
all_anchors = torch.stack([anchors_x, anchors_y], dim=-1).squeeze(1)
grid_x = (all_anchors[..., 0] - config.IMAGE_SIZE / 2) / (config.IMAGE_SIZE / 2)
grid_y = (all_anchors[..., 1] - config.IMAGE_SIZE / 2) / (config.IMAGE_SIZE / 2)
grid = torch.stack([grid_x, grid_y], dim=-1)
self.register_buffer('all_anchors', all_anchors)
self.register_buffer('grid', grid)
def forward(self, features, kcms):
features = self.conv1(features)
features = self.conv2(features)
features = self.conv3(features)
anchors = self.output(features)
batch, _, height, width = anchors.shape
anchors = anchors.view(batch, self.num_anchors, height, width, 4)
coords = [F.pixel_shuffle(anchors[..., i], upscale_factor=self.upscale_factor) for i in range(4)]
offsets = torch.stack(coords, dim=-1).squeeze(1)
regression = torch.zeros_like(offsets)
regression[..., 0::2] = offsets[..., 0::2] + self.all_anchors[..., 0:1]
regression[..., 1::2] = offsets[..., 1::2] + self.all_anchors[..., 1:2]
trans_grid = self.grid.repeat(offsets.shape[0], 1, 1, 1)
sample_kcm = F.grid_sample(kcms, trans_grid, mode='bilinear', align_corners=True)
reg_weight = F.softmax(sample_kcm.flatten(1), dim=1).unsqueeze(-1)
batch, height, width, compesition = regression.shape
regression = regression.view(batch, height * width, compesition)
return torch.sum(reg_weight * regression, dim=1)
class CACNet(nn.Module):
def __init__(self):
super(CACNet, self).__init__()
self.vgg16 = VGG16()
self.classifier = Classifier()
self.cropper = Cropper(anchor_stride=config.ANCHOR_STRIDE)
def forward(self, images):
f2, f3, f4 = self.vgg16(images)
scores, _, kcms = self.classifier(f2, f3, f4)
boxes = self.cropper(f4, kcms)
return scores, kcms, boxes