Imported Upstream version 5.1.2
[platform/upstream/ffmpeg.git] / libavfilter / tests / dnn-layer-depth2space.c
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
21 #include <stdio.h>
22 #include <string.h>
23 #include <math.h>
24 #include "libavfilter/dnn/dnn_backend_native.h"
25 #include "libavfilter/dnn/dnn_backend_native_layer_depth2space.h"
26
27 #define EPSON 0.00001
28
29 static int test(void)
30 {
31     // the input data and expected data are generated with below python code.
32     /*
33     x = tf.placeholder(tf.float32, shape=[1, None, None, 4])
34     y = tf.depth_to_space(x, 2)
35     data = np.random.rand(1, 5, 3, 4);
36
37     sess=tf.Session()
38     sess.run(tf.global_variables_initializer())
39
40     output = sess.run(y, feed_dict={x: data})
41
42     print("input:")
43     print(data.shape)
44     print(list(data.flatten()))
45
46     print("output:")
47     print(output.shape)
48     print(list(output.flatten()))
49     */
50
51     DepthToSpaceParams params;
52     DnnOperand operands[2];
53     int32_t input_indexes[1];
54     float input[1*5*3*4] = {
55         0.09771065121566602, 0.6336807372403175, 0.5142416549709786, 0.8027206567330333, 0.2154276025069397, 0.12112878462616772, 0.913936596765778,
56         0.38881443647542646, 0.5850447615898835, 0.9311499327398275, 0.3613660929428246, 0.5420722002125493, 0.6002131190230359, 0.44800665702299525,
57         0.7271322557896777, 0.3869293511885826, 0.5144404769364138, 0.6910844856987723, 0.6142102742269762, 0.6249991371621018, 0.45663376215836626,
58         0.19523477129943423, 0.2483895888532045, 0.64326768256278, 0.5485877602998981, 0.45442067849873546, 0.529374943304256, 0.30439850391811885,
59         0.11961343361340993, 0.2909643484561082, 0.9810970344127848, 0.8886928489786549, 0.6112237084436409, 0.8852482695156674, 0.9110868043114374,
60         0.21242780027585217, 0.7101536973207572, 0.9709717457443375, 0.2702666770969332, 0.7718295953780221, 0.3957005164588574, 0.24383544252475453,
61         0.040143453532367035, 0.26358051835323115, 0.013130251443791319, 0.3016550481482074, 0.03582340459943956, 0.718025513612361, 0.09844204177633753,
62         0.04433767496953056, 0.6221895044119757, 0.6190414032940228, 0.8963550834625371, 0.5642449700064629, 0.2482982014723497, 0.17824909294583013,
63         0.024401882408643272, 0.21742800875253465, 0.6794724473181843, 0.4814830479242237
64     };
65     float expected_output[1*10*6*1] = {
66         0.097710654, 0.63368076, 0.2154276, 0.12112878, 0.58504474, 0.93114996, 0.51424164, 0.80272067, 0.9139366, 0.38881445,
67         0.3613661, 0.5420722, 0.6002131, 0.44800666, 0.5144405, 0.6910845, 0.45663378, 0.19523478, 0.72713226, 0.38692936,
68         0.61421025, 0.62499917, 0.24838959, 0.6432677, 0.54858774, 0.4544207, 0.11961343, 0.29096434, 0.6112237, 0.88524824,
69         0.52937496, 0.3043985, 0.98109704, 0.88869286, 0.9110868, 0.2124278, 0.7101537, 0.97097176, 0.3957005, 0.24383545,
70         0.013130251, 0.30165505, 0.27026668, 0.7718296, 0.040143453, 0.26358053, 0.035823405, 0.7180255, 0.09844204,
71         0.044337675, 0.8963551, 0.564245, 0.024401883, 0.21742801, 0.6221895, 0.6190414, 0.2482982, 0.17824909, 0.67947245, 0.48148304
72     };
73     float *output;
74
75     operands[0].data = input;
76     operands[0].dims[0] = 1;
77     operands[0].dims[1] = 5;
78     operands[0].dims[2] = 3;
79     operands[0].dims[3] = 4;
80     operands[1].data = NULL;
81
82     input_indexes[0] = 0;
83     params.block_size = 2;
84     ff_dnn_execute_layer_depth2space(operands, input_indexes, 1, &params, NULL);
85
86     output = operands[1].data;
87     for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
88         if (fabs(output[i] - expected_output[i]) > EPSON) {
89             printf("at index %d, output: %f, expected_output: %f\n", i, output[i], expected_output[i]);
90             av_freep(&output);
91             return 1;
92         }
93     }
94
95     av_freep(&output);
96     return 0;
97 }
98
99 int main(int argc, char **argv)
100 {
101     return test();
102 }