transforms.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <opencv2/core/core.hpp>
  22. #include <opencv2/highgui/highgui.hpp>
  23. #include <opencv2/imgproc/imgproc.hpp>
  24. namespace PaddleX {
  25. // Object for storing all preprocessed data
  26. class ImageBlob {
  27. public:
  28. // Original image height and width
  29. std::vector<int> ori_im_size_ = std::vector<int>(2);
  30. // Newest image height and width after process
  31. std::vector<int> new_im_size_ = std::vector<int>(2);
  32. // Image height and width before resize
  33. std::vector<std::vector<int>> im_size_before_resize_;
  34. // Reshape order
  35. std::vector<std::string> reshape_order_;
  36. // Resize scale
  37. float scale = 1.0;
  38. // Buffer for image data after preprocessing
  39. std::vector<float> im_data_;
  40. void clear() {
  41. ori_im_size_.clear();
  42. new_im_size_.clear();
  43. im_size_before_resize_.clear();
  44. reshape_order_.clear();
  45. im_data_.clear();
  46. }
  47. };
  48. // Abstraction of preprocessing opration class
  49. class Transform {
  50. public:
  51. virtual void Init(const YAML::Node& item) = 0;
  52. virtual bool Run(cv::Mat* im, ImageBlob* data) = 0;
  53. };
  54. class Normalize : public Transform {
  55. public:
  56. virtual void Init(const YAML::Node& item) {
  57. mean_ = item["mean"].as<std::vector<float>>();
  58. std_ = item["std"].as<std::vector<float>>();
  59. }
  60. virtual bool Run(cv::Mat* im, ImageBlob* data);
  61. private:
  62. std::vector<float> mean_;
  63. std::vector<float> std_;
  64. };
  65. class ResizeByShort : public Transform {
  66. public:
  67. virtual void Init(const YAML::Node& item) {
  68. short_size_ = item["short_size"].as<int>();
  69. if (item["max_size"].IsDefined()) {
  70. max_size_ = item["max_size"].as<int>();
  71. } else {
  72. max_size_ = -1;
  73. }
  74. };
  75. virtual bool Run(cv::Mat* im, ImageBlob* data);
  76. private:
  77. float GenerateScale(const cv::Mat& im);
  78. int short_size_;
  79. int max_size_;
  80. };
  81. class ResizeByLong : public Transform {
  82. public:
  83. virtual void Init(const YAML::Node& item) {
  84. long_size_ = item["long_size"].as<int>();
  85. };
  86. virtual bool Run(cv::Mat* im, ImageBlob* data);
  87. private:
  88. int long_size_;
  89. };
  90. class Resize : public Transform {
  91. public:
  92. virtual void Init(const YAML::Node& item) {
  93. if (item["target_size"].IsScalar()) {
  94. height_ = item["target_size"].as<int>();
  95. width_ = item["target_size"].as<int>();
  96. interp_ = item["interp"].as<std::string>();
  97. } else if (item["target_size"].IsSequence()) {
  98. std::vector<int> target_size = item["target_size"].as<std::vector<int>>();
  99. width_ = target_size[0];
  100. height_ = target_size[1];
  101. }
  102. if (height_ <= 0 || width_ <= 0) {
  103. std::cerr << "[Resize] target_size should greater than 0" << std::endl;
  104. exit(-1);
  105. }
  106. }
  107. virtual bool Run(cv::Mat* im, ImageBlob* data);
  108. private:
  109. int height_;
  110. int width_;
  111. std::string interp_;
  112. };
  113. class CenterCrop : public Transform {
  114. public:
  115. virtual void Init(const YAML::Node& item) {
  116. if (item["crop_size"].IsScalar()) {
  117. height_ = item["crop_size"].as<int>();
  118. width_ = item["crop_size"].as<int>();
  119. } else if (item["crop_size"].IsSequence()) {
  120. std::vector<int> crop_size = item["crop_size"].as<std::vector<int>>();
  121. width_ = crop_size[0];
  122. height_ = crop_size[1];
  123. }
  124. }
  125. virtual bool Run(cv::Mat* im, ImageBlob* data);
  126. private:
  127. int height_;
  128. int width_;
  129. };
  130. class Padding : public Transform {
  131. public:
  132. virtual void Init(const YAML::Node& item) {
  133. if (item["coarsest_stride"].IsDefined()) {
  134. coarsest_stride_ = item["coarsest_stride"].as<int>();
  135. if (coarsest_stride_ <= 1) {
  136. std::cerr << "[Padding] coarest_stride should greater than 0"
  137. << std::endl;
  138. exit(-1);
  139. }
  140. } else {
  141. if (item["target_size"].IsScalar()) {
  142. width_ = item["target_size"].as<int>();
  143. height_ = item["target_size"].as<int>();
  144. } else if (item["target_size"].IsSequence()) {
  145. width_ = item["target_size"].as<std::vector<int>>()[1];
  146. height_ = item["target_size"].as<std::vector<int>>()[0];
  147. }
  148. }
  149. if (item["im_padding_value"].IsDefined()) {
  150. value_ = item["im_padding_value"].as<std::vector<float>>();
  151. }
  152. }
  153. virtual bool Run(cv::Mat* im, ImageBlob* data);
  154. private:
  155. int coarsest_stride_ = -1;
  156. int width_ = 0;
  157. int height_ = 0;
  158. std::vector<float> value_;
  159. };
  160. class Transforms {
  161. public:
  162. void Init(const YAML::Node& node, bool to_rgb = true);
  163. std::shared_ptr<Transform> CreateTransform(const std::string& name);
  164. bool Run(cv::Mat* im, ImageBlob* data);
  165. private:
  166. std::vector<std::shared_ptr<Transform>> transforms_;
  167. bool to_rgb_ = true;
  168. };
  169. } // namespace PaddleX