Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yolo系列多batch推理有误 #2446

Open
LorenzoSun-V opened this issue Apr 30, 2024 · 2 comments
Open

yolo系列多batch推理有误 #2446

LorenzoSun-V opened this issue Apr 30, 2024 · 2 comments
Assignees

Comments

@LorenzoSun-V
Copy link


温馨提示:根据社区不完全统计,按照模板提问,可以加快回复和解决问题的速度


环境

  • 【FastDeploy版本】: release-v1.0.7
  • 【编译命令】:
cmake .. -DENABLE_ORT_BACKEND=ON \
         -DENABLE_PADDLE_BACKEND=ON \
         -DENABLE_OPENVINO_BACKEND=ON \
         -DENABLE_TRT_BACKEND=ON \
         -DWITH_GPU=ON \
         -DTRT_DIRECTORY=${Path/to/TensorRT} \
         -DCUDA_DIRECTORY=/usr/local/cuda \
         -DCMAKE_INSTALL_PREFIX=${PWD}/compiled_fastdeploy_sdk \
         -DENABLE_VISION=ON \
         -DOPENCV_DIRECTORY=${/usr/local/lib/cmake/opencv4}
make -j12
make install
  • 【系统平台】: Linux x64(Ubuntu 22.04)
  • 【硬件】: Nvidia GPU 3090, CUDA 11.8 CUDNN 8.3
  • 【编译语言】: C++

问题日志及出现问题的操作流程

  • 【yolo系列多batch推理有误】
  • 问题详细描述:例如在使用yolov5.onnx(yolov8.onnx也尝试过了,是一样的问题)推理时,当同一个batch中,有一张图没有推理结果时,那张图之后的图即使有推理结果,推理结果也为空。例如一共有四张图片,当batch size = 1时,第0,2,3张有推理结果,第1张没有推理结果,结果正确。当batch size = 4时,仅有第0张有推理结果,第1,2,3均没有推理结果,结果与batch size=1时不一致,结果错误。
    我的多batch推理代码:
void Model::ProcessBatchImage(std::vector<cv::Mat>& batch_images, std::vector<fastdeploy::vision::DetectionResult>* batch_results){
    if (!model->BatchPredict(batch_images, batch_results)) {
        std::cerr << "Failed to predict batch." << std::endl;
        return;
    }
    batch_images.clear();
    batch_results->clear();
}

void Model::InferImagesBatch(const std::vector<std::string>& batch_files){
    std::vector<cv::Mat> batch_images;
    std::vector<std::string> batch_names;
    std::vector<fastdeploy::vision::DetectionResult> batch_results;
    batch_images.reserve(cfg.bs);

    for (const auto& file_path : batch_files) {
        auto image = cv::imread(file_path);
        if (image.empty()) {
            std::cerr << "Failed to load image: " << file_path << std::endl;
            continue; // Skip this image and continue with the next
        }
        batch_images.push_back(image);
        if (batch_images.size() == cfg.bs)
            ProcessBatchImage(batch_images, &batch_results);
    }

    // 处理剩余的图片(如果有)
    if (!batch_images.empty())
        ProcessBatchImage(batch_images, &batch_results);
}

更多代码可以参考:https://github.com/LorenzoSun-V/LorenzoDeploy/tree/main/fd/src中的model.cc,核心的就是model->BatchPredict这个代码。

@jia0511
Copy link

jia0511 commented Apr 30, 2024

+1

@hhx518
Copy link

hhx518 commented May 9, 2024

+1,尤其是第一张没有结果,其他的就没有结果了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants