transforms.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <paddle_api.h>
  17. #include <memory>
  18. #include <string>
  19. #include <unordered_map>
  20. #include <utility>
  21. #include <vector>
  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::unique_ptr<paddle::lite_api::Tensor> input_tensor_;
  44. void clear() {
  45. im_size_before_resize_.clear();
  46. reshape_order_.clear();
  47. }
  48. };
  49. // Abstraction of preprocessing opration class
  50. class Transform {
  51. public:
  52. virtual void Init(const YAML::Node& item) = 0;
  53. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  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. /*
  83. * @brief
  84. * This class execute resize by long operation on image matrix. At first, it resizes
  85. * the long side of image matrix to specified length. Accordingly, the short side
  86. * will be resized in the same proportion.
  87. * */
  88. class ResizeByLong : public Transform {
  89. public:
  90. virtual void Init(const YAML::Node& item) {
  91. long_size_ = item["long_size"].as<int>();
  92. }
  93. virtual bool Run(cv::Mat* im, ImageBlob* data);
  94. private:
  95. int long_size_;
  96. };
  97. /*
  98. * @brief
  99. * This class execute resize operation on image matrix. It resizes width and height
  100. * to specified length.
  101. * */
  102. class Resize : public Transform {
  103. public:
  104. virtual void Init(const YAML::Node& item) {
  105. if (item["interp"].IsDefined()) {
  106. interp_ = item["interp"].as<std::string>();
  107. }
  108. if (item["target_size"].IsScalar()) {
  109. height_ = item["target_size"].as<int>();
  110. width_ = item["target_size"].as<int>();
  111. } else if (item["target_size"].IsSequence()) {
  112. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  113. width_ = target_size[0];
  114. height_ = target_size[1];
  115. }
  116. if (height_ <= 0 || width_ <= 0) {
  117. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  118. exit(-1);
  119. }
  120. }
  121. virtual bool Run(cv::Mat* im, ImageBlob* data);
  122. private:
  123. int height_;
  124. int width_;
  125. std::string interp_;
  126. };
  127. class CenterCrop : public Transform {
  128. public:
  129. virtual void Init(const YAML::Node& item) {
  130. if (item["crop_size"].IsScalar()) {
  131. height_ = item["crop_size"].as<int>();
  132. width_ = item["crop_size"].as<int>();
  133. } else if (item["crop_size"].IsSequence()) {
  134. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  135. width_ = crop_size[0];
  136. height_ = crop_size[1];
  137. }
  138. }
  139. virtual bool Run(cv::Mat* im, ImageBlob* data);
  140. private:
  141. int height_;
  142. int width_;
  143. };
  144. /*
  145. * @brief
  146. * This class execute padding operation on image matrix. It makes border on edge
  147. * of image matrix.
  148. * */
  149. class Padding : public Transform {
  150. public:
  151. virtual void Init(const YAML::Node& item) {
  152. if (item["coarsest_stride"].IsDefined()) {
  153. coarsest_stride_ = item["coarsest_stride"].as<int>();
  154. if (coarsest_stride_ < 1) {
  155. std::cerr << "[Padding] coarest_stride should greater than 0"
  156. << std::endl;
  157. exit(-1);
  158. }
  159. }
  160. if (item["target_size"].IsDefined()) {
  161. if (item["target_size"].IsScalar()) {
  162. width_ = item["target_size"].as<int>();
  163. height_ = item["target_size"].as<int>();
  164. } else if (item["target_size"].IsSequence()) {
  165. width_ = item["target_size"].as<std::vector<int>>()[0];
  166. height_ = item["target_size"].as<std::vector<int>>()[1];
  167. }
  168. }
  169. if (item["im_padding_value"].IsDefined()) {
  170. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  171. } else {
  172. im_value_ = {0, 0, 0};
  173. }
  174. }
  175. virtual bool Run(cv::Mat* im, ImageBlob* data);
  176. private:
  177. int coarsest_stride_ = -1;
  178. int width_ = 0;
  179. int height_ = 0;
  180. std::vector<float> im_value_;
  181. };
  182. class Transforms {
  183. public:
  184. void Init(const YAML::Node& node, bool to_rgb = true);
  185. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  186. bool Run(cv::Mat* im, ImageBlob* data);
  187. private:
  188. std::vector<std::shared_ptr<Transform>> transforms_;
  189. bool to_rgb_ = true;
  190. };
  191. } // namespace PaddleX