FeiGeChuanShu / ncnn-android-yolov8

Real time yolov8 Android demo by ncnn
425 stars 83 forks source link

关于图片使用detect检测出海量object #47

Open jiuxiaying opened 7 months ago

jiuxiaying commented 7 months ago

已经参考 Convert yolov8 model to ncnn model · Digital2Slave/ncnn-android-yolov8-seg Wiki (github.com)

是否有yolov8模型转换为NCNN的详细步骤?我尝试将自己的yolov8模型进行了转换,但是识别效果却不正确 · Issue #8 · FeiGeChuanShu/ncnn-android-yolov8 (github.com)

我将ndk相机换成自己图片输入,确保实现正常输入输出,已经可以实现yolov8seg分割功能。在后面实现yolov8检测的功能中,我用的是自己的检测模型,已经参考修改yolov8网络和参考wiki使用pt-onnx-ncnn方法,导出bin和param,在load方法正确读取模型返回0,在调试过程中有概率卡死在generate_proposals的float confidence = scores[k];中,app部署在安卓端后运行 !,但是会产生非常多的框,并没有正确检测,有什么办法可以消除这种bug? {1B4F99CC-AD31-4a85-BA37-39AAE55545F3} image-20240423033511667 image-20240423035208011 {67D54B79-5C89-448e-ABD6-DF955E23462C} {F0775764-F9CA-4ac2-9D95-A5E916596CB1} {704F0B25-4804-4fe5-9C97-240E7035AB2C}

6a3db9a593cdc3d83924766a7b89e421 Cameraresult

jiuxiaying commented 7 months ago

我查看了demo的param,demo最后一个参数是permute,我的最后参数是concat,这是否对模型推理有影响?

jiuxiaying commented 7 months ago

已经修复问题,原因是新版yolov8训练检测模型网络结构不太一样,不支持原来的修改c2f和detect模块的方法,我尝试ultralytics-8.0.98训练,并修改网络结构,已经成功导出并实现检测

June1124 commented 7 months ago

已经修复问题,原因是新版yolov8训练检测模型网络结构不太一样,不支持原来的修改c2f和detect模块的方法,我尝试ultralytics-8.0.98训练,并修改网络结构,已经成功导出并实现检测

你好,我这边只能基于8.2.0的release版本部署模型,您知道该怎么修改吗

lxyjyy commented 6 months ago

@June1124 nn/modules/head.py 第76行处修改为:return x_cat if self.export else (y, x) ,在移动端ncnn输出结果中,wh要进行转置就可以了

jiuxiaying commented 6 months ago

@June1124 nn/modules/head.py 第76行处修改为:return x_cat if self.export else (y, x) ,在移动端ncnn输出结果中,wh要进行转置就可以了

可以详细说一下如何在ncnn输出结果进行wh的倒置吗

lxyjyy commented 6 months ago

void transpose(const ncnn::Mat& in, ncnn::Mat& out) { ncnn::Option opt; opt.num_threads = 2; opt.use_fp16_storage = false; opt.use_packing_layout = true;

ncnn::Layer* op = ncnn::create_layer("Permute");

// set param
ncnn::ParamDict pd;
pd.set(0, 1);// order_type

op->load_param(pd);

op->create_pipeline(opt);

ncnn::Mat in_packed = in;
{
    // resolve dst_elempack
    int dims = in.dims;
    int elemcount = 0;
    if (dims == 1) elemcount = in.elempack * in.w;
    if (dims == 2) elemcount = in.elempack * in.h;
    if (dims == 3) elemcount = in.elempack * in.c;

    int dst_elempack = 1;
    if (op->support_packing)
    {
        if (elemcount % 8 == 0 && (ncnn::cpu_support_x86_avx2() || ncnn::cpu_support_x86_avx()))
            dst_elempack = 8;
        else if (elemcount % 4 == 0)
            dst_elempack = 4;
    }

    if (in.elempack != dst_elempack)
    {
        convert_packing(in, in_packed, dst_elempack, opt);
    }
}

// forward
op->forward(in_packed, out, opt);

op->destroy_pipeline(opt);

delete op;

}

以上是NCNN的转置代码 下面是调用流程代码: ncnn::Mat out_1; ex.extract("out0", out_1); ncnn::Mat out; transpose(out_1, out);

jiuxiaying commented 6 months ago

void transpose(const ncnn::Mat& in, ncnn::Mat& out) { ncnn::Option opt; opt.num_threads = 2; opt.use_fp16_storage = false; opt.use_packing_layout = true;

ncnn::Layer* op = ncnn::create_layer("Permute");

// set param
ncnn::ParamDict pd;
pd.set(0, 1);// order_type

op->load_param(pd);

op->create_pipeline(opt);

ncnn::Mat in_packed = in;
{
    // resolve dst_elempack
    int dims = in.dims;
    int elemcount = 0;
    if (dims == 1) elemcount = in.elempack * in.w;
    if (dims == 2) elemcount = in.elempack * in.h;
    if (dims == 3) elemcount = in.elempack * in.c;

    int dst_elempack = 1;
    if (op->support_packing)
    {
        if (elemcount % 8 == 0 && (ncnn::cpu_support_x86_avx2() || ncnn::cpu_support_x86_avx()))
            dst_elempack = 8;
        else if (elemcount % 4 == 0)
            dst_elempack = 4;
    }

    if (in.elempack != dst_elempack)
    {
        convert_packing(in, in_packed, dst_elempack, opt);
    }
}

// forward
op->forward(in_packed, out, opt);

op->destroy_pipeline(opt);

delete op;

}

以上是NCNN的转置代码 下面是调用流程代码: ncnn::Mat out_1; ex.extract("out0", out_1); ncnn::Mat out; transpose(out_1, out);

谢谢,我将会去尝试它

LIZGIHO commented 6 months ago

求yolov8.2.4实力分割安卓NCNN部署?

jiuxiaying commented 6 months ago

求yolov8.2.4实力分割安卓NCNN部署?

参考https://github.com/Digital2Slave/ncnn-android-yolov8-seg

LIZGIHO commented 6 months ago

求yolov8.2.4实力分割安卓NCNN部署?

参考https://github.com/Digital2Slave/ncnn-android-yolov8-seg

谢谢

daiker commented 6 months ago

@June1124 nn/modules/head.py 第76行处修改为:return x_cat if self.export else (y, x) ,在移动端ncnn输出结果中,wh要进行转置就可以了

请问一下大佬,除了修改head.py文件,block.py的c2f中的forward函数还需要修改吗?

lxyjyy commented 5 months ago

@daiker 其他文件不用修改

Ahuangaaa commented 3 months ago

@June1124 nn/modules/head.py 第76行处修改为:return x_cat if self.export else (y, x) ,在移动端ncnn输出结果中,wh要进行转置就可以了

大佬你好,是直接使用的modal.export(format=ncnn)进行转换的吗,转换的时候修改head.py,训练的时候不修改吗

lxyjyy commented 2 months ago

训练的时候不修改,我是通过pnnx转换的,没有用modal.export(format=ncnn)进行转换