transforms.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <iostream>
  15. #include <string>
  16. #include <vector>
  17. #include <math.h>
  18. #include "include/paddlex/transforms.h"
  19. namespace PaddleX {
  20. std::map<std::string, int> interpolations = {{"LINEAR", cv::INTER_LINEAR},
  21. {"NEAREST", cv::INTER_NEAREST},
  22. {"AREA", cv::INTER_AREA},
  23. {"CUBIC", cv::INTER_CUBIC},
  24. {"LANCZOS4", cv::INTER_LANCZOS4}};
  25. bool Normalize::Run(cv::Mat* im, ImageBlob* data) {
  26. for (int h = 0; h < im->rows; h++) {
  27. for (int w = 0; w < im->cols; w++) {
  28. im->at<cv::Vec3f>(h, w)[0] =
  29. (im->at<cv::Vec3f>(h, w)[0] / 255.0 - mean_[0]) / std_[0];
  30. im->at<cv::Vec3f>(h, w)[1] =
  31. (im->at<cv::Vec3f>(h, w)[1] / 255.0 - mean_[1]) / std_[1];
  32. im->at<cv::Vec3f>(h, w)[2] =
  33. (im->at<cv::Vec3f>(h, w)[2] / 255.0 - mean_[2]) / std_[2];
  34. }
  35. }
  36. return true;
  37. }
  38. float ResizeByShort::GenerateScale(const cv::Mat& im) {
  39. int origin_w = im.cols;
  40. int origin_h = im.rows;
  41. int im_size_max = std::max(origin_w, origin_h);
  42. int im_size_min = std::min(origin_w, origin_h);
  43. float scale =
  44. static_cast<float>(short_size_) / static_cast<float>(im_size_min);
  45. if (max_size_ > 0) {
  46. if (round(scale * im_size_max) > max_size_) {
  47. scale = static_cast<float>(max_size_) / static_cast<float>(im_size_max);
  48. }
  49. }
  50. return scale;
  51. }
  52. bool ResizeByShort::Run(cv::Mat* im, ImageBlob* data) {
  53. data->im_size_before_resize_.push_back({im->rows, im->cols});
  54. data->reshape_order_.push_back("resize");
  55. float scale = GenerateScale(*im);
  56. int width = static_cast<int>(round(scale * im->cols));
  57. int height = static_cast<int>(round(scale * im->rows));
  58. cv::resize(*im, *im, cv::Size(width, height), 0, 0, cv::INTER_LINEAR);
  59. data->new_im_size_[0] = im->rows;
  60. data->new_im_size_[1] = im->cols;
  61. data->scale = scale;
  62. return true;
  63. }
  64. bool CenterCrop::Run(cv::Mat* im, ImageBlob* data) {
  65. int height = static_cast<int>(im->rows);
  66. int width = static_cast<int>(im->cols);
  67. if (height < height_ || width < width_) {
  68. std::cerr << "[CenterCrop] Image size less than crop size" << std::endl;
  69. return false;
  70. }
  71. int offset_x = static_cast<int>((width - width_) / 2);
  72. int offset_y = static_cast<int>((height - height_) / 2);
  73. cv::Rect crop_roi(offset_x, offset_y, width_, height_);
  74. *im = (*im)(crop_roi);
  75. data->new_im_size_[0] = im->rows;
  76. data->new_im_size_[1] = im->cols;
  77. return true;
  78. }
  79. bool Padding::Run(cv::Mat* im, ImageBlob* data) {
  80. data->im_size_before_resize_.push_back({im->rows, im->cols});
  81. data->reshape_order_.push_back("padding");
  82. int padding_w = 0;
  83. int padding_h = 0;
  84. if (width_ > 1 & height_ > 1) {
  85. padding_w = width_ - im->cols;
  86. padding_h = height_ - im->rows;
  87. } else if (coarsest_stride_ >= 1) {
  88. int h = im->rows;
  89. int w = im->cols;
  90. padding_h =
  91. ceil(h * 1.0 / coarsest_stride_) * coarsest_stride_ - im->rows;
  92. padding_w =
  93. ceil(w * 1.0 / coarsest_stride_) * coarsest_stride_ - im->cols;
  94. }
  95. if (padding_h < 0 || padding_w < 0) {
  96. std::cerr << "[Padding] Computed padding_h=" << padding_h
  97. << ", padding_w=" << padding_w
  98. << ", but they should be greater than 0." << std::endl;
  99. return false;
  100. }
  101. cv::Scalar value = cv::Scalar(im_value_[0], im_value_[1], im_value_[2]);
  102. cv::copyMakeBorder(
  103. *im, *im, 0, padding_h, 0, padding_w, cv::BORDER_CONSTANT, value);
  104. data->new_im_size_[0] = im->rows;
  105. data->new_im_size_[1] = im->cols;
  106. return true;
  107. }
  108. bool ResizeByLong::Run(cv::Mat* im, ImageBlob* data) {
  109. if (long_size_ <= 0) {
  110. std::cerr << "[ResizeByLong] long_size should be greater than 0"
  111. << std::endl;
  112. return false;
  113. }
  114. data->im_size_before_resize_.push_back({im->rows, im->cols});
  115. data->reshape_order_.push_back("resize");
  116. int origin_w = im->cols;
  117. int origin_h = im->rows;
  118. int im_size_max = std::max(origin_w, origin_h);
  119. float scale =
  120. static_cast<float>(long_size_) / static_cast<float>(im_size_max);
  121. cv::resize(*im, *im, cv::Size(), scale, scale, cv::INTER_NEAREST);
  122. data->new_im_size_[0] = im->rows;
  123. data->new_im_size_[1] = im->cols;
  124. data->scale = scale;
  125. return true;
  126. }
  127. bool Resize::Run(cv::Mat* im, ImageBlob* data) {
  128. if (width_ <= 0 || height_ <= 0) {
  129. std::cerr << "[Resize] width and height should be greater than 0"
  130. << std::endl;
  131. return false;
  132. }
  133. if (interpolations.count(interp_) <= 0) {
  134. std::cerr << "[Resize] Invalid interpolation method: '" << interp_ << "'"
  135. << std::endl;
  136. return false;
  137. }
  138. data->im_size_before_resize_.push_back({im->rows, im->cols});
  139. data->reshape_order_.push_back("resize");
  140. cv::resize(
  141. *im, *im, cv::Size(width_, height_), 0, 0, interpolations[interp_]);
  142. data->new_im_size_[0] = im->rows;
  143. data->new_im_size_[1] = im->cols;
  144. return true;
  145. }
  146. void Transforms::Init(const YAML::Node& transforms_node, bool to_rgb) {
  147. transforms_.clear();
  148. to_rgb_ = to_rgb;
  149. for (const auto& item : transforms_node) {
  150. std::string name = item.begin()->first.as<std::string>();
  151. std::cout << "trans name: " << name << std::endl;
  152. std::shared_ptr<Transform> transform = CreateTransform(name);
  153. transform->Init(item.begin()->second);
  154. transforms_.push_back(transform);
  155. }
  156. }
  157. std::shared_ptr<Transform> Transforms::CreateTransform(
  158. const std::string& transform_name) {
  159. if (transform_name == "Normalize") {
  160. return std::make_shared<Normalize>();
  161. } else if (transform_name == "ResizeByShort") {
  162. return std::make_shared<ResizeByShort>();
  163. } else if (transform_name == "CenterCrop") {
  164. return std::make_shared<CenterCrop>();
  165. } else if (transform_name == "Resize") {
  166. return std::make_shared<Resize>();
  167. } else if (transform_name == "Padding") {
  168. return std::make_shared<Padding>();
  169. } else if (transform_name == "ResizeByLong") {
  170. return std::make_shared<ResizeByLong>();
  171. } else {
  172. std::cerr << "There's unexpected transform(name='" << transform_name
  173. << "')." << std::endl;
  174. exit(-1);
  175. }
  176. }
  177. bool Transforms::Run(cv::Mat* im, ImageBlob* data) {
  178. // 按照transforms中预处理算子顺序处理图像
  179. if (to_rgb_) {
  180. cv::cvtColor(*im, *im, cv::COLOR_BGR2RGB);
  181. }
  182. (*im).convertTo(*im, CV_32FC3);
  183. data->ori_im_size_[0] = im->rows;
  184. data->ori_im_size_[1] = im->cols;
  185. data->new_im_size_[0] = im->rows;
  186. data->new_im_size_[1] = im->cols;
  187. for (int i = 0; i < transforms_.size(); ++i) {
  188. if (!transforms_[i]->Run(im, data)) {
  189. std::cerr << "Apply transforms to image failed!" << std::endl;
  190. return false;
  191. }
  192. }
  193. // 将图像由NHWC转为NCHW格式
  194. // 同时转为连续的内存块存储到ImageBlob
  195. int h = im->rows;
  196. int w = im->cols;
  197. int c = im->channels();
  198. (data->im_data_).resize(c * h * w);
  199. float* ptr = (data->im_data_).data();
  200. for (int i = 0; i < c; ++i) {
  201. cv::extractChannel(*im, cv::Mat(h, w, CV_32FC1, ptr + i * h * w), i);
  202. }
  203. return true;
  204. }
  205. } // namespace PaddleX