transforms.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <iostream>
  23. #include <opencv2/core/core.hpp>
  24. #include <opencv2/highgui/highgui.hpp>
  25. #include <opencv2/imgproc/imgproc.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. std::vector<int> ori_im_size_ = std::vector<int>(2);
  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. std::unique_ptr<paddle::lite_api::Tensor> input_tensor_;
  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. }
  62. virtual bool Run(cv::Mat* im, ImageBlob* data);
  63. private:
  64. std::vector<float> mean_;
  65. std::vector<float> std_;
  66. };
  67. class ResizeByShort : public Transform {
  68. public:
  69. virtual void Init(const YAML::Node& item) {
  70. short_size_ = item["short_size"].as<int>();
  71. if (item["max_size"].IsDefined()) {
  72. max_size_ = item["max_size"].as<int>();
  73. } else {
  74. max_size_ = -1;
  75. }
  76. }
  77. virtual bool Run(cv::Mat* im, ImageBlob* data);
  78. private:
  79. float GenerateScale(const cv::Mat& im);
  80. int short_size_;
  81. int max_size_;
  82. };
  83. /*
  84. * @brief
  85. * This class execute resize by long operation on image matrix. At first, it resizes
  86. * the long side of image matrix to specified length. Accordingly, the short side
  87. * will be resized in the same proportion.
  88. * */
  89. class ResizeByLong : public Transform {
  90. public:
  91. virtual void Init(const YAML::Node& item) {
  92. long_size_ = item["long_size"].as<int>();
  93. }
  94. virtual bool Run(cv::Mat* im, ImageBlob* data);
  95. private:
  96. int long_size_;
  97. };
  98. /*
  99. * @brief
  100. * This class execute resize operation on image matrix. It resizes width and height
  101. * to specified length.
  102. * */
  103. class Resize : public Transform {
  104. public:
  105. virtual void Init(const YAML::Node& item) {
  106. if (item["interp"].IsDefined()) {
  107. interp_ = item["interp"].as<std::string>();
  108. }
  109. if (item["target_size"].IsScalar()) {
  110. height_ = item["target_size"].as<int>();
  111. width_ = item["target_size"].as<int>();
  112. } else if (item["target_size"].IsSequence()) {
  113. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  114. width_ = target_size[0];
  115. height_ = target_size[1];
  116. }
  117. if (height_ <= 0 || width_ <= 0) {
  118. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  119. exit(-1);
  120. }
  121. }
  122. virtual bool Run(cv::Mat* im, ImageBlob* data);
  123. private:
  124. int height_;
  125. int width_;
  126. std::string interp_;
  127. };
  128. class CenterCrop : public Transform {
  129. public:
  130. virtual void Init(const YAML::Node& item) {
  131. if (item["crop_size"].IsScalar()) {
  132. height_ = item["crop_size"].as<int>();
  133. width_ = item["crop_size"].as<int>();
  134. } else if (item["crop_size"].IsSequence()) {
  135. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  136. width_ = crop_size[0];
  137. height_ = crop_size[1];
  138. }
  139. }
  140. virtual bool Run(cv::Mat* im, ImageBlob* data);
  141. private:
  142. int height_;
  143. int width_;
  144. };
  145. /*
  146. * @brief
  147. * This class execute padding operation on image matrix. It makes border on edge
  148. * of image matrix.
  149. * */
  150. class Padding : public Transform {
  151. public:
  152. virtual void Init(const YAML::Node& item) {
  153. if (item["coarsest_stride"].IsDefined()) {
  154. coarsest_stride_ = item["coarsest_stride"].as<int>();
  155. if (coarsest_stride_ < 1) {
  156. std::cerr << "[Padding] coarest_stride should greater than 0"
  157. << std::endl;
  158. exit(-1);
  159. }
  160. }
  161. if (item["target_size"].IsDefined()) {
  162. if (item["target_size"].IsScalar()) {
  163. width_ = item["target_size"].as<int>();
  164. height_ = item["target_size"].as<int>();
  165. } else if (item["target_size"].IsSequence()) {
  166. width_ = item["target_size"].as<std::vector<int>>()[0];
  167. height_ = item["target_size"].as<std::vector<int>>()[1];
  168. }
  169. }
  170. if (item["im_padding_value"].IsDefined()) {
  171. im_value_ = item["im_padding_value"].as<std::vector<float>>();
  172. } else {
  173. im_value_ = {0, 0, 0};
  174. }
  175. }
  176. virtual bool Run(cv::Mat* im, ImageBlob* data);
  177. private:
  178. int coarsest_stride_ = -1;
  179. int width_ = 0;
  180. int height_ = 0;
  181. std::vector<float> im_value_;
  182. };
  183. class Transforms {
  184. public:
  185. void Init(const YAML::Node& node, bool to_rgb = true);
  186. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  187. bool Run(cv::Mat* im, ImageBlob* data);
  188. private:
  189. std::vector<std::shared_ptr<Transform>> transforms_;
  190. bool to_rgb_ = true;
  191. };
  192. } // namespace PaddleX