You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

opr_impl.cpp 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * By downloading, copying, installing or using the software you agree to this license.
  3. * If you do not agree to this license, do not download, install,
  4. * copy or use the software.
  5. *
  6. *
  7. * License Agreement
  8. * For Open Source Computer Vision Library
  9. * (3-clause BSD License)
  10. *
  11. * Copyright (C) 2000-2020, Intel Corporation, all rights reserved.
  12. * Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
  13. * Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
  14. * Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
  15. * Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
  16. * Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
  17. * Copyright (C) 2019-2020, Xperience AI, all rights reserved.
  18. * Third party copyrights are property of their respective owners.
  19. *
  20. * Redistribution and use in source and binary forms, with or without modification,
  21. * are permitted provided that the following conditions are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. *
  26. * * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * * Neither the names of the copyright holders nor the names of the contributors
  31. * may be used to endorse or promote products derived from this software
  32. * without specific prior written permission.
  33. *
  34. * This software is provided by the copyright holders and contributors "as is" and
  35. * any express or implied warranties, including, but not limited to, the implied
  36. * warranties of merchantability and fitness for a particular purpose are disclaimed.
  37. * In no event shall copyright holders or contributors be liable for any direct,
  38. * indirect, incidental, special, exemplary, or consequential damages
  39. * (including, but not limited to, procurement of substitute goods or services;
  40. * loss of use, data, or profits; or business interruption) however caused
  41. * and on any theory of liability, whether in contract, strict liability,
  42. * or tort (including negligence or otherwise) arising in any way out of
  43. * the use of this software, even if advised of the possibility of such damage.
  44. *
  45. * ---------------------------------------------------------------------------
  46. * \file dnn/src/naive/gaussian_blur/opr_impl.cpp
  47. *
  48. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  49. *
  50. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  51. *
  52. * Unless required by applicable law or agreed to in writing,
  53. * software distributed under the License is distributed on an
  54. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  55. *
  56. * This file has been modified by Megvii ("Megvii Modifications").
  57. * All Megvii Modifications are Copyright (C) 2014-2021 Megvii Inc. All rights reserved.
  58. *
  59. * ---------------------------------------------------------------------------
  60. */
  61. #include "./opr_impl.h"
  62. #include "src/common/cv/common.h"
  63. #include "src/common/cv/helper.h"
  64. #include "src/common/gaussian_blur_helper.h"
  65. #include "src/naive/handle.h"
  66. namespace megdnn {
  67. namespace naive {
  68. template <>
  69. void GaussianBlurImpl::exec_internal<uint8_t>(
  70. _megdnn_tensor_in src, _megdnn_tensor_out dst) {
  71. auto N = src.layout.shape[0], IH = src.layout.shape[1], IW = src.layout.shape[2],
  72. IC = src.layout.shape[3];
  73. using namespace megcv;
  74. Size ksize = Size(param().kernel_height, param().kernel_width);
  75. Mat<float> kx_(1, ksize.cols(), 1);
  76. Mat<float> ky_(1, ksize.rows(), 1);
  77. gaussian_blur::createGaussianKernels<float>(
  78. kx_, ky_, ksize, param().sigma_x, param().sigma_y);
  79. uint32_t kernel_height = ky_.width();
  80. uint32_t kernel_width = kx_.width();
  81. Mat<int> kx(1, kernel_width, 1);
  82. Mat<int> ky(1, kernel_height, 1);
  83. const uint8_t bits = 8;
  84. for (size_t i = 0; i < kernel_height; i++) {
  85. ky.at(0, i, 0) = static_cast<int>(ky_.at(0, i, 0) * (1 << bits));
  86. }
  87. for (size_t i = 0; i < kernel_width; i++) {
  88. kx.at(0, i, 0) = static_cast<int>(kx_.at(0, i, 0) * (1 << bits));
  89. }
  90. FixedPtCastEx<int, uint8_t> cast_op(2 * bits);
  91. rep(n, N) rep(h, IH) rep(w, IW) rep(c, IC) {
  92. int val = 0;
  93. rep(iy, kernel_height) {
  94. int y = gaussian_blur::border_interpolate(
  95. h + iy - kernel_height / 2, IH, param().border_mode);
  96. rep(ix, kernel_width) {
  97. int x = gaussian_blur::border_interpolate(
  98. w + ix - kernel_width / 2, IW, param().border_mode);
  99. //! BORDER_CONSTANT or BORDER_TRANSPARENT
  100. if (x != -1 && y != -1) {
  101. val += kx.at(0, ix, 0) * ky.at(0, iy, 0) *
  102. src.ptr<uint8_t>()
  103. [n * src.layout.stride[0] +
  104. y * src.layout.stride[1] +
  105. x * src.layout.stride[2] +
  106. c * src.layout.stride[3]];
  107. }
  108. }
  109. }
  110. dst.ptr<uint8_t>()
  111. [n * dst.layout.stride[0] + h * dst.layout.stride[1] +
  112. w * dst.layout.stride[2] + c * dst.layout.stride[3]] = cast_op(val);
  113. }
  114. }
  115. template <typename T>
  116. void GaussianBlurImpl::exec_internal(_megdnn_tensor_in src, _megdnn_tensor_out dst) {
  117. auto N = src.layout.shape[0], IH = src.layout.shape[1], IW = src.layout.shape[2],
  118. IC = src.layout.shape[3];
  119. using namespace megcv;
  120. Size ksize = Size(param().kernel_height, param().kernel_width);
  121. Mat<float> kx(1, ksize.cols(), 1);
  122. Mat<float> ky(1, ksize.rows(), 1);
  123. gaussian_blur::createGaussianKernels<float>(
  124. kx, ky, ksize, param().sigma_x, param().sigma_y);
  125. uint32_t kernel_height = ky.width();
  126. uint32_t kernel_width = kx.width();
  127. uint32_t half_h = kernel_height / 2;
  128. uint32_t half_w = kernel_width / 2;
  129. rep(n, N) rep(h, IH) rep(w, IW) rep(c, IC) {
  130. double val = 0;
  131. rep(iy, kernel_height) {
  132. int y = gaussian_blur::border_interpolate(
  133. h + iy - half_h, IH, param().border_mode);
  134. rep(ix, kernel_width) {
  135. int x = gaussian_blur::border_interpolate(
  136. w + ix - half_w, IW, param().border_mode);
  137. //! BORDER_CONSTANT or BORDER_TRANSPARENT
  138. if (x != -1 && y != -1) {
  139. val += kx.at(0, ix, 0) * ky.at(0, iy, 0) *
  140. src.ptr<T>()
  141. [n * src.layout.stride[0] +
  142. y * src.layout.stride[1] +
  143. x * src.layout.stride[2] +
  144. c * src.layout.stride[3]];
  145. }
  146. }
  147. }
  148. dst.ptr<T>()
  149. [n * dst.layout.stride[0] + h * dst.layout.stride[1] +
  150. w * dst.layout.stride[2] + c * dst.layout.stride[3]] =
  151. static_cast<T>(val);
  152. }
  153. }
  154. void GaussianBlurImpl::exec(
  155. _megdnn_tensor_in src, _megdnn_tensor_in dst, _megdnn_workspace /*workspace*/) {
  156. #define cb(DType) \
  157. if (src.layout.dtype == DType()) { \
  158. using ctype = typename DTypeTrait<DType>::ctype; \
  159. MEGDNN_DISPATCH_CPU_KERN_OPR(exec_internal<ctype>(src, dst)); \
  160. return; \
  161. }
  162. cb(dtype::Uint8);
  163. cb(dtype::Float32);
  164. #undef cb
  165. megdnn_assert_internal(0);
  166. }
  167. } // namespace naive
  168. } // namespace megdnn
  169. // vim: syntax=cpp.doxygen