参考网址:https://github.com/cmdbug/YOLOv5_NCNN

 

这里这三个输出层的名称,每一个人转出来的不一定一样。

 

结构查看:  https://netron.app/

 

查看best.pt转换生成的param文件结构,然后修改

标签名,类名,输出层

//
//  YoloV5.hpp
//  YOLOv5NCNN
//
//  Created by WZTENG on 2020/7/6.
//  Copyright © 2020 TENG. All rights reserved.
//

#ifndef YoloV5_hpp
#define YoloV5_hpp

#include <stdio.h>
#include "ncnn/ncnn/net.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIImage.h>
#import <functional>

namespace yolocv {
    typedef struct {
        int width;
        int height;
    }YoloSize;
}

typedef struct {
    std::string name;
    int stride;
    std::vector<yolocv::YoloSize> anchors;
}YoloLayerData;

typedef struct BoxInfo {
    float x1;
    float y1;
    float x2;
    float y2;
    float score;
    int label;
}BoxInfo;

class YoloV5 {
public:
    YoloV5(bool useGPU);
    ~YoloV5();
    std::vector<BoxInfo> dectect(UIImage *image, float threshold, float nms_threshold);
    //改成自己的类别
    std::vector<std::string> labels{"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
        "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
        "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
        "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
        "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
        "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
        "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone",
        "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear",
        "hair drier", "toothbrush"};
    
//    std::vector<std::string> labels{"sender", "receiver"};

private:
    static std::vector<BoxInfo> decode_infer(ncnn::Mat &data, int stride, const yolocv::YoloSize& frame_size, int net_size, int num_classes, const std::vector<yolocv::YoloSize>& anchors, float threshold);
    static void nms(std::vector<BoxInfo>& result, float nms_threshold);
    
    ncnn::Net* Net;
    int input_size = 640;
    int num_class = 80;  // 修改成自己的类别
    std::vector<YoloLayerData> layers{  // 根据param文件去改
        {"394", 32, {{116, 90}, {156, 198}, {373, 326}}},
        {"375", 16, {{30, 61}, {62, 45}, {59, 119}}},
        {"output", 8, {{10, 13}, {16, 30}, {33, 23}}},
    };
    
public:
    static YoloV5 *detector;
    static bool hasGPU;
    static bool toUseGPU;
    
};


#endif /* YoloV5_hpp */

 

附:宏观思路

ONNX转换NCNN部署到移动端

第一步:测试训练好的权重文件能否正常检测图片

python detect.py --source xxx --weights yolov5s.pt --conf 0.25

 

第二步: 转换pt模型文件到 onnx模型

python models/export.py --weights yolov5s.pt --img  640  --batch 1

 

第三步: 简化模型

python -m onnxsim yolov5s.onnx yolov5s-sim.onnx

 

附:如果提示

Unsupported slice step !

Unsupported slice step !

Unsupported slice step

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