transforms.h 7.2 KB

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