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.

interp.cpp 2.9 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "interp.h"
  15. #include <algorithm>
  16. namespace ncnn {
  17. DEFINE_LAYER_CREATOR(Interp);
  18. Interp::Interp()
  19. {
  20. one_blob_only = true;
  21. }
  22. int Interp::load_param(const ParamDict& pd)
  23. {
  24. resize_type = pd.get(0, 0);
  25. height_scale = pd.get(1, 1.f);
  26. width_scale = pd.get(2, 1.f);
  27. output_height = pd.get(3, 0);
  28. output_width = pd.get(4, 0);
  29. return 0;
  30. }
  31. int Interp::forward(const Mat &bottom_blob, Mat &top_blob) const
  32. {
  33. int h = bottom_blob.h;
  34. int w = bottom_blob.w;
  35. int c = bottom_blob.c;
  36. int oh = output_height;
  37. int ow = output_width;
  38. if (bottom_blob.dims == 1)
  39. {
  40. h = 1;
  41. w = 1;
  42. c = bottom_blob.w;
  43. }
  44. if (oh == 0 || ow == 0)
  45. {
  46. oh = h * height_scale;
  47. ow = w * width_scale;
  48. }
  49. if (oh == h && ow == w)
  50. {
  51. top_blob = bottom_blob;
  52. return 0;
  53. }
  54. top_blob.create(ow, oh, c);
  55. if (top_blob.empty())
  56. return -100;
  57. if (bottom_blob.dims == 1)
  58. {
  59. #pragma omp parallel for
  60. for (int q = 0; q < c; ++q)
  61. {
  62. Mat top_blob_c = top_blob.channel(q);
  63. const float *ptr = ((const float*)bottom_blob.data + q);
  64. top_blob_c.fill(*ptr);
  65. }
  66. return 0;
  67. }
  68. if (resize_type == 1)//nearest
  69. {
  70. #pragma omp parallel for
  71. for (int q = 0; q < c; ++q)
  72. {
  73. const float *ptr = bottom_blob.channel(q);
  74. float *output_ptr = top_blob.channel(q);
  75. for (int y = 0; y < oh; ++y)
  76. {
  77. const int in_y = std::min((int) (y / height_scale), (h - 1));
  78. for (int x = 0; x < ow; ++x)
  79. {
  80. const int in_x = std::min((int) (x / width_scale), (w - 1));
  81. output_ptr[ow * y + x] = ptr[in_y * w + in_x];
  82. }
  83. }
  84. }
  85. return 0;
  86. }
  87. else if (resize_type == 2)// bilinear
  88. {
  89. resize_bilinear(bottom_blob, top_blob, ow, oh);
  90. return 0;
  91. }
  92. else
  93. {
  94. fprintf(stderr, "unsupported resize type %d %d %d\n", resize_type, oh, ow);
  95. return -233;
  96. }
  97. }
  98. } // namespace ncnn