transforms.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "include/paddlex/transforms.h"
  18. namespace PaddleX {
  19. std::map<std::string, int> interpolations = {{"LINEAR", cv::INTER_LINEAR},
  20. {"NEAREST", cv::INTER_NEAREST},
  21. {"AREA", cv::INTER_AREA},
  22. {"CUBIC", cv::INTER_CUBIC},
  23. {"LANCZOS4", cv::INTER_LANCZOS4}};
  24. bool Normalize::Run(cv::Mat* im, ImageBlob* data) {
  25. for (int h = 0; h < im->rows; h++) {
  26. for (int w = 0; w < im->cols; w++) {
  27. im->at<cv::Vec3f>(h, w)[0] =
  28. (im->at<cv::Vec3f>(h, w)[0] / 255.0 - mean_[0]) / std_[0];
  29. im->at<cv::Vec3f>(h, w)[1] =
  30. (im->at<cv::Vec3f>(h, w)[1] / 255.0 - mean_[1]) / std_[1];
  31. im->at<cv::Vec3f>(h, w)[2] =
  32. (im->at<cv::Vec3f>(h, w)[2] / 255.0 - mean_[2]) / std_[2];
  33. }
  34. }
  35. return true;
  36. }
  37. float ResizeByShort::GenerateScale(const cv::Mat& im) {
  38. int origin_w = im.cols;
  39. int origin_h = im.rows;
  40. int im_size_max = std::max(origin_w, origin_h);
  41. int im_size_min = std::min(origin_w, origin_h);
  42. float scale =
  43. static_cast<float>(short_size_) / static_cast<float>(im_size_min);
  44. if (max_size_ > 0) {
  45. if (round(scale * im_size_max) > max_size_) {
  46. scale = static_cast<float>(max_size_) / static_cast<float>(im_size_max);
  47. }
  48. }
  49. return scale;
  50. }
  51. bool ResizeByShort::Run(cv::Mat* im, ImageBlob* data) {
  52. data->im_size_before_resize_[0] = im->rows;
  53. data->im_size_before_resize_[1] = im->cols;
  54. data->reshape_order_.push_back("resize");
  55. float scale = GenerateScale(*im);
  56. int width = static_cast<int>(scale * im->cols);
  57. int height = static_cast<int>(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_padding_[0] = im->rows;
  81. data->im_size_before_padding_[1] = im->cols;
  82. data->reshape_order_.push_back("padding");
  83. int padding_w = 0;
  84. int padding_h = 0;
  85. if (width_ > 0 & height_ > 0) {
  86. padding_w = width_ - im->cols;
  87. padding_h = height_ - im->rows;
  88. } else if (coarsest_stride_ > 0) {
  89. padding_h =
  90. ceil(im->rows * 1.0 / coarsest_stride_) * coarsest_stride_ - im->rows;
  91. padding_w =
  92. ceil(im->cols * 1.0 / coarsest_stride_) * coarsest_stride_ - im->cols;
  93. }
  94. if (padding_h < 0 || padding_w < 0) {
  95. std::cerr << "[Padding] Computed padding_h=" << padding_h
  96. << ", padding_w=" << padding_w
  97. << ", but they should be greater than 0." << std::endl;
  98. return false;
  99. }
  100. cv::copyMakeBorder(
  101. *im, *im, 0, padding_h, 0, padding_w, cv::BORDER_CONSTANT, cv::Scalar(0));
  102. data->new_im_size_[0] = im->rows;
  103. data->new_im_size_[1] = im->cols;
  104. return true;
  105. }
  106. bool ResizeByLong::Run(cv::Mat* im, ImageBlob* data) {
  107. if (long_size_ <= 0) {
  108. std::cerr << "[ResizeByLong] long_size should be greater than 0"
  109. << std::endl;
  110. return false;
  111. }
  112. data->im_size_before_resize_[0] = im->rows;
  113. data->im_size_before_resize_[1] = im->cols;
  114. data->reshape_order_.push_back("resize");
  115. int origin_w = im->cols;
  116. int origin_h = im->rows;
  117. int im_size_max = std::max(origin_w, origin_h);
  118. float scale =
  119. static_cast<float>(long_size_) / static_cast<float>(im_size_max);
  120. cv::resize(*im, *im, cv::Size(), scale, scale, cv::INTER_NEAREST);
  121. data->new_im_size_[0] = im->rows;
  122. data->new_im_size_[1] = im->cols;
  123. data->scale = scale;
  124. return true;
  125. }
  126. bool Resize::Run(cv::Mat* im, ImageBlob* data) {
  127. if (width_ <= 0 || height_ <= 0) {
  128. std::cerr << "[Resize] width and height should be greater than 0"
  129. << std::endl;
  130. return false;
  131. }
  132. if (interpolations.count(interp_) <= 0) {
  133. std::cerr << "[Resize] Invalid interpolation method: '" << interp_ << "'"
  134. << std::endl;
  135. return false;
  136. }
  137. data->im_size_before_resize_[0] = im->rows;
  138. data->im_size_before_resize_[1] = 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