dnn-layer-dense-test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2020
  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_dense.h"
  24. #define EPSON 0.00001
  25. static int test(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.layers.dense(input_x, 3, activation=tf.nn.sigmoid, bias_initializer=tf.keras.initializers.he_normal())
  31. data = np.random.rand(1, 5, 6, 3);
  32. sess=tf.Session()
  33. sess.run(tf.global_variables_initializer())
  34. weights = dict([(var.name, sess.run(var)) for var in tf.trainable_variables()])
  35. kernel = weights['dense/kernel:0']
  36. kernel = np.transpose(kernel, [1, 0])
  37. print("kernel:")
  38. print(kernel.shape)
  39. print(list(kernel.flatten()))
  40. bias = weights['dense/bias:0']
  41. print("bias:")
  42. print(bias.shape)
  43. print(list(bias.flatten()))
  44. output = sess.run(y, feed_dict={x: data})
  45. print("input:")
  46. print(data.shape)
  47. print(list(data.flatten()))
  48. print("output:")
  49. print(output.shape)
  50. print(list(output.flatten()))
  51. */
  52. ConvolutionalParams params;
  53. DnnOperand operands[2];
  54. int32_t input_indexes[1];
  55. float input[1*5*6*3] = {
  56. 0.5552418686576308, 0.20653189262022464, 0.31115120939398877, 0.5897014433221428, 0.37340078861060655, 0.6470921693941893, 0.8039950367872679, 0.8762700891949274,
  57. 0.6556655583829558, 0.5911096107039339, 0.18640250865290997, 0.2803248779238966, 0.31586613136402053, 0.9447300740056483, 0.9443980824873418, 0.8158851991115941,
  58. 0.5631010340387631, 0.9407402251929046, 0.6485434876551682, 0.5631376966470001, 0.17581924875609634, 0.7033802439103178, 0.04802402495561675, 0.9183681450194972,
  59. 0.46059317944364, 0.07964160481596883, 0.871787076270302, 0.973743142324361, 0.15923146943258415, 0.8212946080584571, 0.5415954459227064, 0.9552813822803975,
  60. 0.4908552668172057, 0.33723691635292274, 0.46588057864910026, 0.8994239961321776, 0.09845220457674186, 0.1713400292123486, 0.39570294912818826, 0.08018956486392803,
  61. 0.5290478278169032, 0.7141906125920976, 0.0320878067840098, 0.6412406575332606, 0.0075712007102423096, 0.7150828462386156, 0.1311989216968138, 0.4706847944253756,
  62. 0.5447610794883336, 0.3430923933318001, 0.536082357943209, 0.4371629342483694, 0.40227962985019927, 0.3553806249465469, 0.031806622424259245, 0.7053916426174,
  63. 0.3261570237309813, 0.419500213292063, 0.3155691223480851, 0.05664028113178088, 0.3636491555914486, 0.8502419746667123, 0.9836596530684955, 0.1628681802975801,
  64. 0.09410832912479894, 0.28407218939480294, 0.7983417928813697, 0.24132158596506748, 0.8154729498062224, 0.29173768373895637, 0.13407102008052096, 0.18705786678800385,
  65. 0.7167943621295573, 0.09222004247174376, 0.2319220738766018, 0.17708964382285064, 0.1391440370249517, 0.3254088083499256, 0.4013916894718289, 0.4819742663322323,
  66. 0.15080103744648077, 0.9302407847555013, 0.9397597961319524, 0.5719200825550793, 0.9538938024682824, 0.9583882089203861, 0.5168861091262276, 0.1926396841842669,
  67. 0.6781176744337578, 0.719366447288566
  68. };
  69. float expected_output[1*5*6*3] = {
  70. -0.3921688, -0.9243112, -0.29659146, -0.64000785, -0.9466343, -0.62125254, -0.71759033, -0.9171336, -0.735589, -0.34365994,
  71. -0.92100817, -0.23903961, -0.8962277, -0.9521279, -0.90962386, -0.7488303, -0.9563761, -0.7701762, -0.40800542, -0.87684774,
  72. -0.3339763, -0.6354543, -0.97068924, -0.6246325, -0.6992075, -0.9706726, -0.6818918, -0.51864433, -0.9592881, -0.51187396,
  73. -0.7423632, -0.89911884, -0.7457824, -0.82009757, -0.96402895, -0.8235518, -0.61980766, -0.94494647, -0.5410502, -0.8281218,
  74. -0.95508635, -0.8201453, -0.5937325, -0.8679507, -0.500767, -0.39430764, -0.93967676, -0.32183182, -0.58913624, -0.939717,
  75. -0.55179894, -0.55004454, -0.9214453, -0.4889004, -0.75294703, -0.9118363, -0.7200309, -0.3248641, -0.8878874, -0.18977344,
  76. -0.8873837, -0.9571257, -0.90145934, -0.50521654, -0.93739635, -0.39051685, -0.61143184, -0.9591179, -0.605999, -0.40008977,
  77. -0.92219675, -0.26732883, -0.19607787, -0.9172511, -0.07068595, -0.5409857, -0.9387041, -0.44181606, -0.4705004, -0.8899935,
  78. -0.37997037, -0.66105115, -0.89754754, -0.68141997, -0.6324047, -0.886776, -0.65066385, -0.8334821, -0.94801456, -0.83297
  79. };
  80. float *output;
  81. float kernel[3*3] = {
  82. 0.56611896, -0.5144603, -0.82600045, 0.19219112, 0.3835776, -0.7475352, 0.5209291, -0.6301091, -0.99442935};
  83. float bias[3] = {-0.3654299, -1.5711838, -0.15546428};
  84. params.activation = TANH;
  85. params.has_bias = 1;
  86. params.biases = bias;
  87. params.input_num = 3;
  88. params.kernel = kernel;
  89. params.output_num = 3;
  90. operands[0].data = input;
  91. operands[0].dims[0] = 1;
  92. operands[0].dims[1] = 5;
  93. operands[0].dims[2] = 6;
  94. operands[0].dims[3] = 3;
  95. operands[1].data = NULL;
  96. input_indexes[0] = 0;
  97. dnn_execute_layer_dense(operands, input_indexes, 1, &params, NULL);
  98. output = operands[1].data;
  99. for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
  100. if (fabs(output[i] - expected_output[i]) > EPSON) {
  101. printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
  102. av_freep(&output);
  103. return 1;
  104. }
  105. }
  106. av_freep(&output);
  107. return 0;
  108. }
  109. int main(int argc, char **argv)
  110. {
  111. if (test())
  112. return 1;
  113. return 0;
  114. }