linghu8812 / tensorrt_inference

703 stars 206 forks source link

yolov5 rectangle detection #26

Closed xjsxujingsong closed 3 years ago

xjsxujingsong commented 3 years ago

Thanks for your repo. I can successfully run yolov5 with 640x640 or 416x416. But it cannot detect anything with rectangle input image size. In export_onnx.py, I set default=[416, 640]. In config.yaml, I set IMAGE_WIDTH: 640, IMAGE_HEIGHT: 416.

bobbilichandu commented 3 years ago

Same is the case with scaled_yolov4, if you were able to solve it, please share the code snippet here. TIY

xjsxujingsong commented 3 years ago

@chandu1263 The main problem is image width/height switch. I have quickly solved this problem, but have not tested throughtly. There are three places you need to change. In yolov5.cpp

  1. grids = { {3, int(IMAGE_HEIGHT / stride[0]), int(IMAGE_WIDTH / stride[0])}, {3, int(IMAGE_HEIGHT / stride[1]), int(IMAGE_WIDTH / stride[1])}, {3, int(IMAGE_HEIGHT / stride[2]), int(IMAGE_WIDTH / stride[2])}, };

  2. std::vector split_img = { cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength (index + 2)), cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength (index + 1)), cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength * index) };

  3. box.x = (row[0] 2 - 0.5 + w) / grids[n][2] IMAGE_WIDTH ratio; box.y = (row[1] 2 - 0.5 + h) / grids[n][1] IMAGE_HEIGHT ratio;

DaChaoXc commented 3 years ago

@chandu1263 The main problem is image width/height switch. I have quickly solved this problem, but have not tested throughtly. There are three places you need to change. In yolov5.cpp 1. grids = { {3, int(IMAGE_HEIGHT / stride[0]), int(IMAGE_WIDTH / stride[0])}, {3, int(IMAGE_HEIGHT / stride[1]), int(IMAGE_WIDTH / stride[1])}, {3, int(IMAGE_HEIGHT / stride[2]), int(IMAGE_WIDTH / stride[2])}, };

std::vectorcv::Mat split_img = { cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength (index + 2)), cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength (index + 1)), cv::Mat(IMAGE_HEIGHT, IMAGE_WIDTH, CV_32FC1, data + channelLength * index) };

box.x = (row[0] 2 - 0.5 + w) / grids[n][2] IMAGE_WIDTH ratio; box.y = (row[1] 2 - 0.5 + h) / grids[n][1] IMAGE_HEIGHT ratio;

you are right!

linghu8812 commented 3 years ago

solved and support yolov5 4.0