| 28 | } |
| 29 | |
| 30 | void Predict(YoloBody_tiny detector, cv::Mat image, bool show, float conf_thresh, float nms_thresh) { |
| 31 | int origin_width = image.cols; |
| 32 | int origin_height = image.rows; |
| 33 | cv::resize(image, image, { 416,416 }); |
| 34 | auto img_tensor = torch::from_blob(image.data, { image.rows, image.cols, 3 }, torch::kByte); |
| 35 | img_tensor = img_tensor.permute({ 2, 0, 1 }).unsqueeze(0).to(torch::kFloat) / 255.0; |
| 36 | |
| 37 | float anchor[12] = { 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 }; |
| 38 | auto anchors_ = torch::from_blob(anchor, { 6,2 }, torch::TensorOptions(torch::kFloat32)); |
| 39 | int image_size[2] = { 416,416 }; |
| 40 | img_tensor = img_tensor.cuda(); |
| 41 | |
| 42 | auto outputs = detector->forward(img_tensor); |
| 43 | std::vector<torch::Tensor> output_list = {}; |
| 44 | auto tensor_input = outputs[1]; |
| 45 | auto output_decoded = DecodeBox(tensor_input, anchors_.narrow(0, 0, 3), 80, image_size); |
| 46 | output_list.push_back(output_decoded); |
| 47 | |
| 48 | tensor_input = outputs[0]; |
| 49 | output_decoded = DecodeBox(tensor_input, anchors_.narrow(0, 3, 3), 80, image_size); |
| 50 | output_list.push_back(output_decoded); |
| 51 | |
| 52 | //std::cout << tensor_input << anchors_.narrow(0, 3, 3); |
| 53 | |
| 54 | auto output = torch::cat(output_list, 1); |
| 55 | auto detection = non_maximum_suppression(output, 80, conf_thresh, nms_thresh); |
| 56 | |
| 57 | float w_scale = float(origin_width) / 416; |
| 58 | float h_scale = float(origin_height) / 416; |
| 59 | for (int i = 0; i < detection.size(); i++) { |
| 60 | for (int j = 0; j < detection[i].size(0) / 7; j++) |
| 61 | { |
| 62 | detection[i].select(0, 7 * j + 0) *= w_scale; |
| 63 | detection[i].select(0, 7 * j + 1) *= h_scale; |
| 64 | detection[i].select(0, 7 * j + 2) *= w_scale; |
| 65 | detection[i].select(0, 7 * j + 3) *= h_scale; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | cv::resize(image, image, { origin_width,origin_height }); |
| 70 | if (show) |
| 71 | show_bbox_coco(image, detection[0], 80); |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | int main() |
| 76 | { |
no test coverage detected