preprocessor.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/generation/contrib/preprocessor.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. namespace generation {
  18. bool AnimeGANPreprocessor::Run(std::vector<Mat> &images,
  19. std::vector<FDTensor> *outputs) {
  20. // 1. BGR2RGB
  21. // 2. Convert(opencv style) or Normalize
  22. for (size_t i = 0; i < images.size(); ++i) {
  23. auto ret = BGR2RGB::Run(&images[i]);
  24. if (!ret) {
  25. FDERROR << "Failed to process image:" << i << " in "
  26. << "BGR2RGB"
  27. << "." << std::endl;
  28. return false;
  29. }
  30. ret = Cast::Run(&images[i], "float");
  31. if (!ret) {
  32. FDERROR << "Failed to process image:" << i << " in "
  33. << "Cast"
  34. << "." << std::endl;
  35. return false;
  36. }
  37. std::vector<float> mean{1.f / 127.5f, 1.f / 127.5f, 1.f / 127.5f};
  38. std::vector<float> std{-1.f, -1.f, -1.f};
  39. ret = Convert::Run(&images[i], mean, std);
  40. if (!ret) {
  41. FDERROR << "Failed to process image:" << i << " in "
  42. << "Cast"
  43. << "." << std::endl;
  44. return false;
  45. }
  46. }
  47. outputs->resize(1);
  48. // Concat all the preprocessed data to a batch tensor
  49. std::vector<FDTensor> tensors(images.size());
  50. for (size_t i = 0; i < images.size(); ++i) {
  51. images[i].ShareWithTensor(&(tensors[i]));
  52. tensors[i].ExpandDim(0);
  53. }
  54. if (tensors.size() == 1) {
  55. (*outputs)[0] = std::move(tensors[0]);
  56. } else {
  57. function::Concat(tensors, &((*outputs)[0]), 0);
  58. }
  59. return true;
  60. }
  61. } // namespace generation
  62. } // namespace vision
  63. } // namespace ultra_infer