yasenh / libtorch-yolov5

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

推理速度 #26

Closed guanyuwang0001 closed 3 years ago

guanyuwang0001 commented 3 years ago

在对您的代码进行简单修改后,使用循环读取本地文件的方式,用yolov5s模型,进行效果测试,发现推理速度只有两三帧,且查看GPU,发现GPU的占用率很小,所以想问下,该工程是不是不支持模型加载一次,而进行预测。修改代码部分如图。期待您的答复,谢谢!

`// load input image std::vector filenames; cv::String folder = "/home/xavier/dataset/DF"; cv::glob(folder, filenames); for(size_t i = 0; i < filenames.size(); ++i) { cv::Mat img = cv::imread(filenames[i]); //std::cout<<"**"<<filenames[i]<<std::endl; if (img.empty()) { std::cerr << "Error loading the image!\n"; return -1; } // load network std::string weights = opt["weights"].as(); auto detector = Detector(weights, device_type);

// set up threshold
float conf_thres = opt["conf-thres"].as<float>();
float iou_thres = opt["iou-thres"].as<float>();

// inference
auto result = detector.Run(img, conf_thres, iou_thres);

// visualize detections
if (opt["view-img"].as<bool>()) {
    Demo(img, result[0], class_names);
}
//cv::destroyAllWindows();

}`

yasenh commented 3 years ago

@guanyuwang0001, you interference is slow may due to you try to load weights every time you iterating.

You can move the lines below out of your for loop:

// load network std::string weights = opt["weights"].asstd::string(); auto detector = Detector(weights, device_type); // set up threshold float conf_thres = opt["conf-thres"].as(); float iou_thres = opt["iou-thres"].as();

guanyuwang0001 commented 3 years ago

@yasenh ,thank you very much.