transforms.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. if (item["min_val"].IsDefined()) {
  77. min_val_ = item["min_val"].as<std::vector<float>>();
  78. } else {
  79. min_val_ = std::vector<float>(mean_.size(), 0.);
  80. }
  81. if (item["max_val"].IsDefined()) {
  82. max_val_ = item["max_val"].as<std::vector<float>>();
  83. } else {
  84. max_val_ = std::vector<float>(mean_.size(), 255.);
  85. }
  86. }
  87. virtual bool Run(cv::Mat* im, ImageBlob* data);
  88. private:
  89. std::vector<float> mean_;
  90. std::vector<float> std_;
  91. std::vector<float> min_val_;
  92. std::vector<float> max_val_;
  93. };
  94. /*
  95. * @brief
  96. * This class execute resize by short operation on image matrix. At first, it resizes
  97. * the short side of image matrix to specified length. Accordingly, the long side
  98. * will be resized in the same proportion. If new length of long side exceeds max
  99. * size, the long size will be resized to max size, and the short size will be
  100. * resized in the same proportion
  101. * */
  102. class ResizeByShort : public Transform {
  103. public:
  104. virtual void Init(const YAML::Node& item) {
  105. short_size_ = item["short_size"].as<int>();
  106. if (item["max_size"].IsDefined()) {
  107. max_size_ = item["max_size"].as<int>();
  108. } else {
  109. max_size_ = -1;
  110. }
  111. }
  112. virtual bool Run(cv::Mat* im, ImageBlob* data);
  113. private:
  114. float GenerateScale(const cv::Mat& im);
  115. int short_size_;
  116. int max_size_;
  117. };
  118. /*
  119. * @brief
  120. * This class execute resize by long operation on image matrix. At first, it resizes
  121. * the long side of image matrix to specified length. Accordingly, the short side
  122. * will be resized in the same proportion.
  123. * */
  124. class ResizeByLong : public Transform {
  125. public:
  126. virtual void Init(const YAML::Node& item) {
  127. long_size_ = item["long_size"].as<int>();
  128. }
  129. virtual bool Run(cv::Mat* im, ImageBlob* data);
  130. private:
  131. int long_size_;
  132. };
  133. /*
  134. * @brief
  135. * This class execute resize operation on image matrix. It resizes width and height
  136. * to specified length.
  137. * */
  138. class Resize : public Transform {
  139. public:
  140. virtual void Init(const YAML::Node& item) {
  141. if (item["interp"].IsDefined()) {
  142. interp_ = item["interp"].as<std::string>();
  143. }
  144. if (item["target_size"].IsScalar()) {
  145. height_ = item["target_size"].as<int>();
  146. width_ = item["target_size"].as<int>();
  147. } else if (item["target_size"].IsSequence()) {
  148. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  149. width_ = target_size[0];
  150. height_ = target_size[1];
  151. }
  152. if (height_ <= 0 || width_ <= 0) {
  153. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  154. exit(-1);
  155. }
  156. }
  157. virtual bool Run(cv::Mat* im, ImageBlob* data);
  158. private:
  159. int height_;
  160. int width_;
  161. std::string interp_;
  162. };
  163. /*
  164. * @brief
  165. * This class execute center crop operation on image matrix. It crops the center
  166. * of image matrix accroding to specified size.
  167. * */
  168. class CenterCrop : public Transform {
  169. public:
  170. virtual void Init(const YAML::Node& item) {
  171. if (item["crop_size"].IsScalar()) {
  172. height_ = item["crop_size"].as<int>();
  173. width_ = item["crop_size"].as<int>();
  174. } else if (item["crop_size"].IsSequence()) {
  175. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  176. width_ = crop_size[0];
  177. height_ = crop_size[1];
  178. }
  179. }
  180. virtual bool Run(cv::Mat* im, ImageBlob* data);
  181. private:
  182. int height_;
  183. int width_;
  184. };
  185. /*
  186. * @brief
  187. * This class execute padding operation on image matrix. It makes border on edge
  188. * of image matrix.
  189. * */
  190. class Padding : public Transform {
  191. public:
  192. virtual void Init(const YAML::Node& item) {
  193. if (item["coarsest_stride"].IsDefined()) {
  194. coarsest_stride_ = item["coarsest_stride"].as<int>();
  195. if (coarsest_stride_ < 1) {
  196. std::cerr << "[Padding] coarest_stride should greater than 0"
  197. << std::endl;
  198. exit(-1);
  199. }
  200. }
  201. if (item["target_size"].IsDefined()) {
  202. if (item["target_size"].IsScalar()) {
  203. width_ = item["target_size"].as<int>();
  204. height_ = item["target_size"].as<int>();
  205. } else if (item["target_size"].IsSequence()) {
  206. width_ = item["target_size"].as<std::vector<int>>()[0];
  207. height_ = item["target_size"].as<std::vector<int>>()[1];
  208. }
  209. }
  210. if (item["im_padding_value"].IsDefined()) {
  211. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  212. } else {
  213. im_value_ = {0, 0, 0};
  214. }
  215. }
  216. virtual bool Run(cv::Mat* im, ImageBlob* data);
  217. private:
  218. int coarsest_stride_ = -1;
  219. int width_ = 0;
  220. int height_ = 0;
  221. std::vector<float> im_value_;
  222. };
  223. /*
  224. * @brief
  225. * This class execute clip operation on image matrix
  226. * */
  227. class Clip : public Transform {
  228. public:
  229. virtual void Init(const YAML::Node& item) {
  230. min_val_ = item["min_val"].as<std::vector<float>>();
  231. max_val_ = item["max_val"].as<std::vector<float>>();
  232. }
  233. virtual bool Run(cv::Mat* im, ImageBlob* data);
  234. private:
  235. std::vector<float> min_val_;
  236. std::vector<float> max_val_;
  237. };
  238. /*
  239. * @brief
  240. * This class is transform operations manager. It stores all neccessary
  241. * transform operations and run them in correct order.
  242. * */
  243. class Transforms {
  244. public:
  245. void Init(const YAML::Node& node, bool to_rgb = true);
  246. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  247. bool Run(cv::Mat* im, ImageBlob* data);
  248. private:
  249. std::vector<std::shared_ptr<Transform>> transforms_;
  250. bool to_rgb_ = true;
  251. };
  252. } // namespace PaddleX