cast.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/cast.h"
  15. #include "ultra_infer/vision/common/processors/utils.h"
  16. namespace ultra_infer {
  17. namespace vision {
  18. bool Cast::ImplByOpenCV(Mat *mat) {
  19. cv::Mat *im = mat->GetOpenCVMat();
  20. int c = im->channels();
  21. if (dtype_ == "float") {
  22. if (im->type() != CV_32FC(c)) {
  23. im->convertTo(*im, CV_32FC(c));
  24. }
  25. } else if (dtype_ == "double") {
  26. if (im->type() != CV_64FC(c)) {
  27. im->convertTo(*im, CV_64FC(c));
  28. }
  29. } else {
  30. FDWARNING << "Cast not support for " << dtype_
  31. << " now! will skip this operation." << std::endl;
  32. }
  33. return true;
  34. }
  35. #ifdef ENABLE_FLYCV
  36. bool Cast::ImplByFlyCV(Mat *mat) {
  37. fcv::Mat *im = mat->GetFlyCVMat();
  38. if (dtype_ == "float" && mat->Type() == FDDataType::FP32) {
  39. return true;
  40. }
  41. if (dtype_ == "double" && mat->Type() == FDDataType::FP64) {
  42. return true;
  43. }
  44. if (mat->layout != Layout::HWC) {
  45. FDERROR
  46. << "While using FlyCV to cast image, the image must be layout of HWC."
  47. << std::endl;
  48. return false;
  49. }
  50. if (dtype_ == "float") {
  51. fcv::Mat new_im;
  52. auto fcv_type = CreateFlyCVDataType(FDDataType::FP32, im->channels());
  53. im->convert_to(new_im, fcv_type);
  54. mat->SetMat(new_im);
  55. } else if (dtype_ == "double") {
  56. fcv::Mat new_im;
  57. auto fcv_type = CreateFlyCVDataType(FDDataType::FP64, im->channels());
  58. im->convert_to(new_im, fcv_type);
  59. mat->SetMat(new_im);
  60. } else {
  61. FDWARNING << "Cast not support for " << dtype_
  62. << " now! will skip this operation." << std::endl;
  63. }
  64. return true;
  65. }
  66. #endif
  67. #ifdef ENABLE_CVCUDA
  68. bool Cast::ImplByCvCuda(FDMat *mat) {
  69. FDDataType dst_dtype;
  70. if (dtype_ == "float") {
  71. dst_dtype = FDDataType::FP32;
  72. } else if (dtype_ == "double") {
  73. dst_dtype = FDDataType::FP64;
  74. } else {
  75. FDWARNING << "Cast not support for " << dtype_
  76. << " now! will skip this operation." << std::endl;
  77. return false;
  78. }
  79. if (mat->Type() == dst_dtype) {
  80. return true;
  81. }
  82. // Prepare input tensor
  83. FDTensor *src = CreateCachedGpuInputTensor(mat);
  84. auto src_tensor = CreateCvCudaTensorWrapData(*src);
  85. // Prepare output tensor
  86. mat->output_cache->Resize(src->Shape(), dst_dtype, "output_cache",
  87. Device::GPU);
  88. auto dst_tensor =
  89. CreateCvCudaTensorWrapData(*(mat->output_cache), mat->layout);
  90. cvcuda_convert_op_(mat->Stream(), *src_tensor, *dst_tensor, 1.0f, 0.0f);
  91. mat->SetTensor(mat->output_cache);
  92. mat->mat_type = ProcLib::CVCUDA;
  93. return true;
  94. }
  95. #endif
  96. bool Cast::Run(Mat *mat, const std::string &dtype, ProcLib lib) {
  97. auto c = Cast(dtype);
  98. return c(mat, lib);
  99. }
  100. } // namespace vision
  101. } // namespace ultra_infer