【Atlas500】入门(十)——Centerface移植(成功)

前言: 前面复现了centerface,这是一个简洁,简单的框架。前面也做了ncnn,android平台的移植。这里做Atlas的移植。

1. 开发环境

  • ubuntu16.04
  • pytorch
  • Atlas500

2. pytorch模型训练

采用上一篇博客的安全帽佩戴数据集进行训练,采用centerface项目,训练一个矩形框检测不带关键点的检测模型。

  • 项目自带pytorch2onnx脚本

注意: Atlas的deconvolution操作限制group=1,所以需要对mobilenetv2主干网络的deconvolution group参数改为1就ok。

3. pytorch->caffe->om

3.1 pytorch->caffe

Atlas提供了一个omg模型转换的脚本,只支持caffe,tensorflow的模型。这里采用pytorch->caffe->om的路线。

这里可以用两个项目实现(实测可用)
1.https://github.com/MTlab/onnx2caffe美图的,onnx到caffe

  1. https://github.com/xxradon/PytorchToCaffe不需要转到onnx,支持可以pytorch模型转caffe

以上两个项目实测都是可以的

3.2 caffe->om

命令:

omg --framework 0 --model centernet.prototxt --weight centernet.caffemodel --output center_c --insert_op_conf aipp_center.cfg

注意: aipp_center.cfg配置文件参数的设置

aipp_op{
aipp_mode: static
crop: true
input_format : YUV420SP_U8
load_start_pos_h : 0
load_start_pos_w : 0
src_image_size_h : 512
src_image_size_w : 512
csc_switch : true
rbuv_swap_switch:false
matrix_r0c0 : 256
matrix_r0c1 : 454
matrix_r0c2 : 0
matrix_r1c0 : 256
matrix_r1c1 : -88
matrix_r1c2 : -183
matrix_r2c0 : 256
matrix_r2c1 : 0
matrix_r2c2 : 359
input_bias_0 : 0
input_bias_1 : 128
input_bias_2 : 128
mean_chn_0 : 124
mean_chn_1 : 116
mean_chn_2 : 103
var_reci_chn_0 : 0.01712
var_reci_chn_1 : 0.01751
var_reci_chn_2 : 0.01743
}

Atlas的预处理为: (x - mean_atlas) * var_atlas,而centerface的预处理为 (x/255 - mean) / std,所以 mean_atlas = mean * 255, vat_atlas = 1 / (255 * std)

4. 后处理

这里用Atlas_samples修改而来。

用Atlas开发,一般只用写后处理,这里就没法展开了。

注意:

  • Atlas_samples项目中yolov3和ssd在jpg解码部分用的对齐方式不一样。需要将centerface用yolov3的对齐方式。
# 这里1是yolov3, 2是centerface的推理方式
if (1 == g_modelType || 2 == g_modelType) {
        // set crop area:
        float rx = (float)(jpegdOutData.imgWidth) / (float)(g_detectInputWidth);            // 宽度方向的比例
        float ry = (float)(jpegdOutData.imgHeight) / (float)(g_detectInputHeight);          // 高度方向比例

        int dx = 0;
        int dy = 0;
        float r;
        if (rx > ry) {
            dx = 0;
            r = rx;
            dy = (g_detectInputHeight - jpegdOutData.imgHeight / r) / g_denominator;
        } else {
            dy = 0;
            r = ry;
            dx = (g_detectInputWidth - jpegdOutData.imgWidth / r) / g_denominator;
        }
        
        outputConfigure->outputArea.leftOffset = ALIGN_UP(dx, ALIGN_16);  // align to 16        // 应用开发 P220
        outputConfigure->outputArea.rightOffset = CHECK_ODD(g_detectInputWidth - dx);
        outputConfigure->outputArea.upOffset = CHECK_EVEN(dy);
        outputConfigure->outputArea.downOffset = CHECK_ODD(g_detectInputHeight -  dy);
    } 
  • Atlas_samples项目中的后处理固定格式是:预测bbox的(cx, cy, w, h)中心点归一化,w/h归一化。

5. 简单测试

5.1 时间

  • 设备端推理时间

需要通过日志的方式打印。推理+后处理大概10ms左右

[centernet post process] | time:10.000000 ms, end:160000, start:150000, cha:10000
  • 这个脚本运行时间

直接在main.cpp中用printf打印就行

#include<ctime>
using namespace std;
clock_t start,end;
 
int main(){
	start=clock();		//程序开始计时
	end=clock();		//程序结束用时
	double endtime=(double)(end-start)/CLOCKS_PER_SEC;
}
>>> [Atlas 500] | time use:42.643000

大概在40ms左右

6. 结果可视化

便于检查,还是对结果进行可视化一下。

这里采用将打印的结果,copy到python中进行可视化。

import cv2
'''
 #0, bbox(  60,  106,  152,  214) confidence: 0.872998 classId is 0 
 #1, bbox( 379,   35,  462,  133) confidence: 0.717804 classId is 0 
 #2, bbox( 247,   58,  350,  181) confidence: 0.680454 classId is 1

'''
dict_label = {0:'person', 1:'hat'}
bbox = [[60,  106,  152,  214, 0], [379,   35,  462,  133, 0], [247,   58,  350,  181, 1]]
img_path = './data/demo_data/000009.jpg'
img = cv2.imread(img_path)

for box in bbox:
    cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 0, 255), 2)
    label = dict_label[box[4]]
    cv2.putText(img, label, (box[0], box[1] - 2), 0, float(2) / 3, [0, 0, 0], thickness=1, lineType=cv2.LINE_AA)

cv2.imshow('Atlas500', img)
cv2.waitKey()

在这里插入图片描述

其他

  • 如果用atlas进行开发,尽量用常规的算子,比如:conv+bn+relu。实测se模块支持不好(pytorch转caffe不好转)
  • caffe的模型到om这里很关键,所以能有一个强大的pytorch2caffe的项目(不知道这算不算一个痛点)

猜你喜欢

转载自blog.csdn.net/u011622208/article/details/107019169