From 2fef5f0ef26598df58fda7985fa27fabe9da56a6 Mon Sep 17 00:00:00 2001 From: liuqi <46275888+liuqi123123@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:14:24 +0800 Subject: [PATCH] yolov5: read input images only once (#776) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit you only read once。 把预处理图片缓存下来,画框时不必读一次,一张图片后处理时间少10ms。 --- yolov5/yolov5.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/yolov5/yolov5.cpp b/yolov5/yolov5.cpp index c15b1de..e1b5623 100644 --- a/yolov5/yolov5.cpp +++ b/yolov5/yolov5.cpp @@ -379,6 +379,7 @@ int main(int argc, char** argv) { // prepare input data cache in device memory CUDA_CHECK(cudaMalloc((void**)&img_device, MAX_IMAGE_INPUT_SIZE_THRESH * 3)); int fcount = 0; + std::vector imgs_buffer(BATCH_SIZE); for (int f = 0; f < (int)file_names.size(); f++) { fcount++; if (fcount < BATCH_SIZE && f + 1 != (int)file_names.size()) continue; @@ -386,7 +387,8 @@ int main(int argc, char** argv) { float* buffer_idx = (float*)buffers[inputIndex]; for (int b = 0; b < fcount; b++) { cv::Mat img = cv::imread(img_dir + "/" + file_names[f - fcount + 1 + b]); - if (img.empty()) continue; + if (img.empty()) continue; + imgs_buffer[b] = img; size_t size_image = img.cols * img.rows * 3; size_t size_image_dst = INPUT_H * INPUT_W * 3; //copy data to pinned memory @@ -408,7 +410,7 @@ int main(int argc, char** argv) { } for (int b = 0; b < fcount; b++) { auto& res = batch_res[b]; - cv::Mat img = cv::imread(img_dir + "/" + file_names[f - fcount + 1 + b]); + cv::Mat img = imgs_buffer[b]; for (size_t j = 0; j < res.size(); j++) { cv::Rect r = get_rect(img, res[j].bbox); cv::rectangle(img, r, cv::Scalar(0x27, 0xC1, 0x36), 2);