transforms.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /*
  26. * @brief
  27. * This class represents object for storing all preprocessed data
  28. * */
  29. class ImageBlob {
  30. public:
  31. // Original image height and width
  32. std::vector<int> ori_im_size_ = std::vector<int>(2);
  33. // Newest image height and width after process
  34. std::vector<int> new_im_size_ = std::vector<int>(2);
  35. // Image height and width before resize
  36. std::vector<std::vector<int>> im_size_before_resize_;
  37. // Reshape order
  38. std::vector<std::string> reshape_order_;
  39. // Resize scale
  40. float scale = 1.0;
  41. // Buffer for image data after preprocessing
  42. std::vector<float> im_data_;
  43. void clear() {
  44. im_size_before_resize_.clear();
  45. reshape_order_.clear();
  46. im_data_.clear();
  47. }
  48. };
  49. /*
  50. * @brief
  51. * Abstraction of preprocessing operation class
  52. * */
  53. class Transform {
  54. public:
  55. virtual void Init(const YAML::Node& item) = 0;
  56. /*
  57. * @brief
  58. * This method executes preprocessing operation on image matrix,
  59. * result will be returned at second parameter.
  60. * @param im: single image matrix to be preprocessed
  61. * @param data: the raw data of single image matrix after preprocessed
  62. * @return true if transform successfully
  63. * */
  64. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  65. };
  66. /*
  67. * @brief
  68. * This class execute normalization operation on image matrix
  69. * */
  70. class Normalize : public Transform {
  71. public:
  72. virtual void Init(const YAML::Node& item) {
  73. mean_ = item["mean"].as<std::vector<float>>();
  74. std_ = item["std"].as<std::vector<float>>();
  75. }
  76. virtual bool Run(cv::Mat* im, ImageBlob* data);
  77. private:
  78. std::vector<float> mean_;
  79. std::vector<float> std_;
  80. };
  81. /*
  82. * @brief
  83. * This class execute resize by short operation on image matrix. At first, it resizes
  84. * the short side of image matrix to specified length. Accordingly, the long side
  85. * will be resized in the same proportion. If new length of long side exceeds max
  86. * size, the long size will be resized to max size, and the short size will be
  87. * resized in the same proportion
  88. * */
  89. class ResizeByShort : public Transform {
  90. public:
  91. virtual void Init(const YAML::Node& item) {
  92. short_size_ = item["short_size"].as<int>();
  93. if (item["max_size"].IsDefined()) {
  94. max_size_ = item["max_size"].as<int>();
  95. } else {
  96. max_size_ = -1;
  97. }
  98. }
  99. virtual bool Run(cv::Mat* im, ImageBlob* data);
  100. private:
  101. float GenerateScale(const cv::Mat& im);
  102. int short_size_;
  103. int max_size_;
  104. };
  105. /*
  106. * @brief
  107. * This class execute resize by long operation on image matrix. At first, it resizes
  108. * the long side of image matrix to specified length. Accordingly, the short side
  109. * will be resized in the same proportion.
  110. * */
  111. class ResizeByLong : public Transform {
  112. public:
  113. virtual void Init(const YAML::Node& item) {
  114. long_size_ = item["long_size"].as<int>();
  115. }
  116. virtual bool Run(cv::Mat* im, ImageBlob* data);
  117. private:
  118. int long_size_;
  119. };
  120. /*
  121. * @brief
  122. * This class execute resize operation on image matrix. It resizes width and height
  123. * to specified length.
  124. * */
  125. class Resize : public Transform {
  126. public:
  127. virtual void Init(const YAML::Node& item) {
  128. if (item["interp"].IsDefined()) {
  129. interp_ = item["interp"].as<std::string>();
  130. }
  131. if (item["target_size"].IsScalar()) {
  132. height_ = item["target_size"].as<int>();
  133. width_ = item["target_size"].as<int>();
  134. } else if (item["target_size"].IsSequence()) {
  135. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  136. width_ = target_size[0];
  137. height_ = target_size[1];
  138. }
  139. if (height_ <= 0 || width_ <= 0) {
  140. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  141. exit(-1);
  142. }
  143. }
  144. virtual bool Run(cv::Mat* im, ImageBlob* data);
  145. private:
  146. int height_;
  147. int width_;
  148. std::string interp_;
  149. };
  150. /*
  151. * @brief
  152. * This class execute center crop operation on image matrix. It crops the center
  153. * of image matrix accroding to specified size.
  154. * */
  155. class CenterCrop : public Transform {
  156. public:
  157. virtual void Init(const YAML::Node& item) {
  158. if (item["crop_size"].IsScalar()) {
  159. height_ = item["crop_size"].as<int>();
  160. width_ = item["crop_size"].as<int>();
  161. } else if (item["crop_size"].IsSequence()) {
  162. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  163. width_ = crop_size[0];
  164. height_ = crop_size[1];
  165. }
  166. }
  167. virtual bool Run(cv::Mat* im, ImageBlob* data);
  168. private:
  169. int height_;
  170. int width_;
  171. };
  172. /*
  173. * @brief
  174. * This class execute padding operation on image matrix. It makes border on edge
  175. * of image matrix.
  176. * */
  177. class Padding : public Transform {
  178. public:
  179. virtual void Init(const YAML::Node& item) {
  180. if (item["coarsest_stride"].IsDefined()) {
  181. coarsest_stride_ = item["coarsest_stride"].as<int>();
  182. if (coarsest_stride_ < 1) {
  183. std::cerr << "[Padding] coarest_stride should greater than 0"
  184. << std::endl;
  185. exit(-1);
  186. }
  187. }
  188. if (item["target_size"].IsDefined()) {
  189. if (item["target_size"].IsScalar()) {
  190. width_ = item["target_size"].as<int>();
  191. height_ = item["target_size"].as<int>();
  192. } else if (item["target_size"].IsSequence()) {
  193. width_ = item["target_size"].as<std::vector<int>>()[0];
  194. height_ = item["target_size"].as<std::vector<int>>()[1];
  195. }
  196. }
  197. if (item["im_padding_value"].IsDefined()) {
  198. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  199. }
  200. else {
  201. im_value_ = {0, 0, 0};
  202. }
  203. }
  204. virtual bool Run(cv::Mat* im, ImageBlob* data);
  205. private:
  206. int coarsest_stride_ = -1;
  207. int width_ = 0;
  208. int height_ = 0;
  209. std::vector<float> im_value_;
  210. };
  211. /*
  212. * @brief
  213. * This class is transform operations manager. It stores all neccessary
  214. * transform operations and run them in correct order.
  215. * */
  216. class Transforms {
  217. public:
  218. void Init(const YAML::Node& node, bool to_rgb = true);
  219. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  220. bool Run(cv::Mat* im, ImageBlob* data);
  221. private:
  222. std::vector<std::shared_ptr<Transform>> transforms_;
  223. bool to_rgb_ = true;
  224. };
  225. } // namespace PaddleX