yasenh / libtorch-yolov5

A LibTorch inference implementation of the yolov5
MIT License
372 stars 114 forks source link

改成视频检测后,每次进行第二帧的inference时都会多消耗几百倍的时间。 #47

Open monoloxo opened 3 years ago

monoloxo commented 3 years ago

在main.cpp中改写成视频检测,主要代码如下:

`VideoCapture capture; std::cout << "finish load network and open the video" << std::endl;

capture.open("/home/****/libtorch-yolov5/test.mp4");
if (!capture.isOpened())
    {
        std::cout << "can not open ...\n" << std::endl;
        return -1;
    }
Mat frame;
namedWindow("output",WINDOW_AUTOSIZE);
// set up threshold
float conf_thres = 0.4;//opt["conf-thres"].as<float>();
float iou_thres = 0.5;//opt["iou-thres"].as<float>();
for (;;)
{
    capture >> frame;
    //Mat pic;
    if (frame.empty()) break;
    //imshow("output",frame);
    std::cout << "start forward" <<std::endl;
    auto result = detector.Run(frame, conf_thres, iou_thres);
    Demo(frame, result, class_names);
    imshow("output",frame);
    if (waitKey(33) >= 0) break;
}

capture.release();
cv::destroyAllWindows();
//return 0;`

然后程序运行时,加载模型后第一帧马上就会显示出检测后的图像,也能正确画出检测框,这个过程很快,但第二帧就需要几百倍的时间,在inference阶段。。之后又回复到更短的时间,每次都是这样,换了视频也是如此,我统计了时间: ----------New Frame---------- img size:1080x1920 pre-process takes : 4 ms inference takes : 137 ms <-------------------------------------------------------137 post-process takes : 19 ms start forward ----------New Frame---------- img size:1080x1920 pre-process takes : 5 ms inference takes : 7869 ms <--------------------------------------------------------7869 post-process takes : 24 ms start forward ----------New Frame---------- img size:1080x1920 pre-process takes : 3 ms inference takes : 8 ms <------------------------------------------------------------8 post-process takes : 25 ms start forward ----------New Frame---------- img size:1080x1920 pre-process takes : 4 ms inference takes : 8 ms <-------------------------------------------------------------8 post-process takes : 23 ms

请问这可能是什么原因造成的呢? 注:不知道有什么作用,所以我取消掉了warm up。

CGump commented 2 years ago

同样的问题,我直接循环跑单图前两张图消耗时间非常大