-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretinanet.py
296 lines (206 loc) · 9.62 KB
/
retinanet.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import torch.nn as nn
import torch
import numpy as np
from utils import ConvBlock, BasicBlock, Bottleneck
from fpn import FPN
from anchors import RPN
from losses import FocalLoss
import math
import sys, os
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
}
def layers_dim(h_x, w_x, factor, n_layers):
h = []
w = []
for i in range(n_layers):
h.append(h_x)
w.append(w_x)
h_x /= float(factor)
w_x /= float(factor)
return h, w
class RegressionModel(nn.Module):
"""
Input:
- in_plane : features generated by FPN model at a given level.
"""
def __init__(self, in_plane, n_layers=4, num_anchors=9, feature_size=256):
super(RegressionModel, self).__init__()
in_features = [in_plane] + [feature_size] * (n_layers-1)
self.convgroup = self._make_convgroup(n_layers, in_features, feature_size)
self.output = nn.Conv2d(feature_size, num_anchors * 4, kernel_size=3, stride=1, padding=1)
def _make_convgroup(self, n_layers, in_features, out_features):
layers = []
for i in range(n_layers):
layers.append( nn.Conv2d(in_features[i], out_features, kernel_size=3, padding=1) )
layers.append( nn.ReLU() )
return nn.Sequential(*layers)
def forward(self, x):
out = self.convgroup(x)
out = self.output(out)
# out is B x C x W x H, with C = 4*num_anchors
out = out.permute(0, 2, 3, 1)
out = out.contiguous().view(out.shape[0], -1, 4)
return out
class ClassificationModel(nn.Module):
""" Uses sigmoid, but could be extended to CE. """
def __init__(self, in_plane, n_layers=4, num_anchors=9, num_classes=80, prior=0.01, feature_size=256):
super(ClassificationModel, self).__init__()
in_features = [in_plane] + [feature_size] * (n_layers-1)
self.num_anchors = num_anchors
self.num_classes = num_classes
self.convgroup = self._make_convgroup(n_layers, in_features, feature_size)
self.output = nn.Conv2d(feature_size, num_anchors * num_classes, kernel_size=3, stride=1, padding=1)
self.output_act = nn.Sigmoid()
def _make_convgroup(self, n_layers, in_features, out_features):
layers = []
for i in range(n_layers):
layers.append( nn.Conv2d(in_features[i], out_features, kernel_size=3, padding=1) )
layers.append( nn.ReLU() )
return nn.Sequential(*layers)
def forward(self, x):
out = self.convgroup(x)
out = self.output(out)
out = self.output_act(out)
# Output: [N,C,H,W], with C = num_classes * num_anchors
out1 = out.permute(0, 2, 3, 1)
N, H, W, C = out1.size()
out2 = out1.view(N, H, W, self.num_anchors, self.num_classes)
# [N, HxWxnum_anchors, num_classes]
out3 = out2.contiguous().view(x.size(0), -1, self.num_classes)
return out3
class RetinaNet(nn.Module):
"""
Ideally, it should extend to allow more multiple type of backbones.
"""
def __init__(self,):
super(RetinaNet, self).__init__()
def forward(self):
pass
class ResNet(nn.Module):
def __init__(self, layers, block, num_classes):
super(ResNet, self).__init__()
self.training = True
self.in_plane = 64
self.conv1 = ConvBlock(3, 64, kernel=7, stride=2, pad=3, bias=False)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
# Needs to be converted toa for loop
self.conv_2 = self._make_layers(block, layers[0], 64)
self.conv_3 = self._make_layers(block, layers[1], 128, stride=2)
self.conv_4 = self._make_layers(block, layers[2], 256, stride=2)
self.conv_5 = self._make_layers(block, layers[3], 512, stride=2)
# Feature Pyramid Network
self.fpn = FPN([128, 256, 512])
# Regression Model
self.regressionModel = RegressionModel(256)
self.classificationModel = ClassificationModel(256, num_classes=num_classes)
# Anchors
self.anchors = RPN()
# Focal Loss
self.focalLoss = FocalLoss()
# Utils Function
self.regressBoxes = BBoxTransform()
self.clipBoxes = ClipBoxes()
self._init_weights()
def _init_weights(self):
conv_count = 0
bn_count = 0
for module in self.modules():
if isinstance(module, nn.Conv2d):
# print(module)
n = module.kernel_size[0] * module.kernel_size[1] * module.out_channels
module.weight.data.normal_(0, math.sqrt(2. / n))
conv_count += 1
elif isinstance(module, nn.BatchNorm2d):
# print(module)
module.weight.data.fill_(1)
module.bias.data.zero_()
bn_count += 1
prior = 0.01
self.classificationModel.output.weight.data.fill_(0)
self.classificationModel.output.bias.data.fill_(-np.log( (1-prior) / prior) )
self.regressionModel.output.weight.data.fill_(0)
self.regressionModel.output.bias.data.fill_(0)
self.freeze_bn()
print("Initialized Conv Layers:",conv_count)
print("Initialized BatchNorm Layers:",bn_count)
def freeze_bn(self):
''' Freeze BatchNorm Layers '''
for module in self.modules():
if isinstance(module, nn.BatchNorm2d):
for param in module.parameters():
param.requires_grad = False
def _make_layers(self, block, n_layers, out_plane, stride=1):
downsample = None
# Assume downsampling is performed by adding an extra layer to perform downsampling
if self.in_plane != out_plane or block == Bottleneck:
downsample = ConvBlock(self.in_plane, out_plane * block.expansion, kernel=1, stride=stride, pad=0, bias=False, is_relu=False)
layers = [ block(self.in_plane, out_plane, stride, downsample) ]
self.in_plane = out_plane * block.expansion
for i in range(1, n_layers):
layers.append( block(self.in_plane, out_plane) )
return nn.Sequential(*layers)
def forward(self, inputs):
if self.training:
img_batch, annotations = inputs
x = self.conv1(img_batch)
x = self.maxpool(x)
# This could be for-loop
x1 = self.conv_2(x)
x2 = self.conv_3(x1)
x3 = self.conv_4(x2)
x4 = self.conv_5(x3)
# Reverse order for top-to-bottom pass
features = self.fpn([x4,x3,x2])
# Regression-Box (Different): list of varying sizes
regression = torch.cat([self.regressionModel(feature) for feature in features], dim=1)
# Classification
classification = torch.cat([self.classificationModel(feature) for feature in features], dim=1)
# Anchors
anchors = self.anchors(img_batch)
if self.training:
return self.focalLoss(classification, regression, anchors, annotations)
return None
# ==================== LOAD MODEL ======================
def resnet18(num_classes, pretrained=False, **kwargs):
""" Constructs ResNet Model """
model = ResNet([2, 2, 2, 2], BasicBlock, num_classes)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet18'], model_dir='models'), strict=False)
return model
def resnet34(num_classes, pretrained=False, **kwargs):
""" Constructs ResNet Model """
model = ResNet([3, 4, 6, 3], BasicBlock, num_classes)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet34'], model_dir='models'), strict=False)
return model
def resnet50(num_classes, pretrained=False, **kwargs):
""" Constructs ResNet Model """
model = ResNet([3, 4, 6, 3], Bottleneck, num_classes)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet50'], model_dir='models'), strict=False)
return model
def resnet101(num_classes, pretrained=False, **kwargs):
""" Constructs ResNet Model """
model = ResNet([3, 4, 23, 3], Bottleneck, num_classes)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet101'], model_dir='models'), strict=False)
return model
def resnet152(num_classes, pretrained=False, **kwargs):
""" Constructs ResNet Model """
model = ResNet([3, 8, 36, 3], Bottleneck, num_classes)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['resnet152'], model_dir='models'), strict=False)
return model
if __name__ == '__main__':
print(">>> Start...")
os.environ["CUDA_VISIBLE_DEVICES"]="2"
x = torch.randn((10, 3, 224, 224)).cuda()
x_dim = np.array([x.size(2), x.size(3)])
layers = [3, 4, 6, 3]
model = ResNet(layers, Bottleneck, 80).cuda()
model.forward((x, None))