11
11
from functools import partial
12
12
13
13
from multiprocessing import Pool
14
- import multiprocessing
15
14
16
15
17
16
def _make_layers (in_channels , net_cfg ):
@@ -67,7 +66,8 @@ def _process_batch(data, size_index):
67
66
np .ascontiguousarray (bbox_pred_np , dtype = np .float ),
68
67
anchors ,
69
68
H , W )
70
- bbox_np = bbox_np [0 ] # bbox_np = (hw, num_anchors, (x1, y1, x2, y2)) range: 0 ~ 1
69
+ # bbox_np = (hw, num_anchors, (x1, y1, x2, y2)) range: 0 ~ 1
70
+ bbox_np = bbox_np [0 ]
71
71
bbox_np [:, :, 0 ::2 ] *= float (inp_size [0 ]) # rescale x
72
72
bbox_np [:, :, 1 ::2 ] *= float (inp_size [1 ]) # rescale y
73
73
@@ -95,8 +95,10 @@ def _process_batch(data, size_index):
95
95
target_boxes = np .empty (gt_boxes_b .shape , dtype = np .float )
96
96
target_boxes [:, 0 ] = cx - np .floor (cx ) # cx
97
97
target_boxes [:, 1 ] = cy - np .floor (cy ) # cy
98
- target_boxes [:, 2 ] = (gt_boxes_b [:, 2 ] - gt_boxes_b [:, 0 ]) / inp_size [0 ] * out_size [0 ] # tw
99
- target_boxes [:, 3 ] = (gt_boxes_b [:, 3 ] - gt_boxes_b [:, 1 ]) / inp_size [1 ] * out_size [1 ] # th
98
+ target_boxes [:, 2 ] = \
99
+ (gt_boxes_b [:, 2 ] - gt_boxes_b [:, 0 ]) / inp_size [0 ] * out_size [0 ] # tw
100
+ target_boxes [:, 3 ] = \
101
+ (gt_boxes_b [:, 3 ] - gt_boxes_b [:, 1 ]) / inp_size [1 ] * out_size [1 ] # th
100
102
101
103
# for each gt boxes, match the best anchor
102
104
gt_boxes_resize = np .copy (gt_boxes_b )
@@ -116,8 +118,9 @@ def _process_batch(data, size_index):
116
118
continue
117
119
a = anchor_inds [i ]
118
120
119
- iou_pred_cell_anchor = iou_pred_np [cell_ind , a , :] # 0 ~ 1, should be close to 1
120
- _iou_mask [cell_ind , a , :] = cfg .object_scale * (1 - iou_pred_cell_anchor )
121
+ # 0 ~ 1, should be close to 1
122
+ iou_pred_cell_anchor = iou_pred_np [cell_ind , a , :]
123
+ _iou_mask [cell_ind , a , :] = cfg .object_scale * (1 - iou_pred_cell_anchor ) # noqa
121
124
# _ious[cell_ind, a, :] = anchor_ious[a, i]
122
125
_ious [cell_ind , a , :] = ious_reshaped [cell_ind , a , i ]
123
126
@@ -169,7 +172,7 @@ def __init__(self):
169
172
# linear
170
173
out_channels = cfg .num_anchors * (cfg .num_classes + 5 )
171
174
self .conv5 = net_utils .Conv2d (c4 , out_channels , 1 , 1 , relu = False )
172
- self .global_average_pool = nn .AvgPool2d ((1 ,1 ))
175
+ self .global_average_pool = nn .AvgPool2d ((1 , 1 ))
173
176
174
177
# train
175
178
self .bbox_loss = None
@@ -181,7 +184,8 @@ def __init__(self):
181
184
def loss (self ):
182
185
return self .bbox_loss + self .iou_loss + self .cls_loss
183
186
184
- def forward (self , im_data , gt_boxes = None , gt_classes = None , dontcare = None , size_index = 0 ):
187
+ def forward (self , im_data , gt_boxes = None , gt_classes = None , dontcare = None ,
188
+ size_index = 0 ):
185
189
conv1s = self .conv1s (im_data )
186
190
conv2 = self .conv2 (conv1s )
187
191
conv3 = self .conv3 (conv2 )
@@ -192,10 +196,13 @@ def forward(self, im_data, gt_boxes=None, gt_classes=None, dontcare=None, size_i
192
196
global_average_pool = self .global_average_pool (conv5 )
193
197
194
198
# for detection
195
- # bsize, c, h, w -> bsize, h, w, c -> bsize, h x w, num_anchors, 5+num_classes
199
+ # bsize, c, h, w -> bsize, h, w, c ->
200
+ # bsize, h x w, num_anchors, 5+num_classes
196
201
bsize , _ , h , w = global_average_pool .size ()
197
202
# assert bsize == 1, 'detection only support one image per batch'
198
- global_average_pool_reshaped = global_average_pool .permute (0 , 2 , 3 , 1 ).contiguous ().view (bsize , - 1 , cfg .num_anchors , cfg .num_classes + 5 )
203
+ global_average_pool_reshaped = \
204
+ global_average_pool .permute (0 , 2 , 3 , 1 ).contiguous ().view (bsize ,
205
+ - 1 , cfg .num_anchors , cfg .num_classes + 5 ) # noqa
199
206
200
207
# tx, ty, tw, th, to -> sig(tx), sig(ty), exp(tw), exp(th), sig(to)
201
208
xy_pred = F .sigmoid (global_average_pool_reshaped [:, :, :, 0 :2 ])
@@ -204,44 +211,55 @@ def forward(self, im_data, gt_boxes=None, gt_classes=None, dontcare=None, size_i
204
211
iou_pred = F .sigmoid (global_average_pool_reshaped [:, :, :, 4 :5 ])
205
212
206
213
score_pred = global_average_pool_reshaped [:, :, :, 5 :].contiguous ()
207
- prob_pred = F .softmax (score_pred .view (- 1 , score_pred .size ()[- 1 ])).view_as (score_pred )
214
+ prob_pred = F .softmax (score_pred .view (- 1 , score_pred .size ()[- 1 ])).view_as (score_pred ) # noqa
208
215
209
216
# for training
210
217
if self .training :
211
218
bbox_pred_np = bbox_pred .data .cpu ().numpy ()
212
219
iou_pred_np = iou_pred .data .cpu ().numpy ()
213
- _boxes , _ious , _classes , _box_mask , _iou_mask , _class_mask = self ._build_target (
214
- bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np , size_index )
220
+ _boxes , _ious , _classes , _box_mask , _iou_mask , _class_mask = \
221
+ self ._build_target (bbox_pred_np ,
222
+ gt_boxes ,
223
+ gt_classes ,
224
+ dontcare ,
225
+ iou_pred_np ,
226
+ size_index )
215
227
216
228
_boxes = net_utils .np_to_variable (_boxes )
217
229
_ious = net_utils .np_to_variable (_ious )
218
230
_classes = net_utils .np_to_variable (_classes )
219
- box_mask = net_utils .np_to_variable (_box_mask , dtype = torch .FloatTensor )
220
- iou_mask = net_utils .np_to_variable (_iou_mask , dtype = torch .FloatTensor )
221
- class_mask = net_utils .np_to_variable (_class_mask , dtype = torch .FloatTensor )
231
+ box_mask = net_utils .np_to_variable (_box_mask ,
232
+ dtype = torch .FloatTensor )
233
+ iou_mask = net_utils .np_to_variable (_iou_mask ,
234
+ dtype = torch .FloatTensor )
235
+ class_mask = net_utils .np_to_variable (_class_mask ,
236
+ dtype = torch .FloatTensor )
222
237
223
238
num_boxes = sum ((len (boxes ) for boxes in gt_boxes ))
224
239
225
240
# _boxes[:, :, :, 2:4] = torch.log(_boxes[:, :, :, 2:4])
226
241
box_mask = box_mask .expand_as (_boxes )
227
242
228
- self .bbox_loss = nn .MSELoss (size_average = False )(bbox_pred * box_mask , _boxes * box_mask ) / num_boxes
229
- self .iou_loss = nn .MSELoss (size_average = False )(iou_pred * iou_mask , _ious * iou_mask ) / num_boxes
243
+ self .bbox_loss = nn .MSELoss (size_average = False )(bbox_pred * box_mask , _boxes * box_mask ) / num_boxes # noqa
244
+ self .iou_loss = nn .MSELoss (size_average = False )(iou_pred * iou_mask , _ious * iou_mask ) / num_boxes # noqa
230
245
231
246
class_mask = class_mask .expand_as (prob_pred )
232
- self .cls_loss = nn .MSELoss (size_average = False )(prob_pred * class_mask , _classes * class_mask ) / num_boxes
247
+ self .cls_loss = nn .MSELoss (size_average = False )(prob_pred * class_mask , _classes * class_mask ) / num_boxes # noqa
233
248
234
249
return bbox_pred , iou_pred , prob_pred
235
250
236
- def _build_target (self , bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np , size_index ):
251
+ def _build_target (self , bbox_pred_np , gt_boxes , gt_classes , dontcare ,
252
+ iou_pred_np , size_index ):
237
253
"""
238
- :param bbox_pred: shape: (bsize, h x w, num_anchors, 4) : (sig(tx), sig(ty), exp(tw), exp(th))
254
+ :param bbox_pred: shape: (bsize, h x w, num_anchors, 4) :
255
+ (sig(tx), sig(ty), exp(tw), exp(th))
239
256
"""
240
257
241
258
bsize = bbox_pred_np .shape [0 ]
242
259
243
260
targets = self .pool .map (partial (_process_batch , size_index = size_index ),
244
- ((bbox_pred_np [b ], gt_boxes [b ], gt_classes [b ], dontcare [b ], iou_pred_np [b ])
261
+ ((bbox_pred_np [b ], gt_boxes [b ],
262
+ gt_classes [b ], dontcare [b ], iou_pred_np [b ])
245
263
for b in range (bsize )))
246
264
247
265
_boxes = np .stack (tuple ((row [0 ] for row in targets )))
@@ -256,7 +274,8 @@ def _build_target(self, bbox_pred_np, gt_boxes, gt_classes, dontcare, iou_pred_n
256
274
def load_from_npz (self , fname , num_conv = None ):
257
275
dest_src = {'conv.weight' : 'kernel' , 'conv.bias' : 'biases' ,
258
276
'bn.weight' : 'gamma' , 'bn.bias' : 'biases' ,
259
- 'bn.running_mean' : 'moving_mean' , 'bn.running_var' : 'moving_variance' }
277
+ 'bn.running_mean' : 'moving_mean' ,
278
+ 'bn.running_var' : 'moving_variance' }
260
279
params = np .load (fname )
261
280
own_dict = self .state_dict ()
262
281
keys = list (own_dict .keys ())
0 commit comments