This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 542
/
Copy pathfast_rcnn_heads.py
242 lines (204 loc) · 8.39 KB
/
fast_rcnn_heads.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
from core.config import cfg
import nn as mynn
import utils.net as net_utils
class fast_rcnn_outputs(nn.Module):
def __init__(self, dim_in):
super(fast_rcnn_outputs, self).__init__()
self.cls_score = nn.Linear(dim_in, cfg.MODEL.NUM_CLASSES)
if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG: # bg and fg
self.bbox_pred = nn.Linear(dim_in, 4 * 2)
else:
self.bbox_pred = nn.Linear(dim_in, 4 * cfg.MODEL.NUM_CLASSES)
self._init_weights()
def _init_weights(self):
init.normal_(self.cls_score.weight, std=0.01)
init.constant_(self.cls_score.bias, 0)
init.normal_(self.bbox_pred.weight, std=0.001)
init.constant_(self.bbox_pred.bias, 0)
def detectron_weight_mapping(self):
detectron_weight_mapping = {
'cls_score.weight': 'cls_score_w',
'cls_score.bias': 'cls_score_b',
'bbox_pred.weight': 'bbox_pred_w',
'bbox_pred.bias': 'bbox_pred_b'
}
orphan_in_detectron = []
return detectron_weight_mapping, orphan_in_detectron
def forward(self, x):
if x.dim() == 4:
x = x.squeeze(3).squeeze(2)
cls_score = self.cls_score(x)
if not self.training:
cls_score = F.softmax(cls_score, dim=1)
bbox_pred = self.bbox_pred(x)
return cls_score, bbox_pred
def fast_rcnn_losses(cls_score, bbox_pred, label_int32, bbox_targets,
bbox_inside_weights, bbox_outside_weights):
device_id = cls_score.get_device()
rois_label = Variable(torch.from_numpy(label_int32.astype('int64'))).cuda(device_id)
loss_cls = F.cross_entropy(cls_score, rois_label)
bbox_targets = Variable(torch.from_numpy(bbox_targets)).cuda(device_id)
bbox_inside_weights = Variable(torch.from_numpy(bbox_inside_weights)).cuda(device_id)
bbox_outside_weights = Variable(torch.from_numpy(bbox_outside_weights)).cuda(device_id)
loss_bbox = net_utils.smooth_l1_loss(
bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights)
# class accuracy
cls_preds = cls_score.max(dim=1)[1].type_as(rois_label)
accuracy_cls = cls_preds.eq(rois_label).float().mean(dim=0)
return loss_cls, loss_bbox, accuracy_cls
# ---------------------------------------------------------------------------- #
# Box heads
# ---------------------------------------------------------------------------- #
class roi_2mlp_head(nn.Module):
"""Add a ReLU MLP with two hidden layers."""
def __init__(self, dim_in, roi_xform_func, spatial_scale):
super(roi_2mlp_head, self).__init__()
self.dim_in = dim_in
self.roi_xform = roi_xform_func
self.spatial_scale = spatial_scale
self.dim_out = hidden_dim = cfg.FAST_RCNN.MLP_HEAD_DIM
roi_size = cfg.FAST_RCNN.ROI_XFORM_RESOLUTION
self.fc1 = nn.Linear(dim_in * roi_size**2, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
self._init_weights()
def _init_weights(self):
mynn.init.XavierFill(self.fc1.weight)
init.constant_(self.fc1.bias, 0)
mynn.init.XavierFill(self.fc2.weight)
init.constant_(self.fc2.bias, 0)
def detectron_weight_mapping(self):
detectron_weight_mapping = {
'fc1.weight': 'fc6_w',
'fc1.bias': 'fc6_b',
'fc2.weight': 'fc7_w',
'fc2.bias': 'fc7_b'
}
return detectron_weight_mapping, []
def forward(self, x, rpn_ret):
x = self.roi_xform(
x, rpn_ret,
blob_rois='rois',
method=cfg.FAST_RCNN.ROI_XFORM_METHOD,
resolution=cfg.FAST_RCNN.ROI_XFORM_RESOLUTION,
spatial_scale=self.spatial_scale,
sampling_ratio=cfg.FAST_RCNN.ROI_XFORM_SAMPLING_RATIO
)
batch_size = x.size(0)
x = F.relu(self.fc1(x.view(batch_size, -1)), inplace=True)
x = F.relu(self.fc2(x), inplace=True)
return x
class roi_Xconv1fc_head(nn.Module):
"""Add a X conv + 1fc head, as a reference if not using GroupNorm"""
def __init__(self, dim_in, roi_xform_func, spatial_scale):
super(roi_Xconv1fc_head, self).__init__()
self.dim_in = dim_in
self.roi_xform = roi_xform_func
self.spatial_scale = spatial_scale
hidden_dim = cfg.FAST_RCNN.CONV_HEAD_DIM
module_list = []
for i in range(cfg.FAST_RCNN.NUM_STACKED_CONVS):
module_list.extend([
nn.Conv2d(dim_in, hidden_dim, 3, 1, 1),
nn.ReLU(inplace=True)
])
dim_in = hidden_dim
self.convs = nn.Sequential(*module_list)
self.dim_out = fc_dim = cfg.FAST_RCNN.MLP_HEAD_DIM
roi_size = cfg.FAST_RCNN.ROI_XFORM_RESOLUTION
self.fc = nn.Linear(dim_in * roi_size * roi_size, fc_dim)
self._init_weights()
def _init_weights(self):
def _init(m):
if isinstance(m, nn.Conv2d):
mynn.init.MSRAFill(m.weight)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
mynn.init.XavierFill(m.weight)
init.constant_(m.bias, 0)
self.apply(_init)
def detectron_weight_mapping(self):
mapping = {}
for i in range(cfg.FAST_RCNN.NUM_STACKED_CONVS):
mapping.update({
'convs.%d.weight' % (i*2): 'head_conv%d_w' % (i+1),
'convs.%d.bias' % (i*2): 'head_conv%d_b' % (i+1)
})
mapping.update({
'fc.weight': 'fc6_w',
'fc.bias': 'fc6_b'
})
return mapping, []
def forward(self, x, rpn_ret):
x = self.roi_xform(
x, rpn_ret,
blob_rois='rois',
method=cfg.FAST_RCNN.ROI_XFORM_METHOD,
resolution=cfg.FAST_RCNN.ROI_XFORM_RESOLUTION,
spatial_scale=self.spatial_scale,
sampling_ratio=cfg.FAST_RCNN.ROI_XFORM_SAMPLING_RATIO
)
batch_size = x.size(0)
x = self.convs(x)
x = F.relu(self.fc(x.view(batch_size, -1)), inplace=True)
return x
class roi_Xconv1fc_gn_head(nn.Module):
"""Add a X conv + 1fc head, with GroupNorm"""
def __init__(self, dim_in, roi_xform_func, spatial_scale):
super(roi_Xconv1fc_gn_head, self).__init__()
self.dim_in = dim_in
self.roi_xform = roi_xform_func
self.spatial_scale = spatial_scale
hidden_dim = cfg.FAST_RCNN.CONV_HEAD_DIM
module_list = []
for i in range(cfg.FAST_RCNN.NUM_STACKED_CONVS):
module_list.extend([
nn.Conv2d(dim_in, hidden_dim, 3, 1, 1, bias=False),
nn.GroupNorm(net_utils.get_group_gn(hidden_dim), hidden_dim,
eps=cfg.GROUP_NORM.EPSILON),
nn.ReLU(inplace=True)
])
dim_in = hidden_dim
self.convs = nn.Sequential(*module_list)
self.dim_out = fc_dim = cfg.FAST_RCNN.MLP_HEAD_DIM
roi_size = cfg.FAST_RCNN.ROI_XFORM_RESOLUTION
self.fc = nn.Linear(dim_in * roi_size * roi_size, fc_dim)
self._init_weights()
def _init_weights(self):
def _init(m):
if isinstance(m, nn.Conv2d):
mynn.init.MSRAFill(m.weight)
elif isinstance(m, nn.Linear):
mynn.init.XavierFill(m.weight)
init.constant_(m.bias, 0)
self.apply(_init)
def detectron_weight_mapping(self):
mapping = {}
for i in range(cfg.FAST_RCNN.NUM_STACKED_CONVS):
mapping.update({
'convs.%d.weight' % (i*3): 'head_conv%d_w' % (i+1),
'convs.%d.weight' % (i*3+1): 'head_conv%d_gn_s' % (i+1),
'convs.%d.bias' % (i*3+1): 'head_conv%d_gn_b' % (i+1)
})
mapping.update({
'fc.weight': 'fc6_w',
'fc.bias': 'fc6_b'
})
return mapping, []
def forward(self, x, rpn_ret):
x = self.roi_xform(
x, rpn_ret,
blob_rois='rois',
method=cfg.FAST_RCNN.ROI_XFORM_METHOD,
resolution=cfg.FAST_RCNN.ROI_XFORM_RESOLUTION,
spatial_scale=self.spatial_scale,
sampling_ratio=cfg.FAST_RCNN.ROI_XFORM_SAMPLING_RATIO
)
batch_size = x.size(0)
x = self.convs(x)
x = F.relu(self.fc(x.view(batch_size, -1)), inplace=True)
return x