yolov3-spp darknet版本转caffemodel再转om模型

yolov3-spp darknet版本转caffemodel再转om模型

前段时间,我研究了如何把YOLOv4 darknet版本的模型转换成caffemodel在转换成om模型在Atlas服务器上面推理,如果路过的朋友没有印象,可以先复习下下面两个帖子.
YOLOv4 darknet模型转换成caffemodel再转om在Atlas服务器上面推理
YOLOv4 caffemodel转om在Atlas服务器上面推理

今天工作时候遇到需要把YOLOv3-spp模型转换成离线om模型在Altas服务器上面推理,经过实际实验成功转换,这里把过程都写下来,希望对大家有所帮助,如果大家觉得有用,记得点赞分享加关注哦.

YOLOv3-spp与YOLOv3相比只是多了一个spp(Spatial Pyramid Pooling 空间金字塔池化结构),它是由微软研究院的何凯明大神提出,主要是为了解决两个问题:
有效避免了对图像区域剪裁、缩放操作导致的图像失真等问题;
解决了卷积神经网络对图像重复特征提取的问题,大大提高了产生候选框的速度,且节省了计算成本。

YOLOv3-spp在转换成caffemodel时候和YOLOv4是类似的,只是后续prototxt文件的修改稍微有点不同,下面会直接讲述不同的部分.

1. 修改prototxt文件

1.1 输入描述

对于输入部分的描述,YOLOv3-spp和YOLOv4是一模一样的,这里的入网尺寸是1024*1024,所以把

name: "Darkent2Caffe"
input: "data"
input_dim: 1
input_dim: 3
input_dim: 608
input_dim: 608

修改为

input: "data"
input_shape {
  dim: 1
  dim: 3
  dim: 1024
  dim: 1024
}
input: "img_info"
input_shape {
  dim: 1
  dim: 4
}

1.2 unsample层

和之前YOLOv4的demo一样以"upsample_param"关键词作为搜索,即可以找到需要我们修改的unsample层,分别是:

layer92-conv
layer {
    bottom: "layer92-conv"
    top: "layer93-upsample"
    name: "layer93-upsample"
    type: "Upsample"
    upsample_param {
        scale: 2
    }
}

改成

layer {
    bottom: "layer92-conv"
    top: "layer93-upsample"
    name: "layer93-upsample"
    type: "Upsample"
    upsample_param {
        scale: 1
        stride: 2
    }
}
以及layer104-conv
layer {
    bottom: "layer104-conv"
    top: "layer105-upsample"
    name: "layer105-upsample"
    type: "Upsample"
    upsample_param {
        scale: 2
    }
}

改成

layer {
    bottom: "layer104-conv"
    top: "layer105-upsample"
    name: "layer105-upsample"
    type: "Upsample"
    upsample_param {
        scale: 1
        stride: 2
    }
}

1.3 YOLO相关层

我们都知道YOLOv3会输出3个不同维度的featuremap,所以我们需要先找到这三个Featuremap.这里我们用Netron软件打开转换完成的featuremap查看这三个输出层,分别是:

layer89-conv

在这里插入图片描述

layer101-conv

在这里插入图片描述

以及layer113-conv

在这里插入图片描述
然后由此根据,我们就可以书写剩下的3个YOLO层与一个YOLODetectionOutput层,注意相关参数如classes, boxes,thresh需要根据模型的实际情况修改,如果不太明白,请看我前面的文章,这里再放一次地址:
YOLOv4 caffemodel转om在Atlas服务器上面推理

三个YOLO层
layer {
	bottom: "layer89-conv"
	top: "yolo1_coords"
	top: "yolo1_obj"
	top: "yolo1_classes"
	name: "yolo1"
	type: "Yolo"
	yolo_param {
		boxes: 3
		coords: 4
		classes: 4
		yolo_version: "V3"
		softmax: true
		background: false
    }
}

layer {
	bottom: "layer101-conv"
	top: "yolo2_coords"
	top: "yolo2_obj"
	top: "yolo2_classes"
	name: "yolo2"
	type: "Yolo"
	yolo_param {
		boxes: 3
		coords: 4
		classes: 4
		yolo_version: "V3"
		softmax: true
		background: false
	}
}

layer {
	bottom: "layer113-conv"
	top: "yolo3_coords"
	top: "yolo3_obj"
	top: "yolo3_classes"
	name: "yolo3"
	type: "Yolo"
	yolo_param {
		boxes: 3
		coords: 4
		classes: 4
		yolo_version: "V3"
		softmax: true
		background: false
	}
}
一个YoloV3DetectionOutput层
layer {
       name: "detection_out3"
       type: "YoloV3DetectionOutput"
       bottom: "yolo1_coords"
       bottom: "yolo2_coords"
       bottom: "yolo3_coords"
       bottom: "yolo1_obj"
       bottom: "yolo2_obj"
       bottom: "yolo3_obj"
       bottom: "yolo1_classes"
       bottom: "yolo2_classes"
       bottom: "yolo3_classes"
       bottom: "img_info"
       top: "box_out"
       top: "box_out_num"
       yolov3_detection_output_param {
                           boxes: 3
                           classes: 4
                           relative: true
                           obj_threshold: 0.25
                           score_threshold: 0.5
                           iou_threshold: 0.2
                           pre_nms_topn: 512
                           post_nms_topn: 1024
                           biases_high: 116
                           biases_high: 90
                           biases_high: 156
                           biases_high: 198
                           biases_high: 373
                           biases_high: 326
                           biases_high: 30
                           biases_high: 61
                           biases_high: 62
                           biases_high: 45
                           biases_high: 59
                           biases_high: 119
                           biases_high: 10
                           biases_high: 13
                           biases_high: 16
                           biases_high: 30
                           biases_high: 33
                           biases_high: 23
       }
}
关于YoloV3DetectionOutput的Anchor

在YOLOv3-spp,如果按照之前biases_high, biases_mid, biases_low的写法,实测时候检测框大小不适合,我经过实验把所有的anchor写成biases_high(详见上面的YoloV3DetectionOutput)能够使得om模型最接近原来模型,具体的原理我也没搞懂,如果有明白的同学可以再评论区教教我原理,谢谢~!至于接下来的内容就和YOLOv4部分一致了.

ALL DONE!
本期的内容就讲述完成了,如果大家觉得有帮助,希望可以点赞分享加关注哦,你们的关注可以给我带来写作的动力.

2021/12/22
本人生日这天写下,呜呜,生日还要上班好惨.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值