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

Commit 0f30713

Browse files
markurtzjeanniefinksbfineran
authored
YOLOv5 recipes and apply a recipe tutorial (#334)
* YOLOv5 recipes and tutorials * fix for reloading set lr modifier * add model disk size info to yolov5 apply a recipe tutorial table * update descriptions for recipes * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> * minor updates and remove transfer learning to break that out into a separate diff * updates from review * run make style * fix export folder structure example * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Benjamin Fineran <bfineran@users.noreply.github.com> * Update integrations/ultralytics-yolov5/tutorials/sparsifying_yolov5_using_recipes.md Co-authored-by: Benjamin Fineran <bfineran@users.noreply.github.com> Co-authored-by: Jeannie Finks <74554921+jeanniefinks@users.noreply.github.com> Co-authored-by: Benjamin Fineran <bfineran@users.noreply.github.com>
1 parent d9385b0 commit 0f30713

File tree

10 files changed

+1209
-1
lines changed

10 files changed

+1209
-1
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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: Hardswish
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+
# YOLOv5 backbone
14+
backbone:
15+
# [from, number, module, args]
16+
[[-1, 1, Focus, [64, 3]], # 0-P1/2
17+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18+
[-1, 3, C3, [128]],
19+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20+
[-1, 9, C3, [256]],
21+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22+
[-1, 9, C3, [512]],
23+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24+
[-1, 1, SPP, [1024, [5, 9, 13]]],
25+
[-1, 3, C3, [1024, False]], # 9
26+
]
27+
28+
# YOLOv5 head
29+
head:
30+
[[-1, 1, Conv, [512, 1, 1]],
31+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
32+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
33+
[-1, 3, C3, [512, False]], # 13
34+
35+
[-1, 1, Conv, [256, 1, 1]],
36+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
37+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
38+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
39+
40+
[-1, 1, Conv, [256, 3, 2]],
41+
[[-1, 14], 1, Concat, [1]], # cat head P4
42+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43+
44+
[-1, 1, Conv, [512, 3, 2]],
45+
[[-1, 10], 1, Concat, [1]], # cat head P5
46+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47+
48+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49+
]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# parameters
2+
nc: 80 # number of classes
3+
depth_multiple: 0.33 # model depth multiple
4+
width_multiple: 0.50 # layer channel multiple
5+
act: Hardswish
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+
# YOLOv5 backbone
14+
backbone:
15+
# [from, number, module, args]
16+
[[-1, 1, Focus, [64, 3]], # 0-P1/2
17+
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18+
[-1, 3, C3, [128]],
19+
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20+
[-1, 9, C3, [256]],
21+
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22+
[-1, 9, C3, [512]],
23+
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
24+
[-1, 1, SPP, [1024, [5, 9, 13]]],
25+
[-1, 3, C3, [1024, False]], # 9
26+
]
27+
28+
# YOLOv5 head
29+
head:
30+
[[-1, 1, Conv, [512, 1, 1]],
31+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
32+
[[-1, 6], 1, Concat, [1]], # cat backbone P4
33+
[-1, 3, C3, [512, False]], # 13
34+
35+
[-1, 1, Conv, [256, 1, 1]],
36+
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
37+
[[-1, 4], 1, Concat, [1]], # cat backbone P3
38+
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
39+
40+
[-1, 1, Conv, [256, 3, 2]],
41+
[[-1, 14], 1, Concat, [1]], # cat head P4
42+
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
43+
44+
[-1, 1, Conv, [512, 3, 2]],
45+
[[-1, 10], 1, Concat, [1]], # cat head P5
46+
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
47+
48+
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
49+
]

integrations/ultralytics-yolov5/recipes/.gitkeep

Whitespace-only changes.
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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 Hyperparams
19+
num_epochs: &num_epochs 180
20+
init_lr: &init_lr 0.01
21+
final_lr: &final_lr 0.002
22+
weights_warmup_lr: &weights_warmup_lr 0
23+
biases_warmup_lr: &biases_warmup_lr 0.1
24+
25+
# Pruning Hyperparams
26+
init_sparsity: &init_sparsity 0.05
27+
pruning_start_epoch: &pruning_start_epoch 4
28+
pruning_end_epoch: &pruning_end_epoch 100
29+
update_frequency: &pruning_update_frequency 1.0
30+
prune_none_target_sparsity: &prune_none_target_sparsity 0.6
31+
prune_low_target_sparsity: &prune_low_target_sparsity 0.75
32+
prune_mid_target_sparsity: &prune_mid_target_sparsity 0.85
33+
prune_high_target_sparsity: &prune_high_target_sparsity 0.9
34+
35+
training_modifiers:
36+
- !EpochRangeModifier
37+
start_epoch: 0
38+
end_epoch: *num_epochs
39+
40+
- !LearningRateFunctionModifier
41+
start_epoch: 3
42+
end_epoch: *num_epochs
43+
lr_func: cosine
44+
init_lr: *init_lr
45+
final_lr: *final_lr
46+
47+
- !LearningRateFunctionModifier
48+
start_epoch: 0
49+
end_epoch: 3
50+
lr_func: linear
51+
init_lr: *weights_warmup_lr
52+
final_lr: *init_lr
53+
param_groups: [0, 1]
54+
55+
- !LearningRateFunctionModifier
56+
start_epoch: 0
57+
end_epoch: 3
58+
lr_func: linear
59+
init_lr: *biases_warmup_lr
60+
final_lr: *init_lr
61+
param_groups: [2]
62+
63+
pruning_modifiers:
64+
- !GMPruningModifier
65+
params:
66+
- model.2.cv3.conv.weight
67+
- model.2.cv1.conv.weight
68+
- model.2.m.2.cv1.conv.weight
69+
- model.2.m.1.cv1.conv.weight
70+
- model.2.m.0.cv1.conv.weight
71+
- model.24.m.0.weight
72+
- model.24.m.1.weight
73+
- model.24.m.2.weight
74+
init_sparsity: *init_sparsity
75+
final_sparsity: *prune_none_target_sparsity
76+
start_epoch: *pruning_start_epoch
77+
end_epoch: *pruning_end_epoch
78+
update_frequency: *pruning_update_frequency
79+
80+
- !GMPruningModifier
81+
params:
82+
- model.2.cv2.conv.weight
83+
- model.6.m.7.cv1.conv.weight
84+
- model.6.cv1.conv.weight
85+
- model.6.cv3.conv.weight
86+
- model.1.conv.weight
87+
- model.13.m.0.cv1.conv.weight
88+
- model.17.cv1.conv.weight
89+
- model.4.m.0.cv1.conv.weight
90+
- model.17.m.2.cv1.conv.weight
91+
- model.4.cv2.conv.weight
92+
- model.2.m.0.cv2.conv.weight
93+
- model.2.m.2.cv2.conv.weight
94+
- model.2.m.1.cv2.conv.weight
95+
- model.4.m.1.cv1.conv.weight
96+
- model.4.m.2.cv1.conv.weight
97+
- model.4.m.3.cv1.conv.weight
98+
- model.4.m.7.cv1.conv.weight
99+
- model.4.m.8.cv1.conv.weight
100+
- model.4.m.5.cv1.conv.weight
101+
- model.4.m.6.cv1.conv.weight
102+
- model.4.m.4.cv1.conv.weight
103+
- model.17.m.1.cv1.conv.weight
104+
- model.23.m.1.cv1.conv.weight
105+
- model.9.m.1.cv1.conv.weight
106+
- model.23.m.0.cv1.conv.weight
107+
- model.23.m.2.cv1.conv.weight
108+
- model.4.cv3.conv.weight
109+
- model.4.cv1.conv.weight
110+
- model.17.m.0.cv1.conv.weight
111+
- model.23.cv1.conv.weight
112+
- model.9.m.0.cv1.conv.weight
113+
- model.13.m.1.cv1.conv.weight
114+
- model.6.m.8.cv1.conv.weight
115+
- model.9.m.2.cv1.conv.weight
116+
- model.6.m.2.cv1.conv.weight
117+
- model.9.cv1.conv.weight
118+
- model.20.cv1.conv.weight
119+
- model.13.cv1.conv.weight
120+
- model.6.m.4.cv1.conv.weight
121+
- model.6.m.5.cv1.conv.weight
122+
- model.13.m.2.cv1.conv.weight
123+
- model.6.m.6.cv1.conv.weight
124+
- model.6.m.1.cv1.conv.weight
125+
- model.6.m.3.cv1.conv.weight
126+
- model.20.m.1.cv1.conv.weight
127+
- model.20.m.0.cv1.conv.weight
128+
- model.6.m.0.cv1.conv.weight
129+
- model.8.cv1.conv.weight
130+
init_sparsity: *init_sparsity
131+
final_sparsity: *prune_low_target_sparsity
132+
start_epoch: *pruning_start_epoch
133+
end_epoch: *pruning_end_epoch
134+
update_frequency: *pruning_update_frequency
135+
136+
- !GMPruningModifier
137+
params:
138+
- model.20.cv3.conv.weight
139+
- model.6.m.0.cv2.conv.weight
140+
- model.8.cv2.conv.weight
141+
- model.13.m.2.cv2.conv.weight
142+
- model.20.cv2.conv.weight
143+
- model.6.m.8.cv2.conv.weight
144+
- model.6.m.2.cv2.conv.weight
145+
- model.6.m.7.cv2.conv.weight
146+
- model.6.m.1.cv2.conv.weight
147+
- model.6.m.5.cv2.conv.weight
148+
- model.6.m.4.cv2.conv.weight
149+
- model.6.m.6.cv2.conv.weight
150+
- model.6.m.3.cv2.conv.weight
151+
- model.13.m.1.cv2.conv.weight
152+
- model.13.m.0.cv2.conv.weight
153+
- model.5.conv.weight
154+
- model.9.cv3.conv.weight
155+
- model.17.m.2.cv2.conv.weight
156+
- model.17.cv2.conv.weight
157+
- model.20.m.2.cv1.conv.weight
158+
- model.9.cv2.conv.weight
159+
- model.17.m.1.cv2.conv.weight
160+
- model.4.m.8.cv2.conv.weight
161+
- model.4.m.7.cv2.conv.weight
162+
- model.4.m.6.cv2.conv.weight
163+
- model.4.m.5.cv2.conv.weight
164+
- model.10.conv.weight
165+
- model.4.m.3.cv2.conv.weight
166+
- model.4.m.2.cv2.conv.weight
167+
- model.4.m.4.cv2.conv.weight
168+
- model.4.m.0.cv2.conv.weight
169+
- model.4.m.1.cv2.conv.weight
170+
- model.17.cv3.conv.weight
171+
- model.6.cv2.conv.weight
172+
- model.3.conv.weight
173+
- model.13.cv3.conv.weight
174+
- model.13.cv2.conv.weight
175+
- model.14.conv.weight
176+
init_sparsity: *init_sparsity
177+
final_sparsity: *prune_mid_target_sparsity
178+
start_epoch: *pruning_start_epoch
179+
end_epoch: *pruning_end_epoch
180+
update_frequency: *pruning_update_frequency
181+
182+
- !GMPruningModifier
183+
params:
184+
- model.23.m.2.cv2.conv.weight
185+
- model.21.conv.weight
186+
- model.17.m.0.cv2.conv.weight
187+
- model.23.m.1.cv2.conv.weight
188+
- model.23.m.0.cv2.conv.weight
189+
- model.9.m.1.cv2.conv.weight
190+
- model.9.m.0.cv2.conv.weight
191+
- model.23.cv3.conv.weight
192+
- model.9.m.2.cv2.conv.weight
193+
- model.23.cv2.conv.weight
194+
- model.20.m.2.cv2.conv.weight
195+
- model.18.conv.weight
196+
- model.7.conv.weight
197+
- model.20.m.1.cv2.conv.weight
198+
- model.20.m.0.cv2.conv.weight
199+
init_sparsity: *init_sparsity
200+
final_sparsity: *prune_high_target_sparsity
201+
start_epoch: *pruning_start_epoch
202+
end_epoch: *pruning_end_epoch
203+
update_frequency: *pruning_update_frequency
204+
---
205+
206+
# YOLOv5l Pruned
207+
208+
This recipe creates a sparse, [YOLOv5l](https://github.com/ultralytics/yolov5) model that achieves 98% recovery of its baseline accuracy on the COCO dataset (0.654 mAP@0.5 baseline vs 0.643 mAP@0.5 for this recipe).
209+
Training was done using 4 GPUs at half precision with a total batch size of 64 using the [SparseML integration with ultralytics/yolov5](https://github.com/neuralmagic/sparseml/tree/main/integrations/ultralytics-yolov5).
210+
211+
When running, adjust hyperparameters based on training environment and dataset.
212+
213+
## Training
214+
215+
To set up the training environment, follow the instructions on the [integration README](https://github.com/neuralmagic/sparseml/blob/main/integrations/ultralytics-yolov5/README.md).
216+
Using the given training script from the `yolov5` directory the following command can be used to launch this recipe.
217+
Adjust the script command for your GPU device setup.
218+
Ultralytics supports both DataParallel and DDP.
219+
220+
*script command:*
221+
222+
```
223+
python train.py \
224+
--cfg ../models/yolov5l.yaml \
225+
--weights PRETRAINED_WEIGHTS \
226+
--data coco.yaml \
227+
--hyp data/hyp.scratch.yaml \
228+
--recipe ../recipes/yolov5l.pruned.md \
229+
```

0 commit comments

Comments
 (0)