forked from longcw/yolo2-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
darknet.py
369 lines (295 loc) · 14.7 KB
/
darknet.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import utils.network as net_utils
import cfgs.config as cfg
from layers.reorg.reorg_layer import ReorgLayer
from utils.cython_bbox import bbox_ious, bbox_intersections, bbox_overlaps, anchor_intersections
from utils.cython_yolo import yolo_to_bbox
from multiprocessing import Pool
def _make_layers(in_channels, net_cfg):
layers = []
if len(net_cfg) > 0 and isinstance(net_cfg[0], list):
for sub_cfg in net_cfg:
layer, in_channels = _make_layers(in_channels, sub_cfg)
layers.append(layer)
else:
for item in net_cfg:
if item == 'M':
layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
else:
out_channels, ksize = item
layers.append(net_utils.Conv2d_BatchNorm(in_channels, out_channels, ksize, same_padding=True))
# layers.append(net_utils.Conv2d(in_channels, out_channels, ksize, same_padding=True))
in_channels = out_channels
return nn.Sequential(*layers), in_channels
def _process_batch(data):
bbox_pred_np, gt_boxes, gt_classes, dontcares, inp_size = data
out_size = inp_size / 32
W, H = out_size
# mask out dontcare
# net output
hw, num_anchors, _ = bbox_pred_np.shape
# gt
_classes = np.zeros([hw, num_anchors, cfg.num_classes], dtype=np.float)
_class_mask = np.zeros([hw, num_anchors, 1], dtype=np.float)
_ious = np.zeros([hw, num_anchors, 1], dtype=np.float)
_iou_mask = np.zeros([hw, num_anchors, 1], dtype=np.float)
_boxes = np.zeros([hw, num_anchors, 4], dtype=np.float)
_boxes[:, :, 0:2] = 0.5
_boxes[:, :, 2:4] = 1.0
_box_mask = np.zeros([hw, num_anchors, 1], dtype=np.float) + 0.01
# scale pred_bbox
anchors = np.ascontiguousarray(cfg.anchors, dtype=np.float)
bbox_pred_np = np.expand_dims(bbox_pred_np, 0)
bbox_np = yolo_to_bbox(
np.ascontiguousarray(bbox_pred_np, dtype=np.float),
anchors,
H, W)
bbox_np = bbox_np[0]
bbox_np[:, :, 0::2] *= float(inp_size[0])
bbox_np[:, :, 1::2] *= float(inp_size[1])
# gt_boxes_b = np.asarray(gt_boxes[b], dtype=np.float)
gt_boxes_b = np.asarray(gt_boxes, dtype=np.float)
# for each cell
bbox_np_b = np.reshape(bbox_np, [-1, 4])
ious = bbox_ious(
np.ascontiguousarray(bbox_np_b, dtype=np.float),
np.ascontiguousarray(gt_boxes_b, dtype=np.float)
)
best_ious = np.max(ious, axis=1).reshape(_iou_mask.shape)
_iou_mask[best_ious <= cfg.iou_thresh] = cfg.noobject_scale
# locate the cell of each gt_boxes
cell_w = float(inp_size[0]) / W
cell_h = float(inp_size[1]) / H
cx = (gt_boxes_b[:, 0] + gt_boxes_b[:, 2]) * 0.5 / cell_w
cy = (gt_boxes_b[:, 1] + gt_boxes_b[:, 3]) * 0.5 / cell_h
cell_inds = np.floor(cy) * W + np.floor(cx)
cell_inds = cell_inds.astype(np.int)
target_boxes = np.empty(gt_boxes_b.shape, dtype=np.float)
target_boxes[:, 0] = cx - np.floor(cx) # cx
target_boxes[:, 1] = cy - np.floor(cy) # cy
target_boxes[:, 2] = (gt_boxes_b[:, 2] - gt_boxes_b[:, 0]) / inp_size[0] * out_size[0] # tw
target_boxes[:, 3] = (gt_boxes_b[:, 3] - gt_boxes_b[:, 1]) / inp_size[1] * out_size[1] # th
# for each gt boxes, match the best anchor
# gt_boxes_resize = [(xmin, ymin, xmax, ymax)] unit: cell px
gt_boxes_resize = np.copy(gt_boxes_b)
gt_boxes_resize[:, 0::2] *= (out_size[0] / float(inp_size[0]))
gt_boxes_resize[:, 1::2] *= (out_size[1] / float(inp_size[1]))
anchor_ious = anchor_intersections(
anchors,
np.ascontiguousarray(gt_boxes_resize, dtype=np.float)
)
anchor_inds = np.argmax(anchor_ious, axis=0)
ious_reshaped = np.reshape(ious, [hw, num_anchors, len(cell_inds)])
for i, cell_ind in enumerate(cell_inds):
if cell_ind >= hw or cell_ind < 0:
print('warning: cell_ind >= hw', cell_ind, hw)
continue
a = anchor_inds[i]
# do not evaluate for dontcare
if gt_classes[i] == -1:
# print(-1)
continue
_iou_mask[cell_ind, a, :] = cfg.object_scale
# _ious[cell_ind, a, :] = anchor_ious[a, i]
_ious[cell_ind, a, :] = ious_reshaped[cell_ind, a, i]
# print('anchor_iou {:.4f} vs real_iou {:.4f}'.format(anchor_ious[a, i], ious_reshaped[cell_ind, a, i]))
_box_mask[cell_ind, a, :] = cfg.coord_scale
target_boxes[i, 2:4] /= anchors[a]
_boxes[cell_ind, a, :] = target_boxes[i]
_class_mask[cell_ind, a, :] = cfg.class_scale
_classes[cell_ind, a, gt_classes[i]] = 1.
# _boxes[:, :, 2:4] = np.maximum(_boxes[:, :, 2:4], 0.001)
# _boxes[:, :, 2:4] = np.log(_boxes[:, :, 2:4])
# _boxes = (sig(tx), sig(ty), exp(tw), exp(th))
return _boxes, _ious, _classes, _box_mask, _iou_mask, _class_mask
class Darknet19(nn.Module):
def __init__(self):
super(Darknet19, self).__init__()
net_cfgs = [
# conv1s
[(32, 3)],
['M', (64, 3)],
['M', (128, 3), (64, 1), (128, 3)],
['M', (256, 3), (128, 1), (256, 3)],
['M', (512, 3), (256, 1), (512, 3), (256, 1), (512, 3)],
# conv2
['M', (1024, 3), (512, 1), (1024, 3), (512, 1), (1024, 3)],
# ------------
# conv3
[(1024, 3), (1024, 3)],
# conv4
[(1024, 3)]
]
# darknet
self.conv1s, c1 = _make_layers(3, net_cfgs[0:5])
self.conv2, c2 = _make_layers(c1, net_cfgs[5])
# ---
self.conv3, c3 = _make_layers(c2, net_cfgs[6])
stride = 2
self.reorg = ReorgLayer(stride=2) # stride*stride times the channels of conv1s
# cat [conv1s, conv3]
self.conv4, c4 = _make_layers((c1*(stride*stride) + c3), net_cfgs[7])
# linear
out_channels = cfg.num_anchors * (cfg.num_classes + 5)
self.conv5 = net_utils.Conv2d(c4, out_channels, 1, 1, relu=False)
# train
self.bbox_loss = None
self.iou_loss = None
self.cls_loss = None
self.pool = Pool(processes=10)
@property
def loss(self):
return self.bbox_loss + self.iou_loss + self.cls_loss
def forward(self, im_data, gt_boxes=None, gt_classes=None, dontcare=None, inp_size=None):
conv1s = self.conv1s(im_data)
conv2 = self.conv2(conv1s)
conv3 = self.conv3(conv2)
conv1s_reorg = self.reorg(conv1s)
cat_1_3 = torch.cat([conv1s_reorg, conv3], 1)
conv4 = self.conv4(cat_1_3)
conv5 = self.conv5(conv4) # batch_size, out_channels, h, w
# for detection
# bsize, c, h, w -> bsize, h, w, c -> bsize, h x w, num_anchors, 5+num_classes
bsize, _, h, w = conv5.size()
# assert bsize == 1, 'detection only support one image per batch'
conv5_reshaped = conv5.permute(0, 2, 3, 1).contiguous().view(bsize, -1, cfg.num_anchors, cfg.num_classes + 5)
# tx, ty, tw, th, to -> sig(tx), sig(ty), exp(tw), exp(th), sig(to)
xy_pred = F.sigmoid(conv5_reshaped[:, :, :, 0:2])
wh_pred = torch.exp(conv5_reshaped[:, :, :, 2:4])
bbox_pred = torch.cat([xy_pred, wh_pred], 3)
iou_pred = F.sigmoid(conv5_reshaped[:, :, :, 4:5])
score_pred = conv5_reshaped[:, :, :, 5:].contiguous()
prob_pred = F.softmax(score_pred.view(-1, score_pred.size()[-1])).view_as(score_pred)
debug = False
if debug:
bbox_pred_np = bbox_pred.data.cpu().numpy()
iou_pred_np = iou_pred.data.cpu().numpy()
prob_pred_np = prob_pred.data.cpu().numpy()
print(np.max(bbox_pred_np), np.max(iou_pred_np), np.max(prob_pred_np))
# for training
if self.training:
bbox_pred_np = bbox_pred.data.cpu().numpy()
_boxes, _ious, _classes, _box_mask, _iou_mask, _class_mask = self._build_target(
bbox_pred_np, gt_boxes, gt_classes, dontcare, inp_size)
_boxes = net_utils.np_to_variable(_boxes)
_ious = net_utils.np_to_variable(_ious)
_classes = net_utils.np_to_variable(_classes)
box_mask = net_utils.np_to_variable(_box_mask, dtype=torch.FloatTensor)
iou_mask = net_utils.np_to_variable(_iou_mask, dtype=torch.FloatTensor)
class_mask = net_utils.np_to_variable(_class_mask, dtype=torch.FloatTensor)
num_boxes = sum((len(boxes) for boxes in gt_boxes))
# _boxes[:, :, :, 2:4] = torch.log(_boxes[:, :, :, 2:4])
box_mask = box_mask.expand_as(_boxes)
self.bbox_loss = nn.MSELoss(size_average=False)(bbox_pred * box_mask, _boxes * box_mask) / num_boxes
self.iou_loss = nn.MSELoss(size_average=False)(iou_pred * iou_mask, _ious * iou_mask) / num_boxes
class_mask = class_mask.expand_as(prob_pred)
self.cls_loss = nn.MSELoss(size_average=False)(prob_pred * class_mask, _classes * class_mask) / num_boxes
return bbox_pred, iou_pred, prob_pred
def feed_feature(self, feature, layer, gt_boxes=None, gt_classes=None, dontcare=None):
# for detection
# bsize, c, h, w -> bsize, h, w, c -> bsize, h x w, num_anchors, 5+num_classes
if layer == 'conv4':
conv4 = feature
conv5 = self.conv5(conv4)
else:
conv5 = feature
bsize, _, h, w = conv5.size()
# assert bsize == 1, 'detection only support one image per batch'
conv5_reshaped = conv5.permute(0, 2, 3, 1).contiguous().view(bsize, -1, cfg.num_anchors, cfg.num_classes + 5)
# tx, ty, tw, th, to -> sig(tx), sig(ty), exp(tw), exp(th), sig(to)
xy_pred = F.sigmoid(conv5_reshaped[:, :, :, 0:2])
# wh_pred = torch.exp(conv5_reshaped[:, :, :, 2:4])
wh_pred = conv5_reshaped[:, :, :, 2:4]
bbox_pred = torch.cat([xy_pred, wh_pred], 3)
iou_pred = F.sigmoid(conv5_reshaped[:, :, :, 4:5])
score_pred = conv5_reshaped[:, :, :, 5:].contiguous()
prob_pred = F.softmax(score_pred.view(-1, score_pred.size()[-1])).view_as(score_pred)
# for training
if self.training:
bbox_pred_np = bbox_pred.data.cpu().numpy()
_boxes, _ious, _classes, _box_mask, _iou_mask, _class_mask = self._build_target(
bbox_pred_np, gt_boxes, gt_classes, dontcare)
_boxes = net_utils.np_to_variable(_boxes)
_ious = net_utils.np_to_variable(_ious)
_classes = net_utils.np_to_variable(_classes)
box_mask = net_utils.np_to_variable(_box_mask, dtype=torch.FloatTensor)
iou_mask = net_utils.np_to_variable(_iou_mask, dtype=torch.FloatTensor)
class_mask = net_utils.np_to_variable(_class_mask, dtype=torch.FloatTensor)
num_boxes = sum((len(boxes) for boxes in gt_boxes))
# _boxes[:, :, :, 2:4] = torch.log(_boxes[:, :, :, 2:4])
box_mask = box_mask.expand_as(_boxes)
# self.bbox_loss = torch.sum(torch.pow(_boxes - bbox_pred, 2) * box_mask) / num_boxes
# bbox_pred_log = torch.cat([xy_pred, wh_pred], 3)
# self.bbox_loss = nn.MSELoss(size_average=False)(bbox_pred_log * box_mask, _boxes * box_mask) / num_boxes
self.bbox_loss = nn.MSELoss(size_average=False)(bbox_pred * box_mask, _boxes * box_mask) / num_boxes
# self.iou_loss = torch.sum(torch.pow(_ious - iou_pred, 2) * iou_mask) / num_boxes
self.iou_loss = nn.MSELoss(size_average=False)(iou_pred * iou_mask, _ious * iou_mask) / num_boxes
class_mask = class_mask.expand_as(prob_pred)
# self.cls_loss = torch.sum(torch.pow(_classes - prob_pred, 2) * class_mask) / num_boxes
self.cls_loss = nn.MSELoss(size_average=False)(prob_pred * class_mask, _classes * class_mask) / num_boxes
wh_pred = torch.exp(conv5_reshaped[:, :, :, 2:4])
bbox_pred = torch.cat([xy_pred, wh_pred], 3)
return bbox_pred, iou_pred, prob_pred
def get_feature_map(self, im_data, layer='conv5'):
conv1s = self.conv1s(im_data)
if layer == 'conv1s':
return conv1s
conv2 = self.conv2(conv1s)
if layer == 'conv2':
return conv2
conv3 = self.conv3(conv2)
if layer == 'conv3':
return conv3
conv1s_reorg = self.reorg(conv1s)
if layer == 'conv1s_reorg':
return conv1s_reorg
cat_1_3 = torch.cat([conv1s_reorg, conv3], 1)
if layer == 'cat_1_3':
return cat_1_3
conv4 = self.conv4(cat_1_3)
if layer == 'conv4':
return conv4
conv5 = self.conv5(conv4) # batch_size, out_channels, h, w
if layer == 'conv5':
return conv5
return None
def _build_target(self, bbox_pred_np, gt_boxes, gt_classes, dontcare, inp_size):
"""
:param bbox_pred: shape: (bsize, h x w, num_anchors, 4) : (sig(tx), sig(ty), exp(tw), exp(th))
"""
bsize = bbox_pred_np.shape[0]
targets = self.pool.map(_process_batch, ((bbox_pred_np[b], gt_boxes[b], gt_classes[b], dontcare[b], inp_size) for b in range(bsize)))
_boxes = np.stack(tuple((row[0] for row in targets)))
_ious = np.stack(tuple((row[1] for row in targets)))
_classes = np.stack(tuple((row[2] for row in targets)))
_box_mask = np.stack(tuple((row[3] for row in targets)))
_iou_mask = np.stack(tuple((row[4] for row in targets)))
_class_mask = np.stack(tuple((row[5] for row in targets)))
return _boxes, _ious, _classes, _box_mask, _iou_mask, _class_mask
def load_from_npz(self, fname, num_conv=None):
dest_src = {'conv.weight': 'kernel', 'conv.bias': 'biases',
'bn.weight': 'gamma', 'bn.bias': 'biases',
'bn.running_mean': 'moving_mean', 'bn.running_var': 'moving_variance'}
params = np.load(fname)
own_dict = self.state_dict()
keys = list(own_dict.keys())
for i, start in enumerate(range(0, len(keys), 5)):
if num_conv is not None and i >= num_conv:
break
end = min(start+5, len(keys))
for key in keys[start:end]:
list_key = key.split('.')
ptype = dest_src['{}.{}'.format(list_key[-2], list_key[-1])]
src_key = '{}-convolutional/{}:0'.format(i, ptype)
print('src_key', src_key, own_dict[key].size(), params[src_key].shape)
param = torch.from_numpy(params[src_key])
if ptype == 'kernel':
param = param.permute(3, 2, 0, 1)
own_dict[key].copy_(param)
if __name__ == '__main__':
net = Darknet19()
net.load_from_npz('models/yolo-voc.weights.npz')
# net.load_from_npz('models/darknet19.weights.npz', num_conv=18)