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 
15 #include "layer/interp.h"
16 #include "testutil.h"
17 
test_interp(const ncnn::Mat & a,int resize_type,float height_scale,float width_scale,int output_height,int output_width)18 static int test_interp(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width)
19 {
20     ncnn::ParamDict pd;
21     pd.set(0, resize_type);
22     pd.set(1, height_scale);
23     pd.set(2, width_scale);
24     pd.set(3, output_height);
25     pd.set(4, output_width);
26 
27     std::vector<ncnn::Mat> weights(0);
28 
29     int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
30     if (ret != 0)
31     {
32         fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width);
33     }
34 
35     return ret;
36 }
37 
test_interp_ref(const ncnn::Mat & a,int resize_type,int output_height,int output_width)38 static int test_interp_ref(const ncnn::Mat& a, int resize_type, int output_height, int output_width)
39 {
40     ncnn::ParamDict pd;
41     pd.set(0, resize_type);
42     pd.set(5, 1);
43 
44     std::vector<ncnn::Mat> as(2);
45     as[0] = a;
46     as[1] = ncnn::Mat(output_width, output_height, 1);
47 
48     std::vector<ncnn::Mat> weights(0);
49 
50     int ret = test_layer<ncnn::Interp>("Interp", pd, weights, as);
51     if (ret != 0)
52     {
53         fprintf(stderr, "test_interp_ref failed a.dims=%d a=(%d %d %d) resize_type=%d output_height=%d output_width=%d\n", a.dims, a.w, a.h, a.c, resize_type, output_height, output_width);
54     }
55 
56     return ret;
57 }
58 
test_interp_align_corner(const ncnn::Mat & a,int resize_type,float height_scale,float width_scale,int output_height,int output_width,int align_corner)59 static int test_interp_align_corner(const ncnn::Mat& a, int resize_type, float height_scale, float width_scale, int output_height, int output_width, int align_corner)
60 {
61     ncnn::ParamDict pd;
62     pd.set(0, resize_type);
63     pd.set(1, height_scale);
64     pd.set(2, width_scale);
65     pd.set(3, output_height);
66     pd.set(4, output_width);
67     pd.set(6, align_corner);
68 
69     std::vector<ncnn::Mat> weights(0);
70 
71     int ret = test_layer<ncnn::Interp>("Interp", pd, weights, a);
72     if (ret != 0)
73     {
74         fprintf(stderr, "test_interp failed a.dims=%d a=(%d %d %d) resize_type=%d height_scale=%f width_scale=%f output_height=%d output_width=%d align_corner=%d\n", a.dims, a.w, a.h, a.c, resize_type, height_scale, width_scale, output_height, output_width, align_corner);
75     }
76 
77     return ret;
78 }
79 
test_interp_0()80 static int test_interp_0()
81 {
82     ncnn::Mat a = RandomMat(15, 16, 7);
83     ncnn::Mat b = RandomMat(14, 17, 12);
84     ncnn::Mat c = RandomMat(13, 14, 24);
85 
86     return 0
87            || test_interp(a, 1, 2.f, 2.f, 0, 0)
88            || test_interp(a, 1, 4.f, 0.5f, 0, 0)
89            || test_interp(a, 1, 1.2f, 1.2f, 0, 0)
90            || test_interp(a, 1, 0.8f, 0.8f, 0, 0)
91            || test_interp(a, 1, 1.f, 1.f, 10, 12)
92            || test_interp(a, 1, 1.f, 1.f, 2, 2)
93            || test_interp(a, 1, 1.f, 1.f, 15, 16)
94            || test_interp_ref(a, 1, 10, 12)
95            || test_interp_ref(a, 1, 2, 2)
96            || test_interp_ref(a, 1, 15, 16)
97 
98            || test_interp(b, 1, 2.f, 2.f, 0, 0)
99            || test_interp(b, 1, 4.f, 0.5f, 0, 0)
100            || test_interp(b, 1, 1.2f, 1.2f, 0, 0)
101            || test_interp(b, 1, 0.8f, 0.8f, 0, 0)
102            || test_interp(b, 1, 1.f, 1.f, 10, 12)
103            || test_interp(b, 1, 1.f, 1.f, 2, 2)
104            || test_interp(b, 1, 1.f, 1.f, 14, 17)
105            || test_interp_ref(b, 1, 10, 12)
106            || test_interp_ref(b, 1, 2, 2)
107            || test_interp_ref(b, 1, 14, 17)
108 
109            || test_interp(c, 1, 2.f, 2.f, 0, 0)
110            || test_interp(c, 1, 4.f, 0.5f, 0, 0)
111            || test_interp(c, 1, 1.2f, 1.2f, 0, 0)
112            || test_interp(c, 1, 0.8f, 0.8f, 0, 0)
113            || test_interp(c, 1, 1.f, 1.f, 10, 12)
114            || test_interp(c, 1, 1.f, 1.f, 2, 2)
115            || test_interp(c, 1, 1.f, 1.f, 14, 17)
116            || test_interp_ref(c, 1, 10, 12)
117            || test_interp_ref(c, 1, 2, 2)
118            || test_interp_ref(c, 1, 14, 17);
119 }
120 
test_interp_1()121 static int test_interp_1()
122 {
123     ncnn::Mat a = RandomMat(15, 16, 7);
124     ncnn::Mat b = RandomMat(14, 17, 12);
125     ncnn::Mat c = RandomMat(13, 14, 24);
126 
127     return 0
128            || test_interp(a, 2, 2.f, 2.f, 0, 0)
129            || test_interp(a, 2, 4.f, 0.5f, 0, 0)
130            || test_interp(a, 2, 1.2f, 1.2f, 0, 0)
131            || test_interp(a, 2, 0.8f, 0.8f, 0, 0)
132            || test_interp(a, 2, 1.f, 1.f, 10, 12)
133            || test_interp(a, 2, 1.f, 1.f, 2, 2)
134            || test_interp(a, 2, 1.f, 1.f, 15, 16)
135            || test_interp_align_corner(a, 2, 2.f, 2.f, 0, 0, 1)
136            || test_interp_align_corner(a, 2, 4.f, 0.5f, 0, 0, 1)
137            || test_interp_align_corner(a, 2, 1.2f, 1.2f, 0, 0, 1)
138            || test_interp_align_corner(a, 2, 0.8f, 0.8f, 0, 0, 1)
139            || test_interp_align_corner(a, 2, 1.f, 1.f, 10, 12, 1)
140            || test_interp_align_corner(a, 2, 1.f, 1.f, 2, 2, 1)
141            || test_interp_align_corner(a, 2, 1.f, 1.f, 15, 16, 1)
142            || test_interp_ref(a, 2, 10, 12)
143            || test_interp_ref(a, 2, 2, 2)
144            || test_interp_ref(a, 2, 15, 16)
145 
146            || test_interp(b, 2, 2.f, 2.f, 0, 0)
147            || test_interp(b, 2, 4.f, 0.5f, 0, 0)
148            || test_interp(b, 2, 1.2f, 1.2f, 0, 0)
149            || test_interp(b, 2, 0.8f, 0.8f, 0, 0)
150            || test_interp(b, 2, 1.f, 1.f, 10, 12)
151            || test_interp(b, 2, 1.f, 1.f, 2, 2)
152            || test_interp(b, 2, 1.f, 1.f, 14, 17)
153            || test_interp_align_corner(b, 2, 2.f, 2.f, 0, 0, 1)
154            || test_interp_align_corner(b, 2, 4.f, 0.5f, 0, 0, 1)
155            || test_interp_align_corner(b, 2, 1.2f, 1.2f, 0, 0, 1)
156            || test_interp_align_corner(b, 2, 0.8f, 0.8f, 0, 0, 1)
157            || test_interp_align_corner(b, 2, 1.f, 1.f, 10, 12, 1)
158            || test_interp_align_corner(b, 2, 1.f, 1.f, 2, 2, 1)
159            || test_interp_align_corner(b, 2, 1.f, 1.f, 14, 17, 1)
160            || test_interp_ref(b, 2, 10, 12)
161            || test_interp_ref(b, 2, 2, 2)
162            || test_interp_ref(b, 2, 14, 17)
163 
164            || test_interp(c, 2, 2.f, 2.f, 0, 0)
165            || test_interp(c, 2, 4.f, 0.5f, 0, 0)
166            || test_interp(c, 2, 1.2f, 1.2f, 0, 0)
167            || test_interp(c, 2, 0.8f, 0.8f, 0, 0)
168            || test_interp(c, 2, 1.f, 1.f, 10, 12)
169            || test_interp(c, 2, 1.f, 1.f, 2, 2)
170            || test_interp(c, 2, 1.f, 1.f, 14, 17)
171            || test_interp_align_corner(c, 2, 2.f, 2.f, 0, 0, 1)
172            || test_interp_align_corner(c, 2, 4.f, 0.5f, 0, 0, 1)
173            || test_interp_align_corner(c, 2, 1.2f, 1.2f, 0, 0, 1)
174            || test_interp_align_corner(c, 2, 0.8f, 0.8f, 0, 0, 1)
175            || test_interp_align_corner(c, 2, 1.f, 1.f, 10, 12, 1)
176            || test_interp_align_corner(c, 2, 1.f, 1.f, 2, 2, 1)
177            || test_interp_align_corner(c, 2, 1.f, 1.f, 14, 17, 1)
178            || test_interp_ref(c, 2, 10, 12)
179            || test_interp_ref(c, 2, 2, 2)
180            || test_interp_ref(c, 2, 14, 17);
181 }
182 
test_interp_2()183 static int test_interp_2()
184 {
185     ncnn::Mat a = RandomMat(16, 17, 13);
186     ncnn::Mat b = RandomMat(18, 19, 12);
187     ncnn::Mat c = RandomMat(13, 14, 24);
188 
189     return 0
190            || test_interp(a, 3, 2.f, 2.f, 0, 0)
191            || test_interp(a, 3, 4.f, 0.5f, 0, 0)
192            || test_interp(a, 3, 1.2f, 1.2f, 0, 0)
193            || test_interp(a, 3, 0.8f, 0.8f, 0, 0)
194            || test_interp(a, 3, 1.f, 1.f, 10, 12)
195            || test_interp(a, 3, 1.f, 1.f, 2, 2)
196            || test_interp(a, 3, 1.f, 1.f, 6, 7)
197            || test_interp(a, 3, 1.f, 1.f, 16, 17)
198            || test_interp_ref(a, 3, 2, 2)
199            || test_interp_ref(a, 3, 6, 7)
200            || test_interp_ref(a, 3, 16, 17)
201 
202            || test_interp(b, 3, 2.f, 2.f, 0, 0)
203            || test_interp(b, 3, 4.f, 0.5f, 0, 0)
204            || test_interp(b, 3, 1.2f, 1.2f, 0, 0)
205            || test_interp(b, 3, 0.8f, 0.8f, 0, 0)
206            || test_interp(b, 3, 1.f, 1.f, 10, 12)
207            || test_interp(b, 3, 1.f, 1.f, 2, 2)
208            || test_interp(b, 3, 1.f, 1.f, 6, 7)
209            || test_interp(b, 3, 1.f, 1.f, 18, 19)
210            || test_interp_ref(b, 3, 2, 2)
211            || test_interp_ref(b, 3, 6, 7)
212            || test_interp_ref(b, 3, 18, 19)
213 
214            || test_interp(c, 3, 2.f, 2.f, 0, 0)
215            || test_interp(c, 3, 4.f, 0.5f, 0, 0)
216            || test_interp(c, 3, 1.2f, 1.2f, 0, 0)
217            || test_interp(c, 3, 0.8f, 0.8f, 0, 0)
218            || test_interp(c, 3, 1.f, 1.f, 10, 12)
219            || test_interp(c, 3, 1.f, 1.f, 2, 2)
220            || test_interp(c, 3, 1.f, 1.f, 6, 7)
221            || test_interp(c, 3, 1.f, 1.f, 18, 19)
222            || test_interp_ref(c, 3, 2, 2)
223            || test_interp_ref(c, 3, 6, 7)
224            || test_interp_ref(c, 3, 18, 19);
225 }
226 
test_interp_3()227 static int test_interp_3()
228 {
229     ncnn::Mat a = RandomMat(17);
230     ncnn::Mat b = RandomMat(20);
231     ncnn::Mat c = RandomMat(48);
232 
233     return 0
234            || test_interp(a, 1, 2.f, 3.f, 0, 0)
235            || test_interp(a, 1, 1.f, 1.f, 10, 12)
236            || test_interp(a, 1, 1.f, 1.f, 15, 16)
237            || test_interp_ref(a, 1, 10, 12)
238            || test_interp_ref(a, 1, 4, 4)
239            || test_interp_ref(a, 1, 15, 16)
240 
241            || test_interp(b, 1, 4.f, 5.f, 0, 0)
242            || test_interp(b, 1, 1.f, 1.f, 10, 12)
243            || test_interp(b, 1, 1.f, 1.f, 14, 17)
244            || test_interp_ref(b, 1, 5, 5)
245            || test_interp_ref(b, 1, 14, 17)
246 
247            || test_interp(c, 1, 6.f, 7.f, 0, 0)
248            || test_interp(c, 1, 1.f, 1.f, 10, 12)
249            || test_interp(c, 1, 1.f, 1.f, 14, 17)
250            || test_interp_ref(c, 1, 6, 6)
251            || test_interp_ref(c, 1, 14, 17);
252 }
253 
main()254 int main()
255 {
256     SRAND(7767517);
257 
258     return 0
259            || test_interp_0()
260            || test_interp_1()
261            || test_interp_2()
262            || test_interp_3();
263 }
264