Git Product home page Git Product logo

yolov5_prune's Introduction

Introduction

Clean code version of YOLOv5(V6) pruning.

The original code comes from : https://github.com/midasklr/yolov5prune.

Steps:

  1. Basic training

    • In COCO Dataset
      python train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 32 --device 0 --epochs 300 --name coco --optimizer AdamW --data data/coco.yaml
  2. Sparse training

    • In COCO Dataset
      python train.py --batch 32 --epochs 50 --weights weights/yolov5s.pt --data data/coco.yaml --cfg models/yolov5s.yaml --name coco_sparsity --optimizer AdamW --bn_sparsity --sparsity_rate 0.00005 --device 0
  3. Pruning

    • In COCO Dataset
      python prune.py --percent 0.5 --weights runs/train/coco_sparsity13/weights/last.pt --data data/coco.yaml --cfg models/yolov5s.yaml --imgsz 640
  4. Fine-tuning

    • In COCO Dataset
      python train.py --img 640 --batch 32 --epochs 100 --weights runs/val/exp1/pruned_model.pt  --data data/coco.yaml --cfg models/yolov5s.yaml --name coco_ft --device 0 --optimizer AdamW --ft_pruned_model --hyp hyp.finetune_prune.yaml

Experiments

  • Result of COCO Dataset

    exp_name model optim&epoch lr sparity [email protected] note prune threshold BN weight distribution Weight
    coco yolov5s adamw 100 0.01 - 0.5402 - - - -
    coco2 yolov5s adamw 300 0.01 - 0.5534 - - - last.pt
    coco_sparsity yolov5s adamw 50 0.0032 0.0001 0.4826 resume official SGD 0.54 -
    coco_sparsity2 yolov5s adamw 50 0.0032 0.00005 0.50354 resume official SGD 0.48 -
    coco_sparsity3 yolov5s adamw 50 0.0032 0.0005 0.39514 resume official SGD 0.576 -
    coco_sparsity4 yolov5s adamw 50 0.0032 0.001 0.34889 resume official SGD 0.576 -
    coco_sparsity5 yolov5s adamw 50 0.0032 0.00001 0.52948 resume official SGD 0.579 -
    coco_sparsity6 yolov5s adamw 50 0.01 0.0005 0.51202 resume coco 0.564 -
    coco_sparsity10 yolov5s adamw 50 0.01 0.001 0.49504 resume coco2 0.6 -
    coco_sparsity11 yolov5s adamw 50 0.01 0.0005 0.52609 resume coco2 0.6 -
    coco_sparsity13 yolov5s adamw 100 0.01 0.0005 0.533 resume coco2 0.55 last.pt
    coco_sparsity14 yolov5s adamw 50 0.01 0.0007 0.515 resume coco2 0.61 -
    coco_sparsity15 yolov5s adamw 100 0.01 0.001 0.501 resume coco2 0.54 -
  • The model of pruning coco_sparsity13

    coco_sparsity13 [email protected] Params/FLOPs
    origin 0.537 7.2M/16.5G
    after 10% prune 0.5327 6.2M/15.6G
    after 20% prune 0.5327 5.4M/14.7G
    after 30% prune 0.5324 4.4M/13.8G
    after 33% prune 0.5281 4.2M/13.6G
    after 34% prune 0.5243 4.18M/13.5G
    after 34.5% prune 0.5203 4.14M/13.5G
    after 35% prune 0.2548 4.1M/13.4G
    after 38% prune 0.2018 3.88M/13.0G
    after 40% prune 0.1622 3.7M/12.7G
    after 42% prune 0.1194 3.6M/12.4G
    after 45% prune 0.0537 3.4M/12.0G
    after 50% prune 0.0032 3.1M/11.4G

yolov5_prune's People

Contributors

uyzhang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

yolov5_prune's Issues

Facing issue to convert pruned model to tflite model

I pruned the model and try to export that model into the tflite model but I got belo error.

TensorFlow SavedModel: starting export with tensorflow 2.13.0...

             from  n    params  module                                  arguments                     

0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2]
1 -1 1 18560 models.common.Conv [32, 64, 3, 2]

TensorFlow SavedModel: export failure: init() missing 3 required positional arguments: 'cv2out', 'cv3out', and 'bottle_args'

TensorFlow Lite: starting export with tensorflow 2.13.0...

TensorFlow Lite: export failure: 'NoneType' object has no attribute 'call'

yolo.py中parse_pruned_model 获取每层剩余通道数疑问

您好,有一个问题如图中所示:之前求得的mask_bn中key是类似这种model.0.conv.1 , 但是这里mask_bn使用的key是named_m_bn 带有.bn关键字,运行时会报错显示mask_bn没有这个关键字。请问是之前求得mask_bn时 修改了相应的key值嘛?
image

question about sparsity training gpu memory

Hello, after basic training, I tried to run it in the same conditional environment as the sparsity training. But out of memory occurred. I think the only addition is the l1 regularization of bn param (scaling factor, bias), is this a lot of memory when subgradient calculation of that param is done? Or is there another reason? It takes about 2 hour per epoch (Of course, the image size was big, so it took about 40 minutes for basic training) thank you for contrib

