6
6
import utils .network as net_utils
7
7
import cfgs .config as cfg
8
8
from layers .reorg .reorg_layer import ReorgLayer
9
- from utils .cython_bbox import bbox_ious , bbox_intersections , bbox_overlaps , anchor_intersections
9
+ from utils .cython_bbox import bbox_ious , anchor_intersections
10
10
from utils .cython_yolo import yolo_to_bbox
11
+ from functools import partial
11
12
12
13
from multiprocessing import Pool
14
+ import multiprocessing
13
15
14
16
15
17
def _make_layers (in_channels , net_cfg ):
@@ -25,17 +27,21 @@ def _make_layers(in_channels, net_cfg):
25
27
layers .append (nn .MaxPool2d (kernel_size = 2 , stride = 2 ))
26
28
else :
27
29
out_channels , ksize = item
28
- layers .append (net_utils .Conv2d_BatchNorm (in_channels , out_channels , ksize , same_padding = True ))
29
- # layers.append(net_utils.Conv2d(in_channels, out_channels, ksize, same_padding=True))
30
+ layers .append (net_utils .Conv2d_BatchNorm (in_channels ,
31
+ out_channels ,
32
+ ksize ,
33
+ same_padding = True ))
34
+ # layers.append(net_utils.Conv2d(in_channels, out_channels,
35
+ # ksize, same_padding=True))
30
36
in_channels = out_channels
31
37
32
38
return nn .Sequential (* layers ), in_channels
33
39
34
40
35
- def _process_batch (data ):
36
- W , H = cfg .out_size
37
- inp_size = cfg .inp_size
38
- out_size = cfg .out_size
41
+ def _process_batch (data , size_index ):
42
+ W , H = cfg .multi_scale_out_size [ size_index ]
43
+ inp_size = cfg .multi_scale_inp_size [ size_index ]
44
+ out_size = cfg .multi_scale_out_size [ size_index ]
39
45
40
46
bbox_pred_np , gt_boxes , gt_classes , dontcares , iou_pred_np = data
41
47
@@ -105,7 +111,7 @@ def _process_batch(data):
105
111
ious_reshaped = np .reshape (ious , [hw , num_anchors , len (cell_inds )])
106
112
for i , cell_ind in enumerate (cell_inds ):
107
113
if cell_ind >= hw or cell_ind < 0 :
108
- print (cell_ind )
114
+ print ('cell over {} hw {}' . format ( cell_ind , hw ) )
109
115
continue
110
116
a = anchor_inds [i ]
111
117
@@ -154,7 +160,8 @@ def __init__(self):
154
160
self .conv3 , c3 = _make_layers (c2 , net_cfgs [6 ])
155
161
156
162
stride = 2
157
- self .reorg = ReorgLayer (stride = 2 ) # stride*stride times the channels of conv1s
163
+ # stride*stride times the channels of conv1s
164
+ self .reorg = ReorgLayer (stride = 2 )
158
165
# cat [conv1s, conv3]
159
166
self .conv4 , c4 = _make_layers ((c1 * (stride * stride ) + c3 ), net_cfgs [7 ])
160
167
@@ -172,7 +179,7 @@ def __init__(self):
172
179
def loss (self ):
173
180
return self .bbox_loss + self .iou_loss + self .cls_loss
174
181
175
- def forward (self , im_data , gt_boxes = None , gt_classes = None , dontcare = None ):
182
+ def forward (self , im_data , gt_boxes = None , gt_classes = None , dontcare = None , size_index = 0 ):
176
183
conv1s = self .conv1s (im_data )
177
184
conv2 = self .conv2 (conv1s )
178
185
conv3 = self .conv3 (conv2 )
@@ -201,7 +208,7 @@ def forward(self, im_data, gt_boxes=None, gt_classes=None, dontcare=None):
201
208
bbox_pred_np = bbox_pred .data .cpu ().numpy ()
202
209
iou_pred_np = iou_pred .data .cpu ().numpy ()
203
210
_boxes , _ious , _classes , _box_mask , _iou_mask , _class_mask = self ._build_target (
204
- bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np )
211
+ bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np , size_index )
205
212
206
213
_boxes = net_utils .np_to_variable (_boxes )
207
214
_ious = net_utils .np_to_variable (_ious )
@@ -223,14 +230,16 @@ def forward(self, im_data, gt_boxes=None, gt_classes=None, dontcare=None):
223
230
224
231
return bbox_pred , iou_pred , prob_pred
225
232
226
- def _build_target (self , bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np ):
233
+ def _build_target (self , bbox_pred_np , gt_boxes , gt_classes , dontcare , iou_pred_np , size_index ):
227
234
"""
228
235
:param bbox_pred: shape: (bsize, h x w, num_anchors, 4) : (sig(tx), sig(ty), exp(tw), exp(th))
229
236
"""
230
237
231
238
bsize = bbox_pred_np .shape [0 ]
232
239
233
- targets = self .pool .map (_process_batch , ((bbox_pred_np [b ], gt_boxes [b ], gt_classes [b ], dontcare [b ], iou_pred_np [b ]) for b in range (bsize )))
240
+ targets = self .pool .map (partial (_process_batch , size_index = size_index ),
241
+ ((bbox_pred_np [b ], gt_boxes [b ], gt_classes [b ], dontcare [b ], iou_pred_np [b ])
242
+ for b in range (bsize )))
234
243
235
244
_boxes = np .stack (tuple ((row [0 ] for row in targets )))
236
245
_ious = np .stack (tuple ((row [1 ] for row in targets )))
@@ -250,7 +259,7 @@ def load_from_npz(self, fname, num_conv=None):
250
259
keys = list (own_dict .keys ())
251
260
252
261
for i , start in enumerate (range (0 , len (keys ), 5 )):
253
- if num_conv is not None and i >= num_conv :
262
+ if num_conv is not None and i >= num_conv :
254
263
break
255
264
end = min (start + 5 , len (keys ))
256
265
for key in keys [start :end ]:
@@ -263,8 +272,8 @@ def load_from_npz(self, fname, num_conv=None):
263
272
param = param .permute (3 , 2 , 0 , 1 )
264
273
own_dict [key ].copy_ (param )
265
274
275
+
266
276
if __name__ == '__main__' :
267
277
net = Darknet19 ()
268
278
# net.load_from_npz('models/yolo-voc.weights.npz')
269
279
net .load_from_npz ('models/darknet19.weights.npz' , num_conv = 18 )
270
-
0 commit comments