yolov3模型压缩

目前有很多的开源实现,基于各种方法对YOLOv3进行裁剪和压缩,例如:
SlimYOLOv3
YOLOv3-model-pruning
YOLOv3-complete-pruning

  • 因为我自己训练的yolov3模型是基于darknet的C语言版本训练的,生成的模型是.weights文件,一般模型压缩使用的都是pytorch或者tensorflow框架的。
  • 于是第一步需要将.weights文件转换成pytorch模型pt文件。(相对tensorflow我更熟悉pytorch一点,所以就转换pytorch模型了。)
模型转换的项目:
convert_yolo_weights
转换脚本
if __name__ == '__main__': convert(cfg='cfg/yolov3.cfg', weights='weights/yolov3.weights')

转换后生成convert.pt
然后就可以基于这个convert.pt模型文件进行模型压缩了。
模型转换完成后需要测试一下是否转换正确,预测一下输出,使用detect.py测试,测试结果如下:
yolov3模型压缩
文章图片

这样看来模型转换结果没有问题。
下一步就需要考虑模型压缩的问题了。
参考:YOLOv3剪枝压缩99.04%参数,精度不变
  • 测试模型剪枝(因为无法上传svg图片,只好上传cfg文件了),我的模型只是从234M压缩到了33.7M,模型大小压缩到了原模型的14.4%。
  • 主要是filter的个数减少了,网络结构没有改变。模型大小比33.1M的yolov3-tiny大一点点,效果高出不少。现在只希望把33.7M的模型继续压缩一下。
    原yolov3模型cfg
[net] # Testing # batch=1 # subdivisions=1 # Training batch=64 subdivisions=8 width=416 height=416 channels=3 momentum=0.9 decay=0.0005 angle=0 saturation = 1.5 exposure = 1.5 hue=.1learning_rate=0.001 burn_in=1000 max_batches = 500200 policy=steps steps=400000,450000 scales=.1,.1[convolutional] batch_normalize=1 filters=32 size=3 stride=1 pad=1 activation=leaky# Downsample[convolutional] batch_normalize=1 filters=64 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=32 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=64 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear# Downsample[convolutional] batch_normalize=1 filters=128 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=128 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear# Downsample[convolutional] batch_normalize=1 filters=256 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear# Downsample[convolutional] batch_normalize=1 filters=512 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear# Downsample[convolutional] batch_normalize=1 filters=1024 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=1024 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=1024 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=1024 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=1024 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear######################[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=1024 activation=leaky[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=1024 activation=leaky[convolutional] batch_normalize=1 filters=512 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=1024 activation=leaky[convolutional] size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask = 6,7,8 anchors = 20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1[route] layers = -4[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers = -1, 61[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=512 activation=leaky[convolutional] size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask = 3,4,5 anchors = 20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1[route] layers = -4[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers = -1, 36[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=256 activation=leaky[convolutional] size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask = 0,1,2 anchors = 20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh = .7 truth_thresh = 1 random=1

剪枝后模型yolov3-prune.cfg
[net] # Testing # batch=1 # subdivisions=1 # Training batch=64 subdivisions=8 width=416 height=416 channels=3 momentum=0.9 decay=0.0005 angle=0 saturation=1.5 exposure=1.5 hue=.1 learning_rate=0.001 burn_in=1000 max_batches=500200 policy=steps steps=400000,450000 scales=.1,.1[convolutional] batch_normalize=1 filters=10 size=3 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=11 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=16 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=11 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=14 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=33 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=14 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=29 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=14 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=19 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=49 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=64 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=59 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=55 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=50 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=60 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=55 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=66 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=19 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=48 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=119 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=117 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=126 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=123 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=133 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=130 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=130 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=131 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=48 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=166 size=3 stride=2 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=250 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=166 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=292 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=166 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=297 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=166 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=295 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 filters=166 size=3 stride=1 pad=1 activation=leaky[shortcut] from=-3 activation=linear[convolutional] batch_normalize=1 filters=238 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=620 activation=leaky[convolutional] batch_normalize=1 filters=252 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=621 activation=leaky[convolutional] batch_normalize=1 filters=245 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=699 activation=leaky[convolutional] batch_normalize=0 size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask=6,7,8 anchors=20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh=.7 truth_thresh=1 random=1[route] layers=-4[convolutional] batch_normalize=1 filters=256 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers=-1, 61[convolutional] batch_normalize=1 filters=100 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=260 activation=leaky[convolutional] batch_normalize=1 filters=108 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=295 activation=leaky[convolutional] batch_normalize=1 filters=144 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=426 activation=leaky[convolutional] batch_normalize=0 size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask=3,4,5 anchors=20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh=.7 truth_thresh=1 random=1[route] layers=-4[convolutional] batch_normalize=1 filters=128 size=1 stride=1 pad=1 activation=leaky[upsample] stride=2[route] layers=-1, 36[convolutional] batch_normalize=1 filters=61 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=140 activation=leaky[convolutional] batch_normalize=1 filters=63 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=150 activation=leaky[convolutional] batch_normalize=1 filters=70 size=1 stride=1 pad=1 activation=leaky[convolutional] batch_normalize=1 size=3 stride=1 pad=1 filters=241 activation=leaky[convolutional] batch_normalize=0 size=1 stride=1 pad=1 filters=24 activation=linear[yolo] mask=0,1,2 anchors=20.11,297.45, 23.75,58.87, 26.94,140.45, 40.95,319.31, 57.66,23.88, 139.99,29.94, 148.85,15.06, 303.99,20.82, 320.36,41.11 classes=3 num=9 jitter=.3 ignore_thresh=.7 truth_thresh=1 random=1

【yolov3模型压缩】参考:
参考:

    推荐阅读