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

204 lines
7.6KB

  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. float input[1*4*4*3] = {
  42. 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
  43. };
  44. float expected_output[1*9*9*3] = {
  45. 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,
  46. 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,
  47. 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,
  48. 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,
  49. 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,
  50. 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,
  51. 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,
  52. 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
  53. };
  54. float output[1*9*9*3];
  55. memset(output, 0, sizeof(output));
  56. params.mode = LPMP_SYMMETRIC;
  57. params.paddings[0][0] = 0;
  58. params.paddings[0][1] = 0;
  59. params.paddings[1][0] = 2;
  60. params.paddings[1][1] = 3;
  61. params.paddings[2][0] = 3;
  62. params.paddings[2][1] = 2;
  63. params.paddings[3][0] = 0;
  64. params.paddings[3][1] = 0;
  65. dnn_execute_layer_pad(input, output, &params, 1, 4, 4, 3);
  66. for (int i = 0; i < sizeof(output) / sizeof(float); i++) {
  67. if (fabs(output[i] - expected_output[i]) > EPSON) {
  68. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  69. return 1;
  70. }
  71. }
  72. return 0;
  73. }
  74. static int test_with_mode_reflect(void)
  75. {
  76. // the input data and expected data are generated with below python code.
  77. /*
  78. x = tf.placeholder(tf.float32, shape=[3, None, None, 3])
  79. y = tf.pad(x, [[1, 2], [0, 0], [0, 0], [0, 0]], 'REFLECT')
  80. data = np.arange(36).reshape(3, 2, 2, 3);
  81. sess=tf.Session()
  82. sess.run(tf.global_variables_initializer())
  83. output = sess.run(y, feed_dict={x: data})
  84. print(list(data.flatten()))
  85. print(list(output.flatten()))
  86. print(data.shape)
  87. print(output.shape)
  88. */
  89. LayerPadParams params;
  90. float input[3*2*2*3] = {
  91. 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
  92. };
  93. float expected_output[6*2*2*3] = {
  94. 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,
  95. 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,
  96. 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
  97. };
  98. float output[6*2*2*3];
  99. memset(output, 0, sizeof(output));
  100. params.mode = LPMP_REFLECT;
  101. params.paddings[0][0] = 1;
  102. params.paddings[0][1] = 2;
  103. params.paddings[1][0] = 0;
  104. params.paddings[1][1] = 0;
  105. params.paddings[2][0] = 0;
  106. params.paddings[2][1] = 0;
  107. params.paddings[3][0] = 0;
  108. params.paddings[3][1] = 0;
  109. dnn_execute_layer_pad(input, output, &params, 3, 2, 2, 3);
  110. for (int i = 0; i < sizeof(output) / sizeof(float); i++) {
  111. if (fabs(output[i] - expected_output[i]) > EPSON) {
  112. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  113. return 1;
  114. }
  115. }
  116. return 0;
  117. }
  118. static int test_with_mode_constant(void)
  119. {
  120. // the input data and expected data are generated with below python code.
  121. /*
  122. x = tf.placeholder(tf.float32, shape=[1, None, None, 3])
  123. y = tf.pad(x, [[0, 0], [1, 0], [0, 0], [1, 2]], 'CONSTANT', constant_values=728)
  124. data = np.arange(12).reshape(1, 2, 2, 3);
  125. sess=tf.Session()
  126. sess.run(tf.global_variables_initializer())
  127. output = sess.run(y, feed_dict={x: data})
  128. print(list(data.flatten()))
  129. print(list(output.flatten()))
  130. print(data.shape)
  131. print(output.shape)
  132. */
  133. LayerPadParams params;
  134. float input[1*2*2*3] = {
  135. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
  136. };
  137. float expected_output[1*3*2*6] = {
  138. 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0, 728.0,
  139. 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,
  140. 728.0, 6.0, 7.0, 8.0, 728.0, 728.0, 728.0, 9.0, 10.0, 11.0, 728.0, 728.0
  141. };
  142. float output[1*3*2*6];
  143. memset(output, 0, sizeof(output));
  144. params.mode = LPMP_CONSTANT;
  145. params.constant_values = 728;
  146. params.paddings[0][0] = 0;
  147. params.paddings[0][1] = 0;
  148. params.paddings[1][0] = 1;
  149. params.paddings[1][1] = 0;
  150. params.paddings[2][0] = 0;
  151. params.paddings[2][1] = 0;
  152. params.paddings[3][0] = 1;
  153. params.paddings[3][1] = 2;
  154. dnn_execute_layer_pad(input, output, &params, 1, 2, 2, 3);
  155. for (int i = 0; i < sizeof(output) / sizeof(float); i++) {
  156. if (fabs(output[i] - expected_output[i]) > EPSON) {
  157. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  158. return 1;
  159. }
  160. }
  161. return 0;
  162. }
  163. int main(int argc, char **argv)
  164. {
  165. if (test_with_mode_symmetric())
  166. return 1;
  167. if (test_with_mode_reflect())
  168. return 1;
  169. if (test_with_mode_constant())
  170. return 1;
  171. }