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