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.

test_mat_pixel_resize.cpp 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 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 "mat.h"
  15. #include "prng.h"
  16. #include <math.h>
  17. #include <string.h>
  18. #include <algorithm>
  19. static struct prng_rand_t g_prng_rand_state;
  20. #define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
  21. #define RAND() prng_rand(&g_prng_rand_state)
  22. static ncnn::Mat RandomMat(int w, int h, int elempack)
  23. {
  24. ncnn::Mat m(w, h, 1, (size_t)elempack, elempack);
  25. unsigned char* p = m;
  26. for (int i = 0; i < w * h * elempack; i++)
  27. {
  28. p[i] = RAND() % 256;
  29. }
  30. return m;
  31. }
  32. static bool NearlyEqual(float a, float b, float epsilon)
  33. {
  34. if (a == b)
  35. return true;
  36. float diff = fabs(a - b);
  37. if (diff <= epsilon)
  38. return true;
  39. // relative error
  40. return diff < epsilon * std::max(fabs(a), fabs(b));
  41. }
  42. static int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon = 0.001)
  43. {
  44. #define CHECK_MEMBER(m) \
  45. if (a.m != b.m) \
  46. { \
  47. fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
  48. return -1; \
  49. }
  50. CHECK_MEMBER(dims)
  51. CHECK_MEMBER(w)
  52. CHECK_MEMBER(h)
  53. CHECK_MEMBER(c)
  54. CHECK_MEMBER(elemsize)
  55. CHECK_MEMBER(elempack)
  56. #undef CHECK_MEMBER
  57. for (int q = 0; q < a.c; q++)
  58. {
  59. const ncnn::Mat ma = a.channel(q);
  60. const ncnn::Mat mb = b.channel(q);
  61. for (int i = 0; i < a.h; i++)
  62. {
  63. const float* pa = ma.row(i);
  64. const float* pb = mb.row(i);
  65. for (int j = 0; j < a.w; j++)
  66. {
  67. if (!NearlyEqual(pa[j], pb[j], epsilon))
  68. {
  69. fprintf(stderr, "value not match at c:%d h:%d w:%d expect %f but got %f\n", q, i, j, pa[j], pb[j]);
  70. return -1;
  71. }
  72. }
  73. }
  74. }
  75. return 0;
  76. }
  77. static int test_mat_pixel_resize(int w, int h, int ch, int target_width, int target_height)
  78. {
  79. ncnn::Mat a = RandomMat(w, h, ch);
  80. ncnn::Mat b(target_width, target_height, 1, (size_t)ch, ch);
  81. if (ch == 1) resize_bilinear_c1(a, w, h, b, target_width, target_height);
  82. if (ch == 2) resize_bilinear_c2(a, w, h, b, target_width, target_height);
  83. if (ch == 3) resize_bilinear_c3(a, w, h, b, target_width, target_height);
  84. if (ch == 4) resize_bilinear_c4(a, w, h, b, target_width, target_height);
  85. ncnn::Mat a2;
  86. ncnn::convert_packing(a, a2, 1);
  87. ncnn::Mat b2;
  88. ncnn::convert_packing(b, b2, 1);
  89. for (int i = 0; i < ch; i++)
  90. {
  91. ncnn::Mat c = ncnn::Mat::from_pixels(a2.channel(i), ncnn::Mat::PIXEL_GRAY, w, h);
  92. ncnn::Mat d = ncnn::Mat::from_pixels(b2.channel(i), ncnn::Mat::PIXEL_GRAY, target_width, target_height);
  93. ncnn::Mat e;
  94. ncnn::resize_bilinear(c, e, target_width, target_height);
  95. if (Compare(e, d, 0.5) != 0)
  96. {
  97. fprintf(stderr, "test_mat_pixel_resize failed w=%d h=%d ch=%d target_width=%d target_height=%d\n", w, h, ch, target_width, target_height);
  98. return -1;
  99. }
  100. }
  101. return 0;
  102. }
  103. static int test_mat_pixel_0()
  104. {
  105. for (int c = 1; c <= 4; c++)
  106. {
  107. int ret = 0
  108. || test_mat_pixel_resize(24, 48, c, 24, 48)
  109. || test_mat_pixel_resize(13, 17, c, 11, 14)
  110. || test_mat_pixel_resize(33, 23, c, 5, 6)
  111. || test_mat_pixel_resize(5, 4, c, 11, 16)
  112. || test_mat_pixel_resize(23, 11, c, 15, 21);
  113. if (ret != 0)
  114. return ret;
  115. }
  116. return 0;
  117. }
  118. int main()
  119. {
  120. SRAND(7767517);
  121. return test_mat_pixel_0();
  122. }