-
Notifications
You must be signed in to change notification settings - Fork 136
/
models.py
843 lines (764 loc) · 47 KB
/
models.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
from utils.google_utils import *
from utils.parse_config import *
from utils.quantized.quantized_google import *
from utils.quantized.quantized_ptq_cos import *
from utils.quantized.quantized_TPSQ import *
from utils.layers import *
import copy
# YOLO
def create_modules(module_defs, img_size, cfg, quantized, quantizer_output, layer_idx, reorder, TM, TN, a_bit=8,
w_bit=8, steps=0, is_gray_scale=False, maxabsscaler=False, shortcut_way=-1):
# Constructs module list of layer blocks from module configuration in module_defs
img_size = [img_size] * 2 if isinstance(img_size, int) else img_size # expand if necessary
_ = module_defs.pop(0) # cfg training hyperparams (unused)
if is_gray_scale:
output_filters = [1] # input channels
else:
output_filters = [3]
module_list = nn.ModuleList()
routs = [] # list of layers which rout to deeper layers
yolo_index = -1
for i, mdef in enumerate(module_defs):
modules = nn.Sequential()
if mdef['type'] == 'convolutional':
bn = int(mdef['batch_normalize'])
filters = int(mdef['filters'])
kernel_size = int(mdef['size'])
pad = (kernel_size - 1) // 2 if int(mdef['pad']) else 0
if quantized == 1:
modules.add_module('Conv2d', BNFold_QuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=mdef[
'groups'] if 'groups' in mdef else 1,
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
steps=steps,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" + mdef[
'type'][
:4],
layer_idx=layer_idx,
maxabsscaler=maxabsscaler))
elif quantized == 2:
modules.add_module('Conv2d', TPSQ_BNFold_QuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=mdef[
'groups'] if 'groups' in mdef else 1,
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
steps=steps,
quantizer_output=quantizer_output,
maxabsscaler=maxabsscaler))
elif quantized == 3:
modules.add_module('Conv2d', BNFold_COSPTQuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=mdef[
'groups'] if 'groups' in mdef else 1,
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" + mdef[
'type'][
:4],
layer_idx=layer_idx,
maxabsscaler=maxabsscaler))
else:
modules.add_module('Conv2d', nn.Conv2d(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=mdef['groups'] if 'groups' in mdef else 1,
bias=not bn))
if bn:
modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.1))
if mdef['activation'] == 'leaky':
modules.add_module('activation', nn.LeakyReLU(0.1 if not maxabsscaler else 0.25, inplace=True))
# modules.add_module('activation', nn.PReLU(num_parameters=1, init=0.10))
# modules.add_module('activation', Swish())
if mdef['activation'] == 'relu6':
modules.add_module('activation', ReLU6())
if mdef['activation'] == 'h_swish':
modules.add_module('activation', HardSwish())
if mdef['activation'] == 'relu':
modules.add_module('activation', nn.ReLU())
if mdef['activation'] == 'mish':
modules.add_module('activation', Mish())
elif mdef['type'] == 'depthwise':
bn = int(mdef['batch_normalize'])
filters = int(mdef['filters'])
kernel_size = int(mdef['size'])
pad = (kernel_size - 1) // 2 if int(mdef['pad']) else 0
if quantized == 1:
modules.add_module('DepthWise2d',
BNFold_QuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=output_filters[-1],
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
steps=steps,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" + mdef['type'][:4],
layer_idx=layer_idx,
maxabsscaler=maxabsscaler))
if quantized == 2:
modules.add_module('DepthWise2d',
TPSQ_BNFold_QuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=output_filters[-1],
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
steps=steps,
quantizer_output=quantizer_output,
maxabsscaler=maxabsscaler))
elif quantized == 3:
modules.add_module('DepthWise2d', BNFold_COSPTQuantizedConv2d_For_FPGA(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=output_filters[-1],
bias=not bn,
a_bits=a_bit,
w_bits=w_bit,
bn=bn,
activate=mdef['activation'],
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx,
maxabsscaler=maxabsscaler))
else:
modules.add_module('DepthWise2d', nn.Conv2d(in_channels=output_filters[-1],
out_channels=filters,
kernel_size=kernel_size,
stride=int(mdef['stride']),
padding=pad,
groups=output_filters[-1],
bias=not bn), )
if bn:
modules.add_module('BatchNorm2d', nn.BatchNorm2d(filters, momentum=0.1))
if mdef['activation'] == 'leaky':
modules.add_module('activation', nn.LeakyReLU(0.1 if not maxabsscaler else 0.25, inplace=True))
# modules.add_module('activation', nn.PReLU(num_parameters=1, init=0.10))
# modules.add_module('activation', Swish())
if mdef['activation'] == 'relu6':
modules.add_module('activation', ReLU6())
if mdef['activation'] == 'h_swish':
modules.add_module('activation', HardSwish())
if mdef['activation'] == 'relu':
modules.add_module('activation', nn.ReLU())
if mdef['activation'] == 'mish':
modules.add_module('activation', Mish())
elif mdef['type'] == 'BatchNorm2d':
filters = output_filters[-1]
modules = nn.BatchNorm2d(filters, momentum=0.03, eps=1E-4)
if i == 0 and filters == 3: # normalize RGB image
# imagenet mean and var https://pytorch.org/docs/stable/torchvision/models.html#classification
modules.running_mean = torch.tensor([0.485, 0.456, 0.406])
modules.running_var = torch.tensor([0.0524, 0.0502, 0.0506])
elif mdef['type'] == 'maxpool':
k = mdef['size'] # kernel size
stride = mdef['stride']
maxpool = nn.MaxPool2d(kernel_size=k, stride=stride, padding=(k - 1) // 2)
if k == 2 and stride == 1: # yolov3-tiny
modules.add_module('ZeroPad2d', nn.ZeroPad2d((0, 1, 0, 1)))
modules.add_module('MaxPool2d', maxpool)
else:
modules = maxpool
elif mdef['type'] == 'se':
if 'filters' in mdef:
filters = int(mdef['filters'])
modules.add_module('se', SE(channel=filters))
if 'reduction' in mdef:
modules.add_module('se', SE(output_filters[-1], reduction=int(mdef['reduction'])))
elif mdef['type'] == 'upsample':
modules = nn.Upsample(scale_factor=mdef['stride'])
elif mdef['type'] == 'route': # nn.Sequential() placeholder for 'route' layer
layers = mdef['layers']
filters = sum([output_filters[l + 1 if l > 0 else l] for l in layers])
if 'groups' in mdef:
filters = filters // 2
routs.extend([i + l if l < 0 else l for l in layers])
if quantized == -1:
if 'groups' in mdef:
modules = FeatureConcat(layers=layers, groups=True)
else:
modules = FeatureConcat(layers=layers, groups=False)
elif quantized == 3:
if 'groups' in mdef:
modules = COSPTQuantizedFeatureConcat(layers=layers, groups=True, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
else:
modules = COSPTQuantizedFeatureConcat(layers=layers, groups=False, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
else:
if 'groups' in mdef:
modules = QuantizedFeatureConcat(layers=layers, groups=True, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
else:
modules = QuantizedFeatureConcat(layers=layers, groups=False, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
elif mdef['type'] == 'shortcut': # nn.Sequential() placeholder for 'shortcut' layer
layers = mdef['from']
filters = output_filters[-1]
routs.extend([i + l if l < 0 else l for l in layers])
if quantized == -1 or quantized == 2:
modules = Shortcut(layers=layers, weight='weights_type' in mdef)
else:
if quantized == 3:
if shortcut_way == 1:
modules = COSPTQuantizedShortcut_min(layers=layers, weight='weights_type' in mdef, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
elif shortcut_way == 2:
modules = COSPTQuantizedShortcut_max(layers=layers, weight='weights_type' in mdef, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
else:
if shortcut_way == 1:
modules = QuantizedShortcut_min(layers=layers, weight='weights_type' in mdef, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
elif shortcut_way == 2:
modules = QuantizedShortcut_max(layers=layers, weight='weights_type' in mdef, bits=a_bit,
quantizer_output=quantizer_output,
reorder=reorder, TM=TM, TN=TN,
name="{:04d}".format(i) + "_" +
mdef['type'][:4],
layer_idx=layer_idx, )
elif mdef['type'] == 'reorg3d': # yolov3-spp-pan-scale
pass
elif mdef['type'] == 'yolo':
yolo_index += 1
stride = [32, 16, 8] # P5, P4, P3 strides
if any(x in cfg for x in ['panet', 'yolov4', 'cd53']): # stride order reversed
if not 'yolov4-tiny' in cfg:
stride = list(reversed(stride))
layers = mdef['from'] if 'from' in mdef else []
modules = YOLOLayer(anchors=mdef['anchors'][mdef['mask']], # anchor list
nc=mdef['classes'], # number of classes
img_size=img_size, # (416, 416)
yolo_index=yolo_index, # 0, 1, 2...
layers=layers, # output layers
stride=stride[yolo_index],
quantizer_output=quantizer_output)
# Initialize preceding Conv2d() bias (https://arxiv.org/pdf/1708.02002.pdf section 3.3)
try:
with torch.no_grad():
j = layers[yolo_index] if 'from' in mdef else -1
bias_ = module_list[j][0].bias # shape(255,)
bias = bias_[:modules.no * modules.na].view(modules.na, -1) # shape(3,85)
bias[:, 4] = bias[:, 4] - 4.5 # obj ln((1-0.01)/0.01)约等于4.5
bias[:, 5:] = bias[:, 5:] + math.log(0.6 / (modules.nc - 0.99)) # cls (sigmoid(p) = 1/nc)
module_list[j][0].bias = torch.nn.Parameter(bias_, requires_grad=bias_.requires_grad)
except:
print('WARNING: smart bias initialization failure.')
else:
print('Warning: Unrecognized Layer Type: ' + mdef['type'])
# Register module list and number of output filters
module_list.append(modules)
output_filters.append(filters)
routs_binary = [False] * (i + 1)
for i in routs:
routs_binary[i] = True
return module_list, routs_binary
class YOLOLayer(nn.Module):
def __init__(self, anchors, nc, img_size, yolo_index, layers, stride, quantizer_output):
super(YOLOLayer, self).__init__()
self.anchors = torch.Tensor(anchors)
self.index = yolo_index # index of this layer in layers
self.layers = layers # model output layer indices
self.stride = stride # layer stride
self.nl = len(layers) # number of output layers (3)
self.na = len(anchors) # number of anchors (3)
self.nc = nc # number of classes (80)
self.no = nc + 5 # number of outputs (85)
self.nx, self.ny, self.ng = 0, 0, 0 # initialize number of x, y gridpoints
self.anchor_vec = self.anchors / self.stride
self.anchor_wh = self.anchor_vec.view(1, self.na, 1, 1, 2)
self.quantizer_output = quantizer_output
def create_grids(self, ng=(13, 13), device='cpu'):
self.nx, self.ny = ng # x and y grid size
self.ng = torch.tensor(ng, dtype=torch.float)
# build xy offsets
if not self.training:
yv, xv = torch.meshgrid([torch.arange(self.ny, device=device), torch.arange(self.nx, device=device)])
self.grid = torch.stack((xv, yv), 2).view((1, 1, self.ny, self.nx, 2)).float()
if self.anchor_vec.device != device:
self.anchor_vec = self.anchor_vec.to(device)
self.anchor_wh = self.anchor_wh.to(device)
def forward(self, p, out):
ASFF = False # https://arxiv.org/abs/1911.09516
if ASFF:
i, n = self.index, self.nl # index in layers, number of layers
p = out[self.layers[i]]
bs, _, ny, nx = p.shape # bs, 255, 13, 13
if (self.nx, self.ny) != (nx, ny):
self.create_grids((nx, ny), p.device)
# outputs and weights
# w = F.softmax(p[:, -n:], 1) # normalized weights
w = torch.sigmoid(p[:, -n:]) * (2 / n) # sigmoid weights (faster)
# w = w / w.sum(1).unsqueeze(1) # normalize across layer dimension
# weighted ASFF sum
p = out[self.layers[i]][:, :-n] * w[:, i:i + 1]
for j in range(n):
if j != i:
p += w[:, j:j + 1] * \
F.interpolate(out[self.layers[j]][:, :-n], size=[ny, nx], mode='bilinear', align_corners=False)
else:
bs, _, ny, nx = p.shape # bs, 255, 13, 13
# if (self.nx, self.ny) != (nx, ny):
self.create_grids((nx, ny), p.device)
# p.view(bs, 255, 13, 13) -- > (bs, 3, 13, 13, 85) # (bs, anchors, grid, grid, classes + xywh)
p = p.view(bs, self.na, self.no, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction
if self.training:
return p
else: # inference
io = p.clone() # inference output
if self.quantizer_output == True:
sigmoid_output = p.clone()
io[..., :2] = torch.sigmoid(io[..., :2]) + self.grid # xy
io[..., 2:4] = torch.exp(io[..., 2:4]) * self.anchor_wh # wh yolo method
io[..., :4] *= self.stride
torch.sigmoid_(io[..., 4:])
##输出
if self.quantizer_output == True:
xy_sigmoid_output = torch.sigmoid(sigmoid_output[..., :2])
cls_sigmoid_output = sigmoid_output[..., 4:] * self.stride
cls_sigmoid_output = torch.sigmoid_(cls_sigmoid_output)
xy_sigmoid_output = np.array(xy_sigmoid_output.cpu()).reshape(1, -1)
np.savetxt(('./quantizer_output/xy_sigmoid_output.txt'), xy_sigmoid_output,
delimiter='\n')
writer = open('./quantizer_output/xy_sigmoid_bin', "wb")
writer.write(xy_sigmoid_output)
writer.close()
cls_sigmoid_output = np.array(cls_sigmoid_output.cpu()).reshape(1, -1)
np.savetxt(('./quantizer_output/cls_sigmoid_output.txt'), cls_sigmoid_output,
delimiter='\n')
writer = open('./quantizer_output/cls_sigmoid_bin', "wb")
writer.write(cls_sigmoid_output)
writer.close()
return io.view(bs, -1, self.no), p # view [1, 3, 13, 13, 85] as [1, 507, 85]
class Darknet(nn.Module):
# YOLOv3 object detection model
def __init__(self, cfg, img_size=(416, 416), verbose=False, quantized=-1, a_bit=8, w_bit=8,
quantizer_output=False, layer_idx=-1, reorder=False, TM=32, TN=32, steps=0, is_gray_scale=False,
maxabsscaler=False, shortcut_way=-1):
super(Darknet, self).__init__()
if isinstance(cfg, str):
self.module_defs = parse_model_cfg(cfg)
elif isinstance(cfg, list):
self.module_defs = cfg
self.quantized = quantized
self.a_bit = a_bit
self.w_bit = w_bit
self.quantizer_output = quantizer_output ####输出设置超参数
self.layer_idx = layer_idx
self.reorder = reorder
self.TM = TM
self.TN = TN
self.hyperparams = copy.deepcopy(self.module_defs[0])
self.module_list, self.routs = create_modules(self.module_defs, img_size, cfg, quantized=self.quantized,
quantizer_output=self.quantizer_output, reorder=self.reorder,
TM=self.TM, TN=self.TN, layer_idx=self.layer_idx,
a_bit=self.a_bit, w_bit=self.w_bit, steps=steps,
is_gray_scale=is_gray_scale, maxabsscaler=maxabsscaler,
shortcut_way=shortcut_way)
self.yolo_layers = get_yolo_layers(self)
# torch_utils.initialize_weights(self)
# Darknet Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version = np.array([0, 2, 5], dtype=np.int32) # (int32) version info: major, minor, revision
self.seen = np.array([0], dtype=np.int64) # (int64) number of images seen during training
# 输出modelsummary
if self.quantized == -1:
self.info(verbose) # print model description
def forward(self, x, augment=False):
if not augment:
return self.forward_once(x)
else: # Augment images (inference and test only) https://github.com/ultralytics/yolov3/issues/931
img_size = x.shape[-2:] # height, width
s = [0.83, 0.67] # scales
y = []
for i, xi in enumerate((x,
torch_utils.scale_img(x.flip(3), s[0], same_shape=False), # flip-lr and scale
torch_utils.scale_img(x, s[1], same_shape=False), # scale
)):
# cv2.imwrite('img%g.jpg' % i, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1])
y.append(self.forward_once(xi)[0])
y[1][..., :4] /= s[0] # scale
y[1][..., 0] = img_size[1] - y[1][..., 0] # flip lr
y[2][..., :4] /= s[1] # scale
# for i, yi in enumerate(y): # coco small, medium, large = < 32**2 < 96**2 <
# area = yi[..., 2:4].prod(2)[:, :, None]
# if i == 1:
# yi *= (area < 96. ** 2).float()
# elif i == 2:
# yi *= (area > 32. ** 2).float()
# y[i] = yi
y = torch.cat(y, 1)
return y, None
def forward_once(self, x, augment=False, verbose=False):
img_size = x.shape[-2:] # height, width
yolo_out, out, feature_out = [], [], []
if verbose:
print('0', x.shape)
str = ''
# Augment images (inference and test only)
if augment: # https://github.com/ultralytics/yolov3/issues/931
nb = x.shape[0] # batch size
s = [0.83, 0.67] # scales
x = torch.cat((x,
torch_utils.scale_img(x.flip(3), s[0]), # flip-lr and scale
torch_utils.scale_img(x, s[1]), # scale
), 0)
for i, module in enumerate(self.module_list):
name = module.__class__.__name__
if name in ['Shortcut', 'FeatureConcat', 'QuantizedShortcut_max', 'QuantizedShortcut_min',
'QuantizedFeatureConcat', 'COSPTQuantizedShortcut_min',
'COSPTQuantizedShortcut_max', 'COSPTQuantizedFeatureConcat']: # sum, concat
if verbose:
l = [i - 1] + module.layers # layers
sh = [list(x.shape)] + [list(out[i].shape) for i in module.layers] # shapes
str = ' >> ' + ' + '.join(['layer %g %s' % x for x in zip(l, sh)])
x = module(x, out) # Shortcut(), FeatureConcat()
elif name == 'YOLOLayer':
yolo_out.append(module(x, out))
else: # run module directly, i.e. mtype = 'convolutional', 'upsample', 'maxpool', 'batchnorm2d' etc.
if name == 'Upsample' and isinstance(x, list):
x[0] = module(x[0])
x[1] = module(x[1])
else:
x = module(x)
if name == "Sequential" and self.module_list[i + 1].__class__.__name__ != 'YOLOLayer':
feature_out.append(x)
out.append(x if self.routs[i] else [])
if verbose:
print('%g/%g %s -' % (i, len(self.module_list), name), list(x.shape), str)
str = ''
if self.training: # train
return yolo_out, feature_out
else: # inference or test
x, p = zip(*yolo_out) # inference output, training output
x = torch.cat(x, 1) # cat yolo outputs
if augment: # de-augment results
x = torch.split(x, nb, dim=0)
x[1][..., :4] /= s[0] # scale
x[1][..., 0] = img_size[1] - x[1][..., 0] # flip lr
x[2][..., :4] /= s[1] # scale
x = torch.cat(x, 1)
return x, p, feature_out
def fuse(self):
# Fuse Conv2d + BatchNorm2d layers throughout model
print('Fusing layers...')
fused_list = nn.ModuleList()
for a in list(self.children())[0]:
if isinstance(a, nn.Sequential):
for i, b in enumerate(a):
if isinstance(b, nn.modules.batchnorm.BatchNorm2d):
# fuse this bn layer with the previous conv2d layer
conv = a[i - 1]
fused = torch_utils.fuse_conv_and_bn(conv, b)
a = nn.Sequential(fused, *list(a.children())[i + 1:])
break
fused_list.append(a)
self.module_list = fused_list
def info(self, verbose=False):
torch_utils.model_info(self, verbose)
def get_yolo_layers(model):
return [i for i, m in enumerate(model.module_list) if m.__class__.__name__ == 'YOLOLayer'] # [89, 101, 113]
def load_darknet_weights(self, weights, cutoff=-1, pt=False, quant=False):
# Parses and loads the weights stored in 'weights'
# Establish cutoffs (load layers between 0 and cutoff. if cutoff = -1 all are loaded)
file = Path(weights).name
if file == 'darknet53.conv.74':
cutoff = 75
elif file == 'yolov3-tiny.conv.15':
cutoff = 15
# Read weights file
with open(weights, 'rb') as f:
# Read Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version = np.fromfile(f, dtype=np.int32, count=3) # (int32) version info: major, minor, revision
self.seen = np.fromfile(f, dtype=np.int64, count=1) # (int64) number of images seen during training
weights = np.fromfile(f, dtype=np.float32) # The rest are weights
ptr = 0
for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
if mdef['type'] == 'convolutional':
conv_layer = module[0]
if mdef['batch_normalize']:
if quant:
# Load BN bias, weights, running mean and running variance
num_b = conv_layer.beta.numel()
# Bias
bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.beta)
conv_layer.beta.data.copy_(bn_b)
ptr += num_b
# Weight
bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.gamma)
conv_layer.gamma.data.copy_(bn_w)
ptr += num_b
# Running Mean
bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.running_mean)
conv_layer.running_mean.data.copy_(bn_rm)
ptr += num_b
# Running Var
bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.running_var)
conv_layer.running_var.data.copy_(bn_rv)
ptr += num_b
else:
# Load BN bias, weights, running mean and running variance
bn_layer = module[1]
num_b = bn_layer.bias.numel() # Number of biases
# Bias
bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.bias)
bn_layer.bias.data.copy_(bn_b)
ptr += num_b
# Weight
bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.weight)
bn_layer.weight.data.copy_(bn_w)
ptr += num_b
# Running Mean
bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_mean)
bn_layer.running_mean.data.copy_(bn_rm)
ptr += num_b
# Running Var
bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_var)
bn_layer.running_var.data.copy_(bn_rv)
ptr += num_b
# Load conv. weights
num_w = conv_layer.weight.numel()
conv_w = torch.from_numpy(weights[ptr:ptr + num_w]).view_as(conv_layer.weight)
conv_layer.weight.data.copy_(conv_w)
ptr += num_w
else:
# if os.path.basename(file) == 'yolov3.weights' or os.path.basename(file) == 'yolov3-tiny.weights':
# pt标识使用coco预训练模型,读取参数时yolo层前面的一层输出为255
if pt and os.path.basename(file).split('.')[-1] == 'weights':
num_b = 255
ptr += num_b
num_w = int(self.module_defs[i - 1]["filters"]) * 255
ptr += num_w
else:
# Load conv. bias
num_b = conv_layer.bias.numel()
conv_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.bias)
conv_layer.bias.data.copy_(conv_b)
ptr += num_b
# Load conv. weights
num_w = conv_layer.weight.numel()
conv_w = torch.from_numpy(weights[ptr:ptr + num_w]).view_as(conv_layer.weight)
conv_layer.weight.data.copy_(conv_w)
ptr += num_w
elif mdef['type'] == 'depthwise':
depthwise_layer = module[0]
if mdef['batch_normalize']:
if quant:
# Load BN bias, weights, running mean and running variance
num_b = conv_layer.beta.numel()
# Bias
bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.beta)
conv_layer.beta.data.copy_(bn_b)
ptr += num_b
# Weight
bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.gamma)
conv_layer.gamma.data.copy_(bn_w)
ptr += num_b
# Running Mean
bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.running_mean)
conv_layer.running_mean.data.copy_(bn_rm)
ptr += num_b
# Running Var
bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(conv_layer.running_var)
conv_layer.running_var.data.copy_(bn_rv)
ptr += num_b
else:
# Load BN bias, weights, running mean and running variance
bn_layer = module[1]
num_b = bn_layer.bias.numel() # Number of biases
# Bias
bn_b = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.bias)
bn_layer.bias.data.copy_(bn_b)
ptr += num_b
# Weight
bn_w = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.weight)
bn_layer.weight.data.copy_(bn_w)
ptr += num_b
# Running Mean
bn_rm = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_mean)
bn_layer.running_mean.data.copy_(bn_rm)
ptr += num_b
# Running Var
bn_rv = torch.from_numpy(weights[ptr:ptr + num_b]).view_as(bn_layer.running_var)
bn_layer.running_var.data.copy_(bn_rv)
ptr += num_b
# Load conv. weights
num_w = depthwise_layer.weight.numel()
conv_w = torch.from_numpy(weights[ptr:ptr + num_w]).view_as(depthwise_layer.weight)
depthwise_layer.weight.data.copy_(conv_w)
ptr += num_w
elif mdef['type'] == 'se':
se_layer = module[0]
fc = se_layer.fc
fc1 = fc[0]
num_fc1 = fc1.weight.numel()
fc1_w = torch.from_numpy(weights[ptr:ptr + num_fc1]).view_as(fc1.weight)
fc1.weight.data.copy_(fc1_w)
ptr += num_fc1
fc2 = fc[2]
num_fc2 = fc2.weight.numel()
fc2_w = torch.from_numpy(weights[ptr:ptr + num_fc2]).view_as(fc2.weight)
fc2.weight.data.copy_(fc2_w)
ptr += num_fc2
# 确保指针到达权重的最后一个位置
assert ptr == len(weights)
def save_weights(self, path='model.weights', cutoff=-1):
# Converts a PyTorch model to Darket format (*.pt to *.weights)
# Note: Does not work if model.fuse() is applied
with open(path, 'wb') as f:
# Write Header https://github.com/AlexeyAB/darknet/issues/2914#issuecomment-496675346
self.version.tofile(f) # (int32) version info: major, minor, revision
self.seen.tofile(f) # (int64) number of images seen during training
# Iterate through layers
for i, (mdef, module) in enumerate(zip(self.module_defs[:cutoff], self.module_list[:cutoff])):
if mdef['type'] == 'convolutional':
conv_layer = module[0]
# If batch norm, load bn first
if mdef['batch_normalize']:
bn_layer = module[1]
bn_layer.bias.data.cpu().numpy().tofile(f)
bn_layer.weight.data.cpu().numpy().tofile(f)
bn_layer.running_mean.data.cpu().numpy().tofile(f)
bn_layer.running_var.data.cpu().numpy().tofile(f)
# Load conv bias
else:
conv_layer.bias.data.cpu().numpy().tofile(f)
# Load conv weights
conv_layer.weight.data.cpu().numpy().tofile(f)
elif mdef['type'] == 'depthwise':
depthwise_layer = module[0]
# If batch norm, load bn first
if mdef['batch_normalize']:
bn_layer = module[1]
bn_layer.bias.data.cpu().numpy().tofile(f)
bn_layer.weight.data.cpu().numpy().tofile(f)
bn_layer.running_mean.data.cpu().numpy().tofile(f)
bn_layer.running_var.data.cpu().numpy().tofile(f)
# Load conv bias
else:
depthwise_layer.bias.data.cpu().numpy().tofile(f)
# Load conv weights
depthwise_layer.weight.data.cpu().numpy().tofile(f)
elif mdef['type'] == 'se':
se_layer = module[0]
fc = se_layer.fc
fc1 = fc[0]
fc2 = fc[2]
fc1.weight.data.cpu().numpy().tofile(f)
fc2.weight.data.cpu().numpy().tofile(f)
def convert(cfg='cfg/yolov3-spp.cfg', weights='weights/yolov3-spp.weights'):
# Converts between PyTorch and Darknet format per extension (i.e. *.weights convert to *.pt and vice versa)
# from models import *; convert('cfg/yolov3-spp.cfg', 'weights/yolov3-spp.weights')
# Initialize model
model = Darknet(cfg)
# Load weights and save
if weights.endswith('.pt'): # if PyTorch format
model.load_state_dict(torch.load(weights, map_location='cpu')['model'])
target = weights.rsplit('.', 1)[0] + '.weights'
save_weights(model, path=target, cutoff=-1)
print("Success: converted '%s' to '%s'" % (weights, target))
elif weights.endswith('.weights'): # darknet format
_ = load_darknet_weights(model, weights)
chkpt = {'epoch': -1,
'best_fitness': None,
'training_results': None,
'model': model.state_dict(),
'optimizer': None}
target = weights.rsplit('.', 1)[0] + '.pt'
torch.save(chkpt, target)
print("Success: converted '%s' to '%'" % (weights, target))
else:
print('Error: extension not supported.')
def attempt_download(weights):
# Attempt to download pretrained weights if not found locally
weights = weights.strip().replace("'", '')
msg = weights + ' missing, try downloading from https://drive.google.com/open?id=1LezFG5g3BCW6iYaV89B2i64cqEUZD7e0'
if len(weights) > 0 and not os.path.isfile(weights):
d = {'yolov3-spp.weights': '16lYS4bcIdM2HdmyJBVDOvt3Trx6N3W2R',
'yolov3.weights': '1uTlyDWlnaqXcsKOktP5aH_zRDbfcDp-y',
'yolov3-tiny.weights': '1CCF-iNIIkYesIDzaPvdwlcf7H9zSsKZQ',
'yolov3-spp.pt': '1f6Ovy3BSq2wYq4UfvFUpxJFNDFfrIDcR',
'yolov3.pt': '1SHNFyoe5Ni8DajDNEqgB2oVKBb_NoEad',
'yolov3-tiny.pt': '10m_3MlpQwRtZetQxtksm9jqHrPTHZ6vo',
'darknet53.conv.74': '1WUVBid-XuoUBmvzBVUCBl_ELrzqwA8dJ',
'yolov3-tiny.conv.15': '1Bw0kCpplxUqyRYAJr9RY9SGnOJbo9nEj',
'yolov3-spp-ultralytics.pt': '1UcR-zVoMs7DH5dj3N1bswkiQTA4dmKF4'}
file = Path(weights).name
if file in d:
r = gdrive_download(id=d[file], name=weights)
else: # download from pjreddie.com
url = 'https://pjreddie.com/media/files/' + file
print('Downloading ' + url)
r = os.system('curl -f ' + url + ' -o ' + weights)
# Error check
if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB
os.system('rm ' + weights) # remove partial downloads
raise Exception(msg)