hwc2chw.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/vision/common/processors/hwc2chw.h"
  15. #include "ultra_infer/function/transpose.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. bool HWC2CHW::ImplByOpenCV(Mat *mat) {
  19. if (mat->layout != Layout::HWC) {
  20. FDERROR << "HWC2CHW: The input data is not Layout::HWC format!"
  21. << std::endl;
  22. return false;
  23. }
  24. cv::Mat *im = mat->GetOpenCVMat();
  25. cv::Mat im_clone = im->clone();
  26. int rh = im->rows;
  27. int rw = im->cols;
  28. int rc = im->channels();
  29. for (int i = 0; i < rc; ++i) {
  30. cv::extractChannel(
  31. im_clone,
  32. cv::Mat(rh, rw, im->type() % 8,
  33. im->ptr() + i * rh * rw * FDDataTypeSize(mat->Type())),
  34. i);
  35. }
  36. mat->layout = Layout::CHW;
  37. return true;
  38. }
  39. #ifdef ENABLE_FLYCV
  40. bool HWC2CHW::ImplByFlyCV(Mat *mat) {
  41. if (mat->layout != Layout::HWC) {
  42. FDERROR << "HWC2CHW: The input data is not Layout::HWC format!"
  43. << std::endl;
  44. return false;
  45. }
  46. if (mat->Type() != FDDataType::FP32) {
  47. FDERROR << "HWC2CHW: Only support float data while use FlyCV, but now it's "
  48. << mat->Type() << "." << std::endl;
  49. return false;
  50. }
  51. fcv::Mat *im = mat->GetFlyCVMat();
  52. fcv::Mat new_im;
  53. fcv::normalize_to_submean_to_reorder(*im, {0.0, 0.0, 0.0}, {1.0, 1.0, 1.0},
  54. std::vector<uint32_t>(), new_im, false);
  55. mat->SetMat(new_im);
  56. mat->layout = Layout::CHW;
  57. return true;
  58. }
  59. #endif
  60. #ifdef ENABLE_CVCUDA
  61. bool HWC2CHW::ImplByCvCuda(FDMat *mat) {
  62. // Prepare input tensor
  63. FDTensor *src = CreateCachedGpuInputTensor(mat);
  64. auto src_tensor = CreateCvCudaTensorWrapData(*src);
  65. // Prepare output tensor
  66. mat->output_cache->Resize({mat->Channels(), mat->Height(), mat->Width()},
  67. src->Dtype(), "output_cache", Device::GPU);
  68. auto dst_tensor =
  69. CreateCvCudaTensorWrapData(*(mat->output_cache), Layout::CHW);
  70. cvcuda_reformat_op_(mat->Stream(), *src_tensor, *dst_tensor);
  71. mat->layout = Layout::CHW;
  72. mat->SetTensor(mat->output_cache);
  73. mat->mat_type = ProcLib::CVCUDA;
  74. return true;
  75. }
  76. #endif
  77. bool HWC2CHW::Run(Mat *mat, ProcLib lib) {
  78. auto h = HWC2CHW();
  79. return h(mat, lib);
  80. }
  81. } // namespace vision
  82. } // namespace ultra_infer