Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 4.1 KiBRawPermalink
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 "testutil.h"
16
17static int test_crop(const ncnn::Mat& a, int woffset, int hoffset, int doffset, int coffset, const ncnn::Mat& ref)
18{
19 ncnn::ParamDict pd;
20 pd.set(0, woffset);
21 pd.set(1, hoffset);
22 pd.set(13, doffset);
23 pd.set(2, coffset);
24 pd.set(3, 0); // outw
25 pd.set(4, 0); // outh
26 pd.set(14, 0); // outd
27 pd.set(5, 0); // outc
28 pd.set(6, 0); // woffset2
29 pd.set(7, 0); // hoffset2
30 pd.set(15, 0); // doffset2
31 pd.set(8, 0); // coffset2
32
33 std::vector<ncnn::Mat> weights(0);
34
35 std::vector<ncnn::Mat> ab(2);
36 ab[0] = a;
37 ab[1] = ref;
38
39 int ret = test_layer("Crop", pd, weights, ab);
40 if (ret != 0)
41 {
42 fprintf(stderr, "test_crop failed a.dims=%d a=(%d %d %d %d) woffset=%d hoffset=%d doffset=%d coffset=%d ref.dims=%d ref=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c, woffset, hoffset, doffset, coffset, ref.dims, ref.w, ref.h, ref.d, ref.c);
43 }
44
45 return ret;
46}
47
48static int test_crop_2(const ncnn::Mat& a)
49{
50 return 0
51 || test_crop(a, 0, 0, 0, 0, a)
52 || test_crop(a, 0, 0, 0, 0, ncnn::Mat(27))
53
54 || test_crop(a, 11, 0, 0, 0, ncnn::Mat(7))
55 || test_crop(a, 12, 0, 0, 0, ncnn::Mat(12))
56 || test_crop(a, 16, 0, 0, 0, ncnn::Mat(16));
57}
58
59static int test_crop_5(const ncnn::Mat& a)
60{
61 return 0
62 || test_crop(a, 0, 0, 0, 0, a)
63
64 || test_crop(a, 0, 12, 0, 0, ncnn::Mat(8, 7))
65 || test_crop(a, 5, 0, 0, 0, ncnn::Mat(7, 27))
66
67 || test_crop(a, 5, 11, 0, 0, ncnn::Mat(5, 12))
68 || test_crop(a, 6, 12, 0, 0, ncnn::Mat(4, 16))
69 || test_crop(a, 4, 8, 0, 0, ncnn::Mat(6, 7));
70}
71
72static int test_crop_8(const ncnn::Mat& a)
73{
74 return 0
75 || test_crop(a, 0, 0, 0, 0, a)
76
77 || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6))
78 || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8))
79 || test_crop(a, 5, 2, 0, 0, ncnn::Mat(6, 3))
80 || test_crop(a, 6, 3, 0, 0, ncnn::Mat(8, 4))
81 || test_crop(a, 4, 4, 0, 0, ncnn::Mat(7, 5))
82
83 || test_crop(a, 5, 3, 0, 11, ncnn::Mat(7, 3, 7))
84 || test_crop(a, 6, 4, 0, 12, ncnn::Mat(6, 4, 12))
85 || test_crop(a, 4, 2, 0, 8, ncnn::Mat(5, 5, 16));
86}
87
88static int test_crop_11(const ncnn::Mat& a)
89{
90 return 0
91 || test_crop(a, 0, 0, 0, 0, a)
92
93 || test_crop(a, 0, 5, 0, 0, ncnn::Mat(6, 6, 6))
94 || test_crop(a, 6, 0, 0, 0, ncnn::Mat(8, 8, 8))
95 || test_crop(a, 5, 5, 5, 0, ncnn::Mat(6, 6, 6))
96 || test_crop(a, 6, 6, 6, 0, ncnn::Mat(8, 8, 8))
97 || test_crop(a, 4, 4, 4, 0, ncnn::Mat(5, 5, 5))
98
99 || test_crop(a, 3, 3, 3, 11, ncnn::Mat(3, 3, 3, 7))
100 || test_crop(a, 4, 4, 4, 12, ncnn::Mat(6, 6, 6, 12))
101 || test_crop(a, 5, 5, 5, 8, ncnn::Mat(8, 8, 8, 16));
102}
103
104int main()
105{
106 SRAND(776757);
107
108 return 0
109 || test_crop_2(RandomMat(112))
110 || test_crop_2(RandomMat(126))
111 || test_crop_2(RandomMat(127))
112 || test_crop_5(RandomMat(20, 48))
113 || test_crop_5(RandomMat(15, 36))
114 || test_crop_5(RandomMat(16, 33))
115 || test_crop_8(RandomMat(20, 20, 48))
116 || test_crop_8(RandomMat(15, 15, 36))
117 || test_crop_8(RandomMat(16, 16, 33))
118 || test_crop_11(RandomMat(20, 20, 20, 48))
119 || test_crop_11(RandomMat(15, 15, 15, 36))
120 || test_crop_11(RandomMat(16, 16, 16, 33));
121}