transforms.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2020 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. #pragma once
  15. #include <yaml-cpp/yaml.h>
  16. #include <memory>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <utility>
  20. #include <vector>
  21. #include <opencv2/core/core.hpp>
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. namespace PaddleX {
  25. // Object for storing all preprocessed data
  26. class ImageBlob {
  27. public:
  28. // Original image height and width
  29. std::vector<int> ori_im_size_ = std::vector<int>(2);
  30. // Newest image height and width after process
  31. std::vector<int> new_im_size_ = std::vector<int>(2);
  32. // Image height and width before resize
  33. std::vector<std::vector<int>> im_size_before_resize_;
  34. // Reshape order
  35. std::vector<std::string> reshape_order_;
  36. // Resize scale
  37. float scale = 1.0;
  38. // Buffer for image data after preprocessing
  39. std::vector<float> im_data_;
  40. void clear() {
  41. ori_im_size_.clear();
  42. new_im_size_.clear();
  43. im_size_before_resize_.clear();
  44. reshape_order_.clear();
  45. im_data_.clear();
  46. }
  47. };
  48. // Abstraction of preprocessing opration class
  49. class Transform {
  50. public:
  51. virtual void Init(const YAML::Node& item) = 0;
  52. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  53. virtual void SetPaddingSize(int max_h, int max_w) {}
  54. };
  55. class Normalize : public Transform {
  56. public:
  57. virtual void Init(const YAML::Node& item) {
  58. mean_ = item["mean"].as<std::vector<float>>();
  59. std_ = item["std"].as<std::vector<float>>();
  60. }
  61. virtual bool Run(cv::Mat* im, ImageBlob* data);
  62. private:
  63. std::vector<float> mean_;
  64. std::vector<float> std_;
  65. };
  66. class ResizeByShort : public Transform {
  67. public:
  68. virtual void Init(const YAML::Node& item) {
  69. short_size_ = item["short_size"].as<int>();
  70. if (item["max_size"].IsDefined()) {
  71. max_size_ = item["max_size"].as<int>();
  72. } else {
  73. max_size_ = -1;
  74. }
  75. }
  76. virtual bool Run(cv::Mat* im, ImageBlob* data);
  77. private:
  78. float GenerateScale(const cv::Mat& im);
  79. int short_size_;
  80. int max_size_;
  81. };
  82. class ResizeByLong : public Transform {
  83. public:
  84. virtual void Init(const YAML::Node& item) {
  85. long_size_ = item["long_size"].as<int>();
  86. }
  87. virtual bool Run(cv::Mat* im, ImageBlob* data);
  88. private:
  89. int long_size_;
  90. };
  91. class Resize : public Transform {
  92. public:
  93. virtual void Init(const YAML::Node& item) {
  94. if (item["target_size"].IsScalar()) {
  95. height_ = item["target_size"].as<int>();
  96. width_ = item["target_size"].as<int>();
  97. interp_ = item["interp"].as<std::string>();
  98. } else if (item["target_size"].IsSequence()) {
  99. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  100. width_ = target_size[0];
  101. height_ = target_size[1];
  102. }
  103. if (height_ <= 0 || width_ <= 0) {
  104. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  105. exit(-1);
  106. }
  107. }
  108. virtual bool Run(cv::Mat* im, ImageBlob* data);
  109. private:
  110. int height_;
  111. int width_;
  112. std::string interp_;
  113. };
  114. class CenterCrop : public Transform {
  115. public:
  116. virtual void Init(const YAML::Node& item) {
  117. if (item["crop_size"].IsScalar()) {
  118. height_ = item["crop_size"].as<int>();
  119. width_ = item["crop_size"].as<int>();
  120. } else if (item["crop_size"].IsSequence()) {
  121. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  122. width_ = crop_size[0];
  123. height_ = crop_size[1];
  124. }
  125. }
  126. virtual bool Run(cv::Mat* im, ImageBlob* data);
  127. private:
  128. int height_;
  129. int width_;
  130. };
  131. class Padding : public Transform {
  132. public:
  133. virtual void Init(const YAML::Node& item) {
  134. if (item["coarsest_stride"].IsDefined()) {
  135. coarsest_stride_ = item["coarsest_stride"].as<int>();
  136. if (coarsest_stride_ < 1) {
  137. std::cerr << "[Padding] coarest_stride should greater than 0"
  138. << std::endl;
  139. exit(-1);
  140. }
  141. }
  142. if (item["target_size"].IsDefined()) {
  143. if (item["target_size"].IsScalar()) {
  144. width_ = item["target_size"].as<int>();
  145. height_ = item["target_size"].as<int>();
  146. } else if (item["target_size"].IsSequence()) {
  147. width_ = item["target_size"].as<std::vector<int>>()[0];
  148. height_ = item["target_size"].as<std::vector<int>>()[1];
  149. }
  150. }
  151. }
  152. virtual bool Run(cv::Mat* im, ImageBlob* data);
  153. virtual void SetPaddingSize(int max_h, int max_w);
  154. private:
  155. int coarsest_stride_ = -1;
  156. int width_ = 0;
  157. int height_ = 0;
  158. int max_height_ = 0;
  159. int max_width_ = 0;
  160. };
  161. class Transforms {
  162. public:
  163. void Init(const YAML::Node& node, bool to_rgb = true);
  164. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  165. bool Run(cv::Mat* im, ImageBlob* data);
  166. void SetPaddingSize(int max_h, int max_w);
  167. private:
  168. std::vector<std::shared_ptr<Transform>> transforms_;
  169. bool to_rgb_ = true;
  170. int max_h_ = 0;
  171. int max_w_ = 0;
  172. };
  173. } // namespace PaddleX