tanluren / yolov3-channel-and-layer-pruning Goto Github PK
View Code? Open in Web Editor NEWyolov3 yolov4 channel and layer pruning, Knowledge Distillation 层剪枝,通道剪枝,知识蒸馏
License: Apache License 2.0
yolov3 yolov4 channel and layer pruning, Knowledge Distillation 层剪枝,通道剪枝,知识蒸馏
License: Apache License 2.0
我是普通单目标的检测,感觉用不着那么多的参数.
这个问题怎么解决啊?
您好,我训练完之后,用opencv的dnn调用权重,但是什么都没检测出来
WARNING: non-finite loss, ending training tensor([ nan, 0.22070, 0.00000, nan], device='cuda:0')
我使用如下命令进行剪枝
`python train.py --cfg cfg/yolov3.cfg --data data
--weights weights/last.pt
--epochs 100 --batch-size 16 -sr --s 0.001 --prune 1
python shortcut_prune.py --cfg cfg/yolov3.cfg --data data
--weights weights/last.pt
--percent 0.6`
稀疏训练没有100epochs就停止了。
然后结果非常糟糕
请问有何建议?
是需要更多的稀疏训练?然后需要fine tune吗。剪枝之后,不训练,map也可以正常吗?
inference没变?是因为我使用了显卡吗。本来很快?所以剪枝加速不明显?(使用的1080ti单卡)
Epoch gpu_mem GIoU obj cls total soft targets img_size
0%| | 0/309 [00:00<?, ?it/s]Traceback (most recent call last):
File "train.py", line 534, in
train() # train normally
File "train.py", line 313, in train
idx2mask = get_mask2(model, prune_idx, 0.95)
NameError: name 'get_mask2' is not defined
0%| | 0/309 [00:00<?, ?it/s]
请问是不是 命名有问题?
我改成 get_mask之后也报错
你好, 为什么 我运行时 会报这个错误
img, labels = load_mosaic(self, index)
File "O:\Deep_Learning\Dataset\YOLOv3\yolov3-channel-and-layer-pruning-master\utils\datasets.py", line 607, in load_mosaic
labels4.append(labels)
UnboundLocalError: local variable 'labels' referenced before assignment
在prune_utils.py中,对激活值的处理中,这一句next_bn.running_mean.data.sub_(offset),这里为什么用的是sub,这里有点不明白?running_mean不应该是对数据统计的均值吗?
print('learning rate:',optimizer.param_groups[0]['lr']) 是在调整学习速率
lr = adjust_learning_rate(optimizer, 0.1, epoch, ni, nb)之前执行,打印的lr不是当前的lr,而是上一个epoch的学习速率lr
VOC数据集稀疏化训练map很低,使用的yolov3.weights,训练100步map只有10几,loss也在震荡,是不是稀疏化训练只适合单一目标,例如:手的数据集
在不用稀疏化训练时,就能保证很高的map,大佬请教下
hello ,author,i use your code to detect the object but occur some problems ,there are two box arround head and cant deal with it by nms。
i want to detect person hand head 。 i think groudtruth match the anchor which has largest IOU with groudtruth between different yolo layer ,the code may do this by matching the groudtruth with anchor in every yolo layer and this make some problem in the detection result,hope answer,thx。
大佬,请教下用官网yolo(C++)调用剪枝后的cfg和weights文件,为什么什么都检测不到
gpu have enough space,but it always show this matter
torch安装需要python64位,但是报错OSError: [WinError 193] %1 不是有效的 Win32 应用程序。
改正32位的python版本,torch又会报错,报错为找不到torch._C
no question
十分感谢大佬贡献代码,让我们有学习的机会。
不好意思。我又来提问了。十分感谢您的回答。
有两点疑问
1,代码里面,L307,如果是shortcut前一层conv,使用from的conv层的mask。但是这个mask 不能代表 “shortcut前一层conv” 这层的卷积的重要顺序了。
我想了一个规则,不知道是否可行:
使用“from的conv层的mask”中剪枝的数量,再造一个mask2,使sum(mask2)=sum(mask);然后对“shortcut前一层conv”这层的卷积排序,取最大的。
但是随之带来的问题是,两个mask 相加之后通道数就不一样了。所以后面可能要修改创建网络的规则。
第2个疑问
虽然我们使用用bn的剪枝规则与《Learning Efficient Convolutional Networks Through Network Slimming》论文中是一样的。但是不知道您是否有思考过。
剪枝的阈值是通过 所有bn的weight排序得到的。这真的公平吗?
①网络最前面的数值,更靠近图像像素值,最后一层更靠近类别概率。bn的weight不一定分布相同
②在网络中间有shortcut,两个卷积像素值叠加后,weight参数变大。可能会影响bn的weight。
不知道是否存在①②的现象。
issues35
期待您的回复。十分感谢
用官方yolo跑的自己数据集香烟和手机的目标检测,mAp大概在70多
然后进行稀疏训练,第一个epoch的mAp就变成0了?
请问是不是只对单目标检测效果较好啊?
步骤:稀疏化训练
ssh://[email protected]:8026/usr/local/bin/python -u /project/pytorch-yolov3/train_prune.py --data=data/person_1cls.data --batch-size=4 --cfg=cfg/yolov3-spp-1cls-a2.cfg --weights=weights/yolov3-spp.weights --device=0 --prebias -sr --s=0.001 --prune=0
Namespace(accumulate=2, adam=False, arc='defaultpw', batch_size=4, bucket='', cache_images=False, cfg='cfg/yolov3-spp-1cls-a2.cfg', data='data/person_1cls.data', device='0', epochs=273, evolve=False, img_size=416, img_weights=False, multi_scale=False, name='', nosave=False, notest=False, prebias=True, prune=0, rect=False, resume=False, s=0.001, sr=True, t_cfg='', t_weights='', transfer=False, var=None, weights='weights/yolov3-spp.weights')
Using CUDA device0 _CudaDeviceProperties(name='GeForce GTX 1660', total_memory=5941MB)
loaded weights from weights/yolov3-spp.weights
normal sparse training
Model Summary: 225 layers, 6.25733e+07 parameters, 54 gradients
Starting prebias for 1 epochs...
Epoch gpu_mem GIoU obj cls total soft rratio targets img_size
0%| | 0/1524 [00:00<?, ?it/s]learning rate: 1e-06
Traceback (most recent call last):
File "/project/pytorch-yolov3/train_prune.py", line 537, in <module>
prebias() # optional
File "/project/pytorch-yolov3/train_prune.py", line 484, in prebias
train() # transfer-learn yolo biases for 1 epoch
File "/project/pytorch-yolov3/train_prune.py", line 380, in train
BNOptimizer.updateBN(sr_flag, model.module_list, opt.s, prune_idx, idx2mask)
File "/project/pytorch-yolov3/utils/prune_utils.py", line 148, in updateBN
bn_module.weight.grad.data.add_(s * torch.sign(bn_module.weight.data)) # L1
AttributeError: 'NoneType' object has no attribute 'data'
0%| | 0/1524 [00:02<?, ?it/s]
Process finished with exit code 1
关掉prebias后可以训练,请问大家有没有这个问题?
剪枝命令:
python layer_channel_prune.py --cfg /usr/cx/darknetalexeyAB/darknet-master/names_data/yolo-obj.cfg --data /usr/cx/darknetalexeyAB/darknet-master/names_data/voc.data --weights weights/best.pt --global_percent 0.85 --layer_keep 0.01 --shortcuts 16
FileNotFoundError: [Errno 2] No such file or directory: '/prune_0.15_keep_0.01_3_shortcut_usr/prune_0.15_keep_0.01_3_shortcut_cx/prune_0.15_keep_0.01_3_shortcut_darknetalexeyAB/prune_0.15_keep_0.01_3_shortcut_darknet-master/prune_0.15_keep_0.01_3_shortcut_names_data/prune_0.15_keep_0.01_3_shortcut_yolo-obj.cfg
大神,请教下,用自己数据集用官方的weight权重训练完成后,剪枝了,还能正常转成pb吗?有没有现有的直接pb权重文件剪枝的技术推荐一下?
我想做yolov3-spp的剪枝,(就是不同网络的剪枝),有哪些参数要修改呢?
yolov3-spp只比yolov3多几个maxpooling
目前我修改了ignore_idx
def parse_module_defs(model):
if hasattr(model, 'module'):
print('muti-gpus sparse')
module_defs = model.module.module_defs
else:
print('single-gpu sparse')
module_defs = model.module_defs
CBL_idx = []
Conv_idx = []
for i, module_def in enumerate(module_defs):
if module_def['type'] == 'convolutional':
if module_def['batch_normalize'] == '1':
CBL_idx.append(i)
else:
Conv_idx.append(i)
ignore_idx = set()
##不裁剪shortcut之前的两个layer
for i, module_def in enumerate(module_defs):
if module_def['type'] == 'shortcut':
ignore_idx.add(i-1)
identity_idx = (i + int(module_def['from']))
if module_defs[identity_idx]['type'] == 'convolutional':
ignore_idx.add(identity_idx)
elif module_defs[identity_idx]['type'] == 'shortcut':
ignore_idx.add(identity_idx - 1)
#上采样层前的卷积层不裁剪,yolov3的CBL_idx[-1]==104,可判断是否为yolov3-spp
if CBL_idx[-1]==104:
print('sparse: yolov3')
ignore_idx.add(84)
ignore_idx.add(96)
else:
print('sparse: yolov3-spp')
#spp前后两层都不剪枝(77,84)
ignore_idx.add(77)
ignore_idx.add(84)
ignore_idx.add(91)
ignore_idx.add(103)
prune_idx = [idx for idx in CBL_idx if idx not in ignore_idx]
return CBL_idx, Conv_idx, prune_idx
如题,如果我想做一个通用的detector怎么办呀
我在进行sparsity时,遇到了如下报错,请问是什么原因导致的呢?谢谢。
Error LOG:
Reading labels (14363 found, 0 missing, 9 empty for 14372 images): 100%|████████████████████████████████████████████| 14372/14372 [02:20<00:00, 102.01it/s]
Model Summary: 225 layers, 6.42767e+07 parameters, 6.42767e+07 gradients
Starting training for 120 epochs...
Epoch gpu_mem GIoU obj cls total soft rratio targets img_size
0%| | 0/450 [00:00<?, ?it/s]learning rate: 1e-06
0/119 6.96G 1.54 1.88 0.968 4.38 0 0 101 416: 100%|██████████████| 450/450 [06:35<00:00, 1.14it/s]
Class Images Targets P R mAP F1: 100%|██████████████████████████████████| 113/113 [02:52<00:00, 1.53s/it]
all 3.59e+03 1.01e+05 0.322 0.453 0.365 0.363
Epoch gpu_mem GIoU obj cls total soft rratio targets img_size
0%| | 0/450 [00:00<?, ?it/s]learning rate: 0.0011625
Traceback (most recent call last):
File "train.py", line 542, in
train() # train normally
File "train.py", line 348, in train
pred = model(imgs)
File "/devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 541, in call
result = self.forward(*input, **kwargs)
File "/devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/nn/parallel/distributed.py", line 459, in forward
self.reducer.prepare_for_backward([])
RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument find_unused_parameters=True
to torch.nn.parallel.DistributedDataParallel
; (2) making sure all forward
function outputs participate in calculating loss. If you already have done the above two steps, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's forward
function. Please include the loss function and the structure of the return value of forward
of your module when reporting this issue (e.g. list, dict, iterable). (prepare_for_backward at /opt/conda/conda-bld/pytorch_1573049387353/work/torch/csrc/distributed/c10d/reducer.cpp:518)
frame #0: c10::Error::Error(c10::SourceLocation, std::string const&) + 0x47 (0x7f0d6836f687 in /devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/lib/libc10.so)
frame #1: c10d::Reducer::prepare_for_backward(std::vector<torch::autograd::Variable, std::allocatortorch::autograd::Variable > const&) + 0x7b7 (0x7f0d6de68667 in /devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #2: + 0x7cfca1 (0x7f0d6de56ca1 in /devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #3: + 0x2065e6 (0x7f0d6d88d5e6 in /devdata/liqm/Tools/miniconda3/lib/python3.7/site-packages/torch/lib/libtorch_python.so)
frame #4: _PyMethodDef_RawFastCallKeywords + 0x254 (0x55be4fe14744 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #5: _PyCFunction_FastCallKeywords + 0x21 (0x55be4fe14861 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #6: _PyEval_EvalFrameDefault + 0x52f8 (0x55be4fe806e8 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #7: _PyEval_EvalCodeWithName + 0x2f9 (0x55be4fdc4539 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #8: _PyFunction_FastCallDict + 0x1d5 (0x55be4fdc5635 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #9: _PyObject_Call_Prepend + 0x63 (0x55be4fde3e53 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #10: PyObject_Call + 0x6e (0x55be4fdd6dbe in /devdata/liqm/Tools/miniconda3/bin/python)
frame #11: _PyEval_EvalFrameDefault + 0x1e42 (0x55be4fe7d232 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #12: _PyEval_EvalCodeWithName + 0x2f9 (0x55be4fdc4539 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #13: _PyFunction_FastCallDict + 0x1d5 (0x55be4fdc5635 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #14: _PyObject_Call_Prepend + 0x63 (0x55be4fde3e53 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #15: + 0x16ba3a (0x55be4fe1ba3a in /devdata/liqm/Tools/miniconda3/bin/python)
frame #16: _PyObject_FastCallKeywords + 0x49b (0x55be4fe1c8fb in /devdata/liqm/Tools/miniconda3/bin/python)
frame #17: _PyEval_EvalFrameDefault + 0x4a96 (0x55be4fe7fe86 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #18: _PyEval_EvalCodeWithName + 0xac9 (0x55be4fdc4d09 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #19: _PyFunction_FastCallKeywords + 0x387 (0x55be4fe13f57 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #20: _PyEval_EvalFrameDefault + 0x416 (0x55be4fe7b806 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #21: _PyEval_EvalCodeWithName + 0x2f9 (0x55be4fdc4539 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #22: PyEval_EvalCodeEx + 0x44 (0x55be4fdc5424 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #23: PyEval_EvalCode + 0x1c (0x55be4fdc544c in /devdata/liqm/Tools/miniconda3/bin/python)
frame #24: + 0x22ab74 (0x55be4fedab74 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #25: PyRun_FileExFlags + 0xa1 (0x55be4fee4eb1 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #26: PyRun_SimpleFileExFlags + 0x1c3 (0x55be4fee50a3 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #27: + 0x236195 (0x55be4fee6195 in /devdata/liqm/Tools/miniconda3/bin/python)
frame #28: _Py_UnixMain + 0x3c (0x55be4fee62bc in /devdata/liqm/Tools/miniconda3/bin/python)
frame #29: __libc_start_main + 0xf0 (0x7f0d71697830 in /lib/x86_64-linux-gnu/libc.so.6)
frame #30: + 0x1db062 (0x55be4fe8b062 in /devdata/liqm/Tools/miniconda3/bin/python)
作者您好,我制作了一个很小的数据集,尝试把程序跑通,运行了如下指令:
python train.py --cfg cfg/yolov3-nameplate.cfg --data data/nameplate.data --weights weights/darknet53.conv.74 --epochs 2 --batch-size 4
训练过程正常(checkpoint、TensorBoard都正常),但程序运行到最后无法退出,不知道是什么问题?
我在windows cpu环境下跑的,pytorch1.3,训练集16张图,测试集4张图
您好,python -c "from models import *; convert('cfg/yolov3.cfg', 'weights/last.pt')" 这里在模型转换的时候,第一个参数是不是要使用cfg/my_cfg.cfg?
Reading labels (8952 found, 0 missing, 0 empty for 8967 images): 100%|███████████▉| 8952/8967 [00:37<00:00,
Reading labels (8967 found, 0 missing, 0 empty for 8967 images): 100%|████████████| 8967/8967 [00:37<00:00, 237.71it/s]
Model Summary: 222 layers, 6.1626e+07 parameters, 6.1626e+07 gradients
Starting training for 273 epochs...
Epoch gpu_mem GIoU obj cls total soft targets img_size
0%| | 0/561 [00:00<?, ?it/s]learning rate: 1e-06
(Pytorch) F:\DeepLearning\YOLO优化\yolov3-channel-and-layer-pruning-master>
你好,我训练开始不久程序就崩溃了,然后弹出了“Python已停止工作”的窗口。请问这是什么原因呢,该如何解决?
for idx in prune_idx:
bn_weights = gather_bn_weights(model.module_list, [idx])
tb_writer.add_histogram('before_train_perlayer_bn_weights/hist', bn_weights.numpy(), idx, bins='doane')
作者新加的这部分代码报错,如上
channel keep percent per layer 0.01这个参数太小了把
File "train.py", line 499, in
train() # train normally
File "train.py", line 327, in train
pred = model(imgs)
File "/home/112831/anaconda3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 494, in call
result = self.forward(*input, **kwargs)
File "/home/112831/code/yolov3/models.py", line 244, in forward
x = module(x, img_size)
File "/home/112831/anaconda3.6/lib/python3.6/site-packages/torch/nn/modules/module.py", line 494, in call
result = self.forward(*input, **kwargs)
File "/home/112831/code/yolov3/models.py", line 149, in forward
p = p.view(bs, self.na, self.nc + 5, self.ny, self.nx).permute(0, 1, 3, 4, 2).contiguous() # prediction
RuntimeError: shape '[5, 3, 12, 52, 52]' is invalid for input of size 243360
想问一下,目前项目支持yolov3和yolov3-spp。如何修改使用yolov3-spp-pan-scale.cfg? 基础训练和稀疏训练可以支持yolov3-spp-pan-scale.cfg,在层和通道剪枝运行layer_channel_prune.py 时AttributeError: 'NoneType' object has no attribute 'reshape',如何修改这部分剪枝代码?
训练自己的数据集,同样的配置在darknet下面可以正常训练,但使用本项目跑完一轮就报错
Corrupt JPEG data: 2 extraneous bytes before marker 0xd9
0/99 8.39G 1.26 1.18 8.95 11.4 0 162 416: 100%|████████████████████████████████████████████████████████████████████████████| 1228/1228 [07:18<00:00, 2.80it/s]
Traceback (most recent call last):
File "train.py", line 527, in
train() # train normally
File "train.py", line 404, in train
save_json=final_epoch and epoch > 0 and 'coco.data' in data)
File "/home/work/deep_learning/yolov3-channel-and-layer-pruning/test.py", line 50, in test
dataset = LoadImagesAndLabels(test_path, img_size, batch_size)
File "/home/work/deep_learning/yolov3-channel-and-layer-pruning/utils/datasets.py", line 270, in init
nb = bi[-1] + 1 # number of batches
IndexError: index -1 is out of bounds for axis 0 with size 0
进入报错位置,打印了n和bi的值,分别为0和[]
请问有人碰到过吗
loaded weights from weights/yolov3.weights
Reading labels: 100%|████████████████████████| 20/20 [00:00<00:00, 78251.94it/s]
Traceback (most recent call last):
File "train.py", line 497, in
train() # train normally
File "train.py", line 239, in train
cache_images=False if opt.prebias else opt.cache_images)
File "/home/nvidia/yolov3-channel-and-layer-pruning-master/utils/datasets.py", line 373, in init
assert nf > 0, 'No labels found. Recommend correcting image and label paths.'
AssertionError: No labels found. Recommend correcting image and label paths.
这个路径该怎么设置,我的数据没有问题,之前用darknet跑过,现在是放在本项目的路径下报这个错,放在darknet那边也是这样。
大神,下一步怎么处理?
大神,你好, 想问下,在稀疏训练之前 , 必须要 先经过基础训练是吧?,还有 基础训练的时候prune默认是1,不用改把?
yolov3改进:4层shortcuts
基础训练命令:python3 train.py --cfg /usr/cx/darknetalexeyAB/darknet-master/names_data/yolo-obj.cfg --data /usr/cx/darknetalexeyAB/darknet-master/names_data/voc.data --weights /usr/cx/darknetalexeyAB/darknet-master/yolov3.weights --epochs 100 --batch-size 20
你好, 我训练自己的数据,经过基础训练,稀疏训练, 和shortcut_prune剪枝, tensorboard 和 剪枝结果都是正常的, 但是最后我用剪枝后的权重跑detect.py 时候报错了,shape '[512, 256, 3, 3]' is invalid for input of size 357392.. 请问经过shortcut_prune剪枝之后得到的权重是否还要处理?...
对tiny yolo剪枝的时候,报错如上?看过一些解决方法,发现并没有用。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.