Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 733bef1

Browse files
authored
Cherry pick amp refactor (#232)
* Ultralytics yolov3 (#224) * initial setup * update logger implementations with wandb and lambda * refactor for loggers and add SparsificationLoggers class * rename sparsification logger * latest updates for yolov3 integration * styling fixes and update recipes * fix issue with wandb import in pytorch logger * fix failing tests for removed code in pytorch logger * fix for quality * Refactor to enable support for Amp in PyTorch and better support future phase2 work (#231) * Refactor to enable support for Amp in PyTorch and better support future phase2 work * replace modifiers sorting in BaseManager * test fixes * add finalize for pruning and quantization modifiers * fix for import in base manager * address comments from review * address comments from review
1 parent ee2ee13 commit 733bef1

32 files changed

+2630
-931
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--
2+
Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
lr0: 0.005
2+
lrf: 0.1
3+
momentum: 0.843
4+
weight_decay: 0.00036
5+
warmup_epochs: 40.0
6+
warmup_momentum: 0.5
7+
warmup_bias_lr: 0.05
8+
box: 0.0296
9+
cls: 0.243
10+
cls_pw: 0.631
11+
obj: 0.301
12+
obj_pw: 0.911
13+
iou_t: 0.2
14+
anchor_t: 2.91
15+
fl_gamma: 0.0
16+
hsv_h: 0.0138
17+
hsv_s: 0.664
18+
hsv_v: 0.464
19+
degrees: 0.373
20+
translate: 0.245
21+
scale: 0.898
22+
shear: 0.602
23+
perspective: 0.0
24+
flipud: 0.00856
25+
fliplr: 0.5
26+
mosaic: 1.0
27+
mixup: 0.243
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
lr0: 0.0
2+
lrf: 0.0
3+
momentum: 0.843
4+
weight_decay: 0.00036
5+
warmup_epochs: 40.0
6+
warmup_momentum: 0.5
7+
warmup_bias_lr: 0.05
8+
box: 0.0296
9+
cls: 0.243
10+
cls_pw: 0.631
11+
obj: 0.301
12+
obj_pw: 0.911
13+
iou_t: 0.2
14+
anchor_t: 2.91
15+
fl_gamma: 0.0
16+
hsv_h: 0.0138
17+
hsv_s: 0.664
18+
hsv_v: 0.464
19+
degrees: 0.373
20+
translate: 0.245
21+
scale: 0.898
22+
shear: 0.602
23+
perspective: 0.0
24+
flipud: 0.00856
25+
fliplr: 0.5
26+
mosaic: 1.0
27+
mixup: 0.243
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# parameters
2+
nc: 80 # number of classes
3+
depth_multiple: 1.0 # model depth multiple
4+
width_multiple: 1.0 # layer channel multiple
5+
act: nn.LeakyReLU # activations
6+
7+
# anchors
8+
anchors:
9+
- [10,13, 16,30, 33,23] # P3/8
10+
- [30,61, 62,45, 59,119] # P4/16
11+
- [116,90, 156,198, 373,326] # P5/32
12+
13+
# darknet53 backbone
14+
backbone:
15+
# [from, number, module, args]
16+
[[-1, 1, Conv, [32, 3, 1]], # 0
17+
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
18+
[-1, 1, Bottleneck, [64]],
19+
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
20+
[-1, 2, Bottleneck, [128]],
21+
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
22+
[-1, 8, Bottleneck, [256]],
23+
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
24+
[-1, 8, Bottleneck, [512]],
25+
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
26+
[-1, 4, Bottleneck, [1024]], # 10
27+
]
28+
29+
# YOLOv3-SPP head
30+
head:
31+
[[-1, 1, Bottleneck, [1024, False]],
32+
[-1, 1, SPP, [512, [5, 9, 13]]],
33+
[-1, 1, Conv, [1024, 3, 1]],
34+
[-1, 1, Conv, [512, 1, 1]],
35+
[-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
36+
37+
[-2, 1, Conv, [256, 1, 1]],
38+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
39+
[[-1, 8], 1, Concat, [1]], # cat backbone P4
40+
[-1, 1, Bottleneck, [512, False]],
41+
[-1, 1, Bottleneck, [512, False]],
42+
[-1, 1, Conv, [256, 1, 1]],
43+
[-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
44+
45+
[-2, 1, Conv, [128, 1, 1]],
46+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
47+
[[-1, 6], 1, Concat, [1]], # cat backbone P3
48+
[-1, 1, Bottleneck, [256, False]],
49+
[-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
50+
51+
[[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
52+
]
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
<!--
2+
Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing,
11+
software distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
---
18+
# General Epoch/LR variables
19+
num_epochs: &num_epochs 300.0
20+
21+
# pruning hyperparameters
22+
init_sparsity: &init_sparsity 0.05
23+
pruning_start_epoch: &pruning_start_epoch 0.0
24+
pruning_end_epoch: &pruning_end_epoch 80.0
25+
update_frequency: &pruning_update_frequency 0.5
26+
27+
28+
# modifiers
29+
training_modifiers:
30+
- !EpochRangeModifier
31+
start_epoch: 0.0
32+
end_epoch: *num_epochs
33+
34+
pruning_modifiers:
35+
- !GMPruningModifier
36+
params:
37+
- model.16.conv.weight
38+
- model.19.cv1.conv.weight
39+
- model.26.cv2.conv.weight
40+
init_sparsity: *init_sparsity
41+
final_sparsity: 0.6
42+
start_epoch: *pruning_start_epoch
43+
end_epoch: *pruning_end_epoch
44+
update_frequency: *pruning_update_frequency
45+
46+
- !GMPruningModifier
47+
params:
48+
- model.12.cv1.conv.weight
49+
- model.14.conv.weight
50+
- model.2.cv1.conv.weight
51+
- model.20.cv2.conv.weight
52+
- model.22.conv.weight
53+
- model.26.cv1.conv.weight
54+
- model.27.1.cv1.conv.weight
55+
- model.28.m.0.weight
56+
- model.28.m.2.weight
57+
- model.4.0.cv1.conv.weight
58+
- model.6.1.cv1.conv.weight
59+
- model.6.2.cv1.conv.weight
60+
- model.6.3.cv1.conv.weight
61+
- model.6.4.cv1.conv.weight
62+
- model.6.5.cv1.conv.weight
63+
- model.6.6.cv1.conv.weight
64+
- model.6.7.cv1.conv.weight
65+
- model.8.0.cv1.conv.weight
66+
- model.8.1.cv1.conv.weight
67+
- model.8.2.cv1.conv.weight
68+
- model.8.3.cv1.conv.weight
69+
- model.8.4.cv1.conv.weight
70+
- model.8.5.cv1.conv.weight
71+
- model.8.6.cv1.conv.weight
72+
- model.8.7.cv1.conv.weight
73+
init_sparsity: *init_sparsity
74+
final_sparsity: 0.75
75+
start_epoch: *pruning_start_epoch
76+
end_epoch: *pruning_end_epoch
77+
update_frequency: *pruning_update_frequency
78+
79+
- !GMPruningModifier
80+
params:
81+
- model.1.conv.weight
82+
- model.10.0.cv1.conv.weight
83+
- model.10.1.cv1.conv.weight
84+
- model.10.2.cv1.conv.weight
85+
- model.10.3.cv1.conv.weight
86+
- model.11.cv1.conv.weight
87+
- model.12.cv2.conv.weight
88+
- model.19.cv2.conv.weight
89+
- model.2.cv2.conv.weight
90+
- model.27.0.cv1.conv.weight
91+
- model.27.0.cv2.conv.weight
92+
- model.27.1.cv2.conv.weight
93+
- model.28.m.1.weight
94+
- model.3.conv.weight
95+
- model.4.0.cv2.conv.weight
96+
- model.4.1.cv1.conv.weight
97+
- model.4.1.cv2.conv.weight
98+
- model.5.conv.weight
99+
- model.6.0.cv1.conv.weight
100+
- model.6.0.cv2.conv.weight
101+
- model.6.1.cv2.conv.weight
102+
- model.6.2.cv2.conv.weight
103+
- model.6.3.cv2.conv.weight
104+
init_sparsity: *init_sparsity
105+
final_sparsity: 0.8
106+
start_epoch: *pruning_start_epoch
107+
end_epoch: *pruning_end_epoch
108+
update_frequency: *pruning_update_frequency
109+
110+
- !GMPruningModifier
111+
params:
112+
- model.10.0.cv2.conv.weight
113+
- model.10.1.cv2.conv.weight
114+
- model.10.2.cv2.conv.weight
115+
- model.10.3.cv2.conv.weight
116+
- model.11.cv2.conv.weight
117+
- model.13.conv.weight
118+
- model.15.conv.weight
119+
- model.20.cv1.conv.weight
120+
- model.21.conv.weight
121+
- model.23.conv.weight
122+
- model.6.4.cv2.conv.weight
123+
- model.6.5.cv2.conv.weight
124+
- model.6.6.cv2.conv.weight
125+
- model.6.7.cv2.conv.weight
126+
- model.7.conv.weight
127+
- model.8.0.cv2.conv.weight
128+
- model.8.1.cv2.conv.weight
129+
- model.8.2.cv2.conv.weight
130+
- model.8.3.cv2.conv.weight
131+
- model.8.4.cv2.conv.weight
132+
- model.8.5.cv2.conv.weight
133+
- model.8.6.cv2.conv.weight
134+
- model.8.7.cv2.conv.weight
135+
- model.9.conv.weight
136+
init_sparsity: *init_sparsity
137+
final_sparsity: 0.92
138+
start_epoch: *pruning_start_epoch
139+
end_epoch: *pruning_end_epoch
140+
update_frequency: *pruning_update_frequency
141+
---
142+
143+
# YOLOv3-SPP Pruned
144+
145+
This recipe creates a sparse, [YOLOv3-SPP](https://arxiv.org/abs/1804.02767) model that
146+
achieves 97% recovery of its baseline accuracy on the COCO detection dataset.
147+
Training was done using 4 GPUs at half precision using a total training batch size of 256
148+
using an the
149+
[SparseML integration with ultralytics/yolov5](https://github.com/neuralmagic/sparseml/tree/main/integrations/ultralytics-yolov3).
150+
151+
When running, adjust hyperparameters based on training environment and dataset.
152+
153+
## Training
154+
To set up the training environment, follow the instructions on the
155+
[integration README](https://github.com/neuralmagic/sparseml/blob/main/integrations/ultralytics-yolov3/README.md).
156+
Using the given training script from the `yolov3` directory the following command can be used
157+
to launch this recipe. The contents of the `hyp.prune.yaml` hyperparameters file is given below.
158+
Adjust the script command for your GPU device setup. Ultralytics supports both DataParallel and DDP.
159+
160+
*script command:*
161+
162+
```
163+
python train.py \
164+
--recipe ../recipes/yolov3-spp.pruned.md \
165+
--weights PRETRAINED_WEIGHTS \
166+
--cfg ../models/yolov3-spp.lrelu.yaml \
167+
--data coco.yaml \
168+
--hyp ../data/hyp.pruned.yaml \
169+
--batch-size 256 \
170+
--name yolov3-spp-lrelu-pruned
171+
```
172+
173+
hyp.prune.yaml:
174+
```yaml
175+
lr0: 0.005
176+
lrf: 0.1
177+
momentum: 0.843
178+
weight_decay: 0.00036
179+
warmup_epochs: 40.0
180+
warmup_momentum: 0.5
181+
warmup_bias_lr: 0.05
182+
box: 0.0296
183+
cls: 0.243
184+
cls_pw: 0.631
185+
obj: 0.301
186+
obj_pw: 0.911
187+
iou_t: 0.2
188+
anchor_t: 2.91
189+
fl_gamma: 0.0
190+
hsv_h: 0.0138
191+
hsv_s: 0.664
192+
hsv_v: 0.464
193+
degrees: 0.373
194+
translate: 0.245
195+
scale: 0.898
196+
shear: 0.602
197+
perspective: 0.0
198+
flipud: 0.00856
199+
fliplr: 0.5
200+
mosaic: 1.0
201+
mixup: 0.243
202+
```

0 commit comments

Comments
 (0)