dnn-layer-pad-test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2019 Guo Yejun
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include "libavfilter/dnn/dnn_backend_native_layer_pad.h"
  24. #define EPSON 0.00001
  25. static int test_with_mode_symmetric(void)
  26. {
  27. // the input data and expected data are generated with below python code.
  28. /*
  29. x = tf.placeholder(tf.float32, shape=[1, None, None, 3])
  30. y = tf.pad(x, [[0, 0], [2, 3], [3, 2], [0, 0]], 'SYMMETRIC')
  31. data = np.arange(48).reshape(1, 4, 4, 3);
  32. sess=tf.Session()
  33. sess.run(tf.global_variables_initializer())
  34. output = sess.run(y, feed_dict={x: data})
  35. print(list(data.flatten()))
  36. print(list(output.flatten()))
  37. print(data.shape)
  38. print(output.shape)
  39. */
  40. LayerPadParams params;
  41. DnnOperand operands[2];
  42. int32_t input_indexes[1];
  43. float input[1*4*4*3] = {
  44. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47
  45. };
  46. float expected_output[1*9*9*3] = {
  47. 18.0, 19.0, 20.0, 15.0, 16.0, 17.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 21.0, 22.0, 23.0, 18.0, 19.0, 20.0, 6.0, 7.0, 8.0, 3.0,
  48. 4.0, 5.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 6.0, 7.0, 8.0, 3.0, 4.0, 5.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 3.0,
  49. 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 18.0, 19.0, 20.0, 15.0, 16.0, 17.0, 12.0, 13.0, 14.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0,
  50. 21.0, 22.0, 23.0, 21.0, 22.0, 23.0, 18.0, 19.0, 20.0, 30.0, 31.0, 32.0, 27.0, 28.0, 29.0, 24.0, 25.0, 26.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 33.0,
  51. 34.0, 35.0, 30.0, 31.0, 32.0, 42.0, 43.0, 44.0, 39.0, 40.0, 41.0, 36.0, 37.0, 38.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 45.0, 46.0, 47.0, 42.0, 43.0,
  52. 44.0, 42.0, 43.0, 44.0, 39.0, 40.0, 41.0, 36.0, 37.0, 38.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 45.0, 46.0, 47.0, 42.0, 43.0, 44.0, 30.0, 31.0, 32.0,
  53. 27.0, 28.0, 29.0, 24.0, 25.0, 26.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 33.0, 34.0, 35.0, 30.0, 31.0, 32.0, 18.0, 19.0, 20.0, 15.0, 16.0, 17.0, 12.0,
  54. 13.0, 14.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 21.0, 22.0, 23.0, 18.0, 19.0, 20.0
  55. };
  56. float *output;
  57. params.mode = LPMP_SYMMETRIC;
  58. params.paddings[0][0] = 0;
  59. params.paddings[0][1] = 0;
  60. params.paddings[1][0] = 2;
  61. params.paddings[1][1] = 3;
  62. params.paddings[2][0] = 3;
  63. params.paddings[2][1] = 2;
  64. params.paddings[3][0] = 0;
  65. params.paddings[3][1] = 0;
  66. operands[0].data = input;
  67. operands[0].dims[0] = 1;
  68. operands[0].dims[1] = 4;
  69. operands[0].dims[2] = 4;
  70. operands[0].dims[3] = 3;
  71. operands[1].data = NULL;
  72. input_indexes[0] = 0;
  73. dnn_execute_layer_pad(operands, input_indexes, 1, &params);
  74. output = operands[1].data;
  75. for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
  76. if (fabs(output[i] - expected_output[i]) > EPSON) {
  77. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  78. av_freep(&output);
  79. return 1;
  80. }
  81. }
  82. av_freep(&output);
  83. return 0;
  84. }
  85. static int test_with_mode_reflect(void)
  86. {
  87. // the input data and expected data are generated with below python code.
  88. /*
  89. x = tf.placeholder(tf.float32, shape=[3, None, None, 3])
  90. y = tf.pad(x, [[1, 2], [0, 0], [0, 0], [0, 0]], 'REFLECT')
  91. data = np.arange(36).reshape(3, 2, 2, 3);
  92. sess=tf.Session()
  93. sess.run(tf.global_variables_initializer())
  94. output = sess.run(y, feed_dict={x: data})
  95. print(list(data.flatten()))
  96. print(list(output.flatten()))
  97. print(data.shape)
  98. print(output.shape)
  99. */
  100. LayerPadParams params;
  101. DnnOperand operands[2];
  102. int32_t input_indexes[1];
  103. float input[3*2*2*3] = {
  104. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35
  105. };
  106. float expected_output[6*2*2*3] = {
  107. 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0,
  108. 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0,
  109. 35.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0
  110. };
  111. float *output;
  112. params.mode = LPMP_REFLECT;
  113. params.paddings[0][0] = 1;
  114. params.paddings[0][1] = 2;
  115. params.paddings[1][0] = 0;
  116. params.paddings[1][1] = 0;
  117. params.paddings[2][0] = 0;
  118. params.paddings[2][1] = 0;
  119. params.paddings[3][0] = 0;
  120. params.paddings[3][1] = 0;
  121. operands[0].data = input;
  122. operands[0].dims[0] = 3;
  123. operands[0].dims[1] = 2;
  124. operands[0].dims[2] = 2;
  125. operands[0].dims[3] = 3;
  126. operands[1].data = NULL;
  127. input_indexes[0] = 0;
  128. dnn_execute_layer_pad(operands, input_indexes, 1, &params);
  129. output = operands[1].data;
  130. for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
  131. if (fabs(output[i] - expected_output[i]) > EPSON) {
  132. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  133. av_freep(&output);
  134. return 1;
  135. }
  136. }
  137. av_freep(&output);
  138. return 0;
  139. }
  140. static int test_with_mode_constant(void)
  141. {
  142. // the input data and expected data are generated with below python code.
  143. /*
  144. x = tf.placeholder(tf.float32, shape=[1, None, None, 3])
  145. y = tf.pad(x, [[0, 0], [1, 0], [0, 0], [1, 2]], 'CONSTANT', constant_values=728)
  146. data = np.arange(12).reshape(1, 2, 2, 3);
  147. sess=tf.Session()
  148. sess.run(tf.global_variables_initializer())
  149. output = sess.run(y, feed_dict={x: data})
  150. print(list(data.flatten()))
  151. print(list(output.flatten()))
  152. print(data.shape)
  153. print(output.shape)
  154. */
  155. LayerPadParams params;
  156. DnnOperand operands[2];
  157. int32_t input_indexes[1];
  158. float input[1*2*2*3] = {
  159. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  160. };
  161. float expected_output[1*3*2*6] = {
  162. 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0,
  163. 728.0, 728.0, 0.0, 1.0, 2.0, 728.0, 728.0, 728.0, 3.0, 4.0, 5.0, 728.0, 728.0,
  164. 728.0, 6.0, 7.0, 8.0, 728.0, 728.0, 728.0, 9.0, 10.0, 11.0, 728.0, 728.0
  165. };
  166. float *output;
  167. params.mode = LPMP_CONSTANT;
  168. params.constant_values = 728;
  169. params.paddings[0][0] = 0;
  170. params.paddings[0][1] = 0;
  171. params.paddings[1][0] = 1;
  172. params.paddings[1][1] = 0;
  173. params.paddings[2][0] = 0;
  174. params.paddings[2][1] = 0;
  175. params.paddings[3][0] = 1;
  176. params.paddings[3][1] = 2;
  177. operands[0].data = input;
  178. operands[0].dims[0] = 3;
  179. operands[0].dims[1] = 2;
  180. operands[0].dims[2] = 2;
  181. operands[0].dims[3] = 3;
  182. operands[1].data = NULL;
  183. input_indexes[0] = 0;
  184. dnn_execute_layer_pad(operands, input_indexes, 1, &params);
  185. output = operands[1].data;
  186. for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
  187. if (fabs(output[i] - expected_output[i]) > EPSON) {
  188. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  189. av_freep(&output);
  190. return 1;
  191. }
  192. }
  193. av_freep(&output);
  194. return 0;
  195. }
  196. int main(int argc, char **argv)
  197. {
  198. if (test_with_mode_symmetric())
  199. return 1;
  200. if (test_with_mode_reflect())
  201. return 1;
  202. if (test_with_mode_constant())
  203. return 1;
  204. }