Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.46k stars 4.17k forks source link

YOLOV8使用自定义数据集训练转化失败。 #4601

Open mabo1215 opened 1 year ago

mabo1215 commented 1 year ago

error log | 日志或报错信息 | ログ

YOLOV8使用自定义数据集训练转化失败。

model | 模型 | モデル

  1. 模型转化路径(预训练使用yolov8s.pt) customize.pt->onnx-> onnxsim-> onnx2ncnn (option)-> ncnnoptimize customize-sim.param customize-sim.bin customize-sim-fp16.param customize-sim-fp16.bin 65536 模型只有4个类。

    how to reproduce | 复现步骤 | 再現方法

    使用自定义数据训练yolov8模型,detect 调用 generate_proposals函数 最后在 generate_proposals函数报错。

其中param 部分内容如下:

7767517 211 256 Input images 0 1 images MemoryData /model.22/Constant_12_output_0 0 1 /model.22/Constant_12_output_0 0=1260 Interp /model.13/Resize 1 1 /model.12/cv2/act/Mul_output_0_splitncnn_1 /model.13/Resize_output_0 0=1 1=2.000000e+00 2=2.000000e+00 3=0 4=0 6=0 Concat /model.14/Concat 2 1 /model.13/Resize_output_0 /model.4/cv2/act/Mul_output_0_splitncnn_0 /model.14/Concat_output_0 0=0 Convolution /model.15/cv1/conv/Conv 1 1 /model.14/Concat_output_0 /model.15/cv1/conv/Conv_output_0 0=128 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=49152 Swish /model.15/cv1/act/Mul 1 1 /model.15/cv1/conv/Conv_output_0 /model.15/cv1/act/Mul_output_0 Convolution /model.18/cv1/conv/Conv 1 1 /model.17/Concat_output_0 /model.18/cv1/conv/Conv_output_0 0=256 1=1 11=1 2=1 12=1 3=1 13=1 4=0 14=0 15=0 16=0 5=1 6=98304 Swish /model.18/cv1/act/Mul 1 1 /model.18/cv1/conv/Conv_output_0 /model.18/cv1/act/Mul_output_0 Split splitncnn_26 1 2 /model.22/Add_1_output_0 /model.22/Add_1_output_0_splitncnn_0 /model.22/Add_1_output_0_splitncnn_1 BinaryOp /model.22/Add_2 2 1 /model.22/Sub_output_0_splitncnn_1 /model.22/Add_1_output_0_splitncnn_1 /model.22/Add_2_output_0 0=0 BinaryOp /model.22/Div_1 1 1 /model.22/Add_2_output_0 /model.22/Div_1_output_0 0=3 1=1 2=2.000000e+00 BinaryOp /model.22/Sub_1 2 1 /model.22/Add_1_output_0_splitncnn_0 /model.22/Sub_output_0_splitncnn_0 /model.22/Sub_1_output_0 0=1 Concat /model.22/Concat_4 2 1 /model.22/Div_1_output_0 /model.22/Sub_1_output_0 /model.22/Concat_4_output_0 0=0 BinaryOp /model.22/Mul_2 2 1 /model.22/Concat_4_output_0 /model.22/Constant_12_output_0 /model.22/Mul_2_output_0 0=2 Sigmoid /model.22/Sigmoid 1 1 /model.22/Split_output_1 /model.22/Sigmoid_output_0 Concat /model.22/Concat_5 2 1 /model.22/Mul_2_output_0 /model.22/Sigmoid_output_0 output0 0=0

generate_proposals部分函数如下: static void generate_proposals(std::vector grid_strides, const ncnn::Mat& pred, float prob_threshold, std::vector& objects) { const int num_points = grid_strides.size(); // const int num_class = 80; const int num_class = 4; const int reg_max_1 = 16; //pred 是 ncnn::Mat out; ex.extract("output", out); 中的out传入 generate_proposals。 for (int i = 0; i < num_points; i++) { const float scores = pred.row(i) + 4 reg_max_1;

    // find label with max score
    int label = -1;
    float score = -FLT_MAX;
    for (int k = 0; k < num_class; k++)
    {

//其中 scores数组中没有任何数据,scores[0]也没有数据。 float confidence = scores[k];

        if (confidence > score)
        {
            label = k;
            score = confidence;
        }
    }
    float box_prob = sigmoid(score);

} }

scores 输出 为0.0000 , scores[0] 不存在

请问如果调整哪些数据来读取预测的 confidence score

glenn-jocher commented 1 year ago

@mabo1215 I have great news 😃! I've recently added official support for Ultralytics YOLOv8 NCNN export ✅ in PR https://github.com/ultralytics/ultralytics/pull/3529 with the help of @nihui which is part of ultralytics==8.0.129. NCNN works for all tasks including Detect, Segment, Pose and Classify.

You can now export with CLI:

yolo export model=yolov8n.pt format=ncnn

or Python:

from ultralytics import YOLO

# Create a model
model = YOLO('yolov8n.pt')

# Export the model to NCNN with arguments
model.export(format='ncnn', half=True, imgsz=640)

Output is a yolov8n_ncnn_model/ directory containing model.bin, model.param and metadata.yaml, along with extra PNNX files. For details see https://github.com/pnnx/pnnx README.

To get this update:

  • Git – Run git pull from within your ultralytics/ directory or run git clone https://github.com/ultralytics/ultralytics again
  • Pip – Update with pip install -U ultralytics
  • Notebooks – Check out the updated notebooks Run on Gradient Open In Colab Open In Kaggle
  • Docker – Run sudo docker pull ultralytics/ultralytics:latest to update your image Docker Pulls

Please let us know if NCNN export is working correctly for you, and don't hesitate to report any other issues you find or feature requests you may have. Happy training with YOLOv8 🚀!

nihui commented 3 months ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx