This repository has been archived by the owner on May 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathretinanet.py
147 lines (102 loc) · 4.7 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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from resnet_features import resnet50_features
from utilities.layers import conv1x1, conv3x3
class FeaturePyramid(nn.Module):
def __init__(self, resnet):
super(FeaturePyramid, self).__init__()
self.resnet = resnet
# applied in a pyramid
self.pyramid_transformation_3 = conv1x1(512, 256)
self.pyramid_transformation_4 = conv1x1(1024, 256)
self.pyramid_transformation_5 = conv1x1(2048, 256)
# both based around resnet_feature_5
self.pyramid_transformation_6 = conv3x3(2048, 256, padding=1, stride=2)
self.pyramid_transformation_7 = conv3x3(256, 256, padding=1, stride=2)
# applied after upsampling
self.upsample_transform_1 = conv3x3(256, 256, padding=1)
self.upsample_transform_2 = conv3x3(256, 256, padding=1)
def _upsample(self, original_feature, scaled_feature, scale_factor=2):
# is this correct? You do lose information on the upscale...
height, width = scaled_feature.size()[2:]
return F.upsample(original_feature, scale_factor=scale_factor)[:, :, :height, :width]
def forward(self, x):
# don't need resnet_feature_2 as it is too large
_, resnet_feature_3, resnet_feature_4, resnet_feature_5 = self.resnet(x)
pyramid_feature_6 = self.pyramid_transformation_6(resnet_feature_5)
pyramid_feature_7 = self.pyramid_transformation_7(F.relu(pyramid_feature_6))
pyramid_feature_5 = self.pyramid_transformation_5(resnet_feature_5)
pyramid_feature_4 = self.pyramid_transformation_4(resnet_feature_4)
upsampled_feature_5 = self._upsample(pyramid_feature_5, pyramid_feature_4)
pyramid_feature_4 = self.upsample_transform_1(
torch.add(upsampled_feature_5, pyramid_feature_4)
)
pyramid_feature_3 = self.pyramid_transformation_3(resnet_feature_3)
upsampled_feature_4 = self._upsample(pyramid_feature_4, pyramid_feature_3)
pyramid_feature_3 = self.upsample_transform_2(
torch.add(upsampled_feature_4, pyramid_feature_3)
)
return (pyramid_feature_3,
pyramid_feature_4,
pyramid_feature_5,
pyramid_feature_6,
pyramid_feature_7)
class SubNet(nn.Module):
def __init__(self, mode, anchors=9, classes=80, depth=4,
base_activation=F.relu,
output_activation=F.sigmoid):
super(SubNet, self).__init__()
self.anchors = anchors
self.classes = classes
self.depth = depth
self.base_activation = base_activation
self.output_activation = output_activation
self.subnet_base = nn.ModuleList([conv3x3(256, 256, padding=1)
for _ in range(depth)])
if mode == 'boxes':
self.subnet_output = conv3x3(256, 4 * self.anchors, padding=1)
elif mode == 'classes':
# add an extra dim for confidence
self.subnet_output = conv3x3(256, (1 + self.classes) * self.anchors, padding=1)
self._output_layer_init(self.subnet_output.bias.data)
def _output_layer_init(self, tensor, pi=0.01):
fill_constant = - math.log((1 - pi) / pi)
if isinstance(tensor, Variable):
self._output_layer_init(tensor.data)
return tensor.fill_(fill_constant)
def forward(self, x):
for layer in self.subnet_base:
x = self.base_activation(layer(x))
x = self.subnet_output(x)
x = x.permute(0, 2, 3, 1).contiguous().view(x.size(0),
x.size(2) * x.size(3) * self.anchors, -1)
return x
class RetinaNet(nn.Module):
def __init__(self, classes):
super(RetinaNet, self).__init__()
self.classes = classes
_resnet = resnet50_features(pretrained=True)
self.feature_pyramid = FeaturePyramid(_resnet)
self.subnet_boxes = SubNet(mode='boxes')
self.subnet_classes = SubNet(mode='classes')
def forward(self, x):
boxes = []
classes = []
features = self.feature_pyramid(x)
# how faster to do one loop
boxes = [self.subnet_boxes(feature) for feature in features]
classes = [self.subnet_classes(feature) for feature in features]
return torch.cat(boxes, 1), torch.cat(classes, 1)
if __name__ == '__main__':
import time
net = RetinaNet(classes=80)
x = Variable(torch.rand(1, 3, 500, 500), volatile=True)
now = time.time()
predictions = net(x)
later = time.time()
print(later - now)
for prediction in predictions:
print(prediction.size())