mirror of
				https://github.com/PaddlePaddle/FastDeploy.git
				synced 2025-11-01 04:12:58 +08:00 
			
		
		
		
	 6a368f3448
			
		
	
	6a368f3448
	
	
	
		
			
			* [FlyCV] Add global SetProcLibCpuNumThreads method * [Android] Support segmentation and facedet in Android * [Android] add JNI instance check to j_runtime_option_obj * [Model] fixed ppseg flycv resize error * [FlyCV] fix FlyCV resize flags * [cmake] remove un-need lite compile option * [Android] add PaddleSegModel JNI and fix some bugs * [Android] bind PaddleSegModel via JNI * [Android] bind VisSegmentation via JNI * [Android] bind YOLOv5Face and SCRFD via JNI * [Android] fix NewJavaFaceDetectionResultFromCxx error
		
			
				
	
	
		
			249 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			249 lines
		
	
	
		
			8.7 KiB
		
	
	
	
		
			C++
		
	
	
		
			Executable File
		
	
	
	
	
| // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
 | |
| //
 | |
| // Licensed under the Apache License, Version 2.0 (the "License");
 | |
| // you may not use this file except in compliance with the License.
 | |
| // You may obtain a copy of the License at
 | |
| //
 | |
| //     http://www.apache.org/licenses/LICENSE-2.0
 | |
| //
 | |
| // Unless required by applicable law or agreed to in writing, software
 | |
| // distributed under the License is distributed on an "AS IS" BASIS,
 | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | |
| // See the License for the specific language governing permissions and
 | |
| // limitations under the License.
 | |
| 
 | |
| #include "fastdeploy/vision/detection/contrib/yolov7end2end_ort.h"
 | |
| #include "fastdeploy/utils/perf.h"
 | |
| #include "fastdeploy/vision/utils/utils.h"
 | |
| 
 | |