稀疏训练epoch设置的更大,在同样的剪枝率下剪枝的结果反而更差!

Hello!您好,我在按照您的代码以及实验13相同的设置下进行的两次剪枝实验;
实验1:sparsity training epochs = 50;
pruning percent = 30%;
结果如下:

| P | origin:0.6032 | after prune:0.5359 | loss ratio:0.1114
| R | origin:0.4673 | after prune:0.3807 | loss ratio:0.1854
| [email protected] | origin:0.5084 | after prune:0.4142 | loss ratio:0.1852
| [email protected]:.95 | origin:0.3244 | after prune:0.2414 | loss ratio:0.2560

finetuning epoch = 100;
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.319
finetuning后精度损失基本和您实验中是差不多的。

实验2:sparsity training epochs = 100;
pruning percent = 30%;
结果如下:

| P | origin:0.6460 | after prune:0.4668 | loss ratio:0.2774
| R | origin:0.4777 | after prune:0.3093 | loss ratio:0.3525
| [email protected] | origin:0.5308 | after prune:0.3079 | loss ratio:0.4201
| [email protected]:.95 | origin:0.3413 | after prune:0.1745 | loss ratio:0.4888

因为直接剪枝的结果更差,所以此处暂未进行finetuning!

问题1:我看您实验13的设置中也是进行了100个epoch的训练,所以很好奇为啥稀疏训练epoch时间更长,反而效果更差了?
或者说这是不是说明稀疏训练的次数并非越多越好,类似于正常训练时可能会因为训练次数过多而导致的过拟合。

问题2:稀疏训练的过程就会导致精度损失,然后在剪枝后也会导致精度损失,即便进行了finetuning也还是存在精度损失,这也就意味着整个剪枝过程中会面临两次精度下降的问题,所以想请教一下您如何看待这个问题?以及是否可以通过稀疏训练更多的epoch来提高精度,或者是稀疏训练较少的次数,但是剪枝后finetuning更长时间来更大程度上恢复精度?

Looking forward to your reply! Thanks!

剪枝报错stride[1,1]

您好,请问您有遇到这种情况嘛
image

最后一行报错内容为
RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]

error

UnboundLocalError: local variable 'srtmp' referenced before assignment

Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/content/yolov5/yolov5_prune/models/yolo.py'>

what's the error?

Traceback (most recent call last):
File "prune.py", line 223, in
main()
File "prune.py", line 209, in main
prune(**params_prune)
File "/usr/local/lib/python3.7/dist-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "prune.py", line 85, in prune
model = DetectMultiBackend(weights, device=device, dnn=dnn, fuse=False)
File "/content/yolov5/yolov5_prune/models/common.py", line 308, in init
model = attempt_load(weights if isinstance(weights, list) else w, map_location=device, fuse=fuse)
File "/content/yolov5/yolov5_prune/models/experimental.py", line 96, in attempt_load
ckpt = torch.load(attempt_download(w), map_location=map_location) # load
File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 712, in load
return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 1049, in _load
result = unpickler.load()
File "/usr/local/lib/python3.7/dist-packages/torch/serialization.py", line 1042, in find_class
return super().find_class(mod_name, name)
AttributeError: Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/content/yolov5/yolov5_prune/models/yolo.py'>

what is the reason to limit percent threshold?

in get_prune_threshold function, percent threshold is calculated.

def get_prune_threshold(model_list, percent):
bn_weights = gather_bn_weights(model_list)
sorted_bn = torch.sort(bn_weights)[0]

# 避免剪掉所有channel的最高阈值(每个BN层的gamma的最大值的最小值即为阈值上限)
highest_thre = []
for bnlayer in model_list.values():
    highest_thre.append(bnlayer.weight.data.abs().max().item())

highest_thre = min(highest_thre)
# 找到highest_thre对应的下标对应的百分比
threshold_index = (sorted_bn == highest_thre).nonzero().squeeze()
if len(threshold_index.shape) > 0:
    threshold_index = threshold_index[0]
percent_threshold = threshold_index.item() / len(bn_weights)
print('Suggested Gamma threshold should be less than {}'.format(highest_thre))
print('The corresponding prune ratio is {}, but you can set higher'.format(percent_threshold))
thre_index = int(len(sorted_bn) * percent)
thre_prune = sorted_bn[thre_index]
print('Gamma value that less than {} are set to zero'.format(thre_prune))
print("=" * 94)
print(f"|\t{'layer name':<25}{'|':<10}{'origin channels':<20}{'|':<10}{'remaining channels':<20}|")
return thre_prune

we use --percent parameter to apply prune.py like
'python prune.py --percent 0.5 --weights runs/train/coco_sparsity2/weights/last.pt --data data/coco.yaml --cfg models/yolov5s.yaml --imgsz 640' and if --percent parameter is bigger than calculated percent_threshold, it happened error.

I tried to use this github code for custom dataset training and pruning.
Please let me know why percent threshold is limited, Thanks.

Prune YOLOv5 with head discarded.

Excellent work on the implementation. I've successfully pruned various YOLOv5 models by adjusting the width and height multipliers. However, I'm encountering an issue: I seem unable to prune any models when the head is discarded. Is there an available solution or method for this situation?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.