| namespace fastdeploy {
 | |
| namespace vision {
 | |
| namespace detection {
 | |
| 
 | |
| void YOLOv7End2EndORT::LetterBox(Mat* mat, const std::vector<int>& size,
 | |
|                                  const std::vector<float>& color, bool _auto,
 | |
|                                  bool scale_fill, bool scale_up, int stride) {
 | |
|   float scale =
 | |
|       std::min(size[1] * 1.0 / mat->Height(), size[0] * 1.0 / mat->Width());
 | |
|   if (!scale_up) {
 | |
|     scale = std::min(scale, 1.0f);
 | |
|   }
 | |
| 
 | |
|   int resize_h = int(round(mat->Height() * scale));
 | |
|   int resize_w = int(round(mat->Width() * scale));
 | |
| 
 | |
|   int pad_w = size[0] - resize_w;
 | |
|   int pad_h = size[1] - resize_h;
 | |
|   if (_auto) {
 | |
|     pad_h = pad_h % stride;
 | |
|     pad_w = pad_w % stride;
 | |
|   } else if (scale_fill) {
 | |
|     pad_h = 0;
 | |
|     pad_w = 0;
 | |
|     resize_h = size[1];
 | |
|     resize_w = size[0];
 | |
|   }
 | |
|   if (resize_h != mat->Height() || resize_w != mat->Width()) {
 | |
|     Resize::Run(mat, resize_w, resize_h);
 | |
|   }
 | |
|   if (pad_h > 0 || pad_w > 0) {
 | |
|     float half_h = pad_h * 1.0 / 2;
 | |
|     int top = int(round(half_h - 0.1));
 | |
|     int bottom = int(round(half_h + 0.1));
 | |
|     float half_w = pad_w * 1.0 / 2;
 | |
|     int left = int(round(half_w - 0.1));
 | |
|     int right = int(round(half_w + 0.1));
 | |
|     Pad::Run(mat, top, bottom, left, right, color);
 | |
|   }
 | |
| }
 | |
| 
 | |
| YOLOv7End2EndORT::YOLOv7End2EndORT(const std::string& model_file,
 | |
|                                    const std::string& params_file,
 | |
|                                    const RuntimeOption& custom_option,
 | |
|                                    const ModelFormat& model_format) {
 | |
|   if (model_format == ModelFormat::ONNX) {
 | |
|     valid_cpu_backends = {Backend::ORT};
 | |
|     valid_gpu_backends = {Backend::ORT};  // NO TRT
 | |
|   } else {
 | |
|     valid_cpu_backends = {Backend::PDINFER};
 | |
|     valid_gpu_backends = {Backend::PDINFER};
 | |
|   }
 | |
|   runtime_option = custom_option;
 | |
|   runtime_option.model_format = model_format;
 | |
|   runtime_option.model_file = model_file;
 | |
|   if (custom_option.backend == Backend::TRT) {
 | |
|     FDWARNING << "Backend::TRT is not support for YOLOv7End2EndORT, "
 | |
|               << "will fallback to Backend::ORT." << std::endl;
 | |
|   }
 | |
|   initialized = Initialize();
 | |
| }
 | |
| 
 | |
| bool YOLOv7End2EndORT::Initialize() {
 | |
|   // parameters for preprocess
 | |
|   size = {640, 640};
 | |
|   padding_value = {114.0, 114.0, 114.0};
 | |
|   is_mini_pad = false;
 | |
|   is_no_pad = false;
 | |
|   is_scale_up = false;
 | |
|   stride = 32;
 | |
|   reused_input_tensors_.resize(1);
 | |
| 
 | |
|   if (!InitRuntime()) {
 | |
|     FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
 | |
|     return false;
 | |
|   }
 | |
|   // Check if the input shape is dynamic after Runtime already initialized,
 | |
|   // Note that, We need to force is_mini_pad 'false' to keep static
 | |
|   // shape after padding (LetterBox) when the is_dynamic_shape is 'false'.
 | |
|   is_dynamic_input_ = false;
 | |
|   auto shape = InputInfoOfRuntime(0).shape;
 | |
|   for (int i = 0; i < shape.size(); ++i) {
 | |
|     // if height or width is dynamic
 | |
|     if (i >= 2 && shape[i] <= 0) {
 | |
|       is_dynamic_input_ = true;
 | |
|       break;
 | |
|     }
 | |
|   }
 | |
|   if (!is_dynamic_input_) {
 | |
|     is_mini_pad = false;
 | |
|   }
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool YOLOv7End2EndORT::Preprocess(
 | |
|     Mat* mat, FDTensor* output,
 | |
|     std::map<std::string, std::array<float, 2>>* im_info) {
 | |
|   float ratio = std::min(size[1] * 1.0f / static_cast<float>(mat->Height()),
 | |
|                          size[0] * 1.0f / static_cast<float>(mat->Width()));
 | |
|   if (std::fabs(ratio - 1.0f) > 1e-06) {
 | |
|     int interp = cv::INTER_AREA;
 | |
|     if (ratio > 1.0) {
 | |
|       interp = cv::INTER_LINEAR;
 | |
|     }
 | |
|     int resize_h = int(mat->Height() * ratio);
 | |
|     int resize_w = int(mat->Width() * ratio);
 | |
|     Resize::Run(mat, resize_w, resize_h, -1, -1, interp);
 | |
|   }
 | |
|   YOLOv7End2EndORT::LetterBox(mat, size, padding_value, is_mini_pad, is_no_pad,
 | |
|                               is_scale_up, stride);
 | |
|   BGR2RGB::Run(mat);
 | |
|   std::vector<float> alpha = {1.0f / 255.0f, 1.0f / 255.0f, 1.0f / 255.0f};
 | |
|   std::vector<float> beta = {0.0f, 0.0f, 0.0f};
 | |
|   Convert::Run(mat, alpha, beta);
 | |
| 
 | |
|   (*im_info)["output_shape"] = {static_cast<float>(mat->Height()),
 | |
|                                 static_cast<float>(mat->Width())};
 | |
| 
 | |
|   HWC2CHW::Run(mat);
 | |
|   Cast::Run(mat, "float");
 | |
|   mat->ShareWithTensor(output);
 | |
|   output->shape.insert(output->shape.begin(), 1);  // reshape to n, h, w, c
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool YOLOv7End2EndORT::Postprocess(
 | |
|     FDTensor& infer_result, DetectionResult* result,
 | |
|     const std::map<std::string, std::array<float, 2>>& im_info,
 | |
|     float conf_threshold) {
 | |
|   if (infer_result.dtype != FDDataType::FP32) {
 | |
|     FDERROR << "Only support post process with float32 data." << std::endl;
 | |
|     return false;
 | |
|   }
 | |
|   // detected success without valid objects.
 | |
|   if (infer_result.shape[0] == 0) {
 | |
|     return true;
 | |
|   }
 | |
| 
 | |
|   result->Clear();
 | |
|   result->Reserve(infer_result.shape[0]);
 | |
|   // (?,7) (batch_id,x0,y0,x1,y1,cls_id,score) after nms
 | |
|   float* data = static_cast<float*>(infer_result.Data());
 | |
|   for (size_t i = 0; i < infer_result.shape[0]; ++i) {
 | |
|     const float* box_cls_ptr = data + (i * 7);
 | |
|     int64_t batch_id = static_cast<int64_t>(box_cls_ptr[0] + 0.5f);  // 0,1, ...
 | |
|     FDASSERT(batch_id == 0,
 | |
|              "Only support batch=1 now, but found batch_id != 0.");
 | |
|     float confidence = box_cls_ptr[6];
 | |
|     if (confidence <= conf_threshold) {
 | |
|       continue;
 | |
|     }
 | |
|     int32_t label_id = static_cast<int32_t>(box_cls_ptr[5] + 0.5f);
 | |
|     float x1 = box_cls_ptr[1];
 | |
|     float y1 = box_cls_ptr[2];
 | |
|     float x2 = box_cls_ptr[3];
 | |
|     float y2 = box_cls_ptr[4];
 | |
| 
 | |
|     result->boxes.emplace_back(std::array<float, 4>{x1, y1, x2, y2});
 | |
|     result->label_ids.push_back(label_id);
 | |
|     result->scores.push_back(confidence);
 | |
|   }
 | |
| 
 | |
|   if (result->boxes.size() == 0) {
 | |
|     return true;
 | |
|   }
 | |
| 
 | |
|   // scale the boxes to the origin image shape
 | |
|   auto iter_out = im_info.find("output_shape");
 | |
|   auto iter_ipt = im_info.find("input_shape");
 | |
|   FDASSERT(iter_out != im_info.end() && iter_ipt != im_info.end(),
 | |
|            "Cannot find input_shape or output_shape from im_info.");
 | |
|   float out_h = iter_out->second[0];
 | |
|   float out_w = iter_out->second[1];
 | |
|   float ipt_h = iter_ipt->second[0];
 | |
|   float ipt_w = iter_ipt->second[1];
 | |
|   float scale = std::min(out_h / ipt_h, out_w / ipt_w);
 | |
|   float pad_h = (out_h - ipt_h * scale) / 2.0f;
 | |
|   float pad_w = (out_w - ipt_w * scale) / 2.0f;
 | |
|   if (is_mini_pad) {
 | |
|     pad_h = static_cast<float>(static_cast<int>(pad_h) % stride);
 | |
|     pad_w = static_cast<float>(static_cast<int>(pad_w) % stride);
 | |
|   }
 | |
|   for (size_t i = 0; i < result->boxes.size(); ++i) {
 | |
|     int32_t label_id = (result->label_ids)[i];
 | |
|     result->boxes[i][0] = std::max((result->boxes[i][0] - pad_w) / scale, 0.0f);
 | |
|     result->boxes[i][1] = std::max((result->boxes[i][1] - pad_h) / scale, 0.0f);
 | |
|     result->boxes[i][2] = std::max((result->boxes[i][2] - pad_w) / scale, 0.0f);
 | |
|     result->boxes[i][3] = std::max((result->boxes[i][3] - pad_h) / scale, 0.0f);
 | |
|     result->boxes[i][0] = std::min(result->boxes[i][0], ipt_w - 1.0f);
 | |
|     result->boxes[i][1] = std::min(result->boxes[i][1], ipt_h - 1.0f);
 | |
|     result->boxes[i][2] = std::min(result->boxes[i][2], ipt_w - 1.0f);
 | |
|     result->boxes[i][3] = std::min(result->boxes[i][3], ipt_h - 1.0f);
 | |
|   }
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| bool YOLOv7End2EndORT::Predict(cv::Mat* im, DetectionResult* result,
 | |
|                                float conf_threshold) {
 | |
|   Mat mat(*im);
 | |
| 
 | |
|   std::map<std::string, std::array<float, 2>> im_info;
 | |
| 
 | |
|   // Record the shape of image and the shape of preprocessed image
 | |
|   im_info["input_shape"] = {static_cast<float>(mat.Height()),
 | |
|                             static_cast<float>(mat.Width())};
 | |
|   im_info["output_shape"] = {static_cast<float>(mat.Height()),
 | |
|                              static_cast<float>(mat.Width())};
 | |
| 
 | |
|   if (!Preprocess(&mat, &reused_input_tensors_[0], &im_info)) {
 | |
|     FDERROR << "Failed to preprocess input image." << std::endl;
 | |
|     return false;
 | |
|   }
 | |
| 
 | |
|   reused_input_tensors_[0].name = InputInfoOfRuntime(0).name;
 | |
|   if (!Infer()) {
 | |
|     FDERROR << "Failed to inference." << std::endl;
 | |
|     return false;
 | |
|   }
 | |
| 
 | |
|   if (!Postprocess(reused_output_tensors_[0], result, im_info, conf_threshold)) {
 | |
|     FDERROR << "Failed to post process." << std::endl;
 | |
|     return false;
 | |
|   }
 | |
| 
 | |
|   return true;
 | |
| }
 | |
| 
 | |
| }  // namespace detection
 | |
| }  // namespace vision
 | |
| }  // namespace fastdeploy
 |