Elffuss BraidHub

kikocis/ncnn public

master · 3.8 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_normalize(const ncnn::Mat& a, int across_spatial, int across_channel, int channel_shared, float eps, int eps_mode)
18{
19 int scale_data_size = channel_shared ? 1 : a.c;
20
21 ncnn::ParamDict pd;
22 pd.set(0, across_spatial);
23 pd.set(4, across_channel);
24 pd.set(1, channel_shared);
25 pd.set(2, eps);
26 pd.set(3, scale_data_size);
27 pd.set(9, eps_mode);
28
29 std::vector<ncnn::Mat> weights(1);
30 weights[0] = RandomMat(scale_data_size);
31
32 int ret = test_layer("Normalize", pd, weights, a);
33 if (ret != 0)
34 {
35 fprintf(stderr, "test_normalize failed a.dims=%d a=(%d %d %d) across_spatial=%d across_channel=%d channel_shared=%d eps=%f eps_mode=%d\n", a.dims, a.w, a.h, a.c, across_spatial, across_channel, channel_shared, eps, eps_mode);
36 }
37
38 return ret;
39}
40
41static int test_normalize_0()
42{
43 ncnn::Mat a = RandomMat(6, 4, 2);
44 ncnn::Mat b = RandomMat(5, 7, 8);
45
46 return 0
47 || test_normalize(a, 1, 0, 0, 0.01f, 0)
48 || test_normalize(a, 1, 0, 0, 0.001f, 1)
49 || test_normalize(a, 1, 0, 0, 0.002f, 2)
50 || test_normalize(a, 1, 0, 1, 0.01f, 0)
51 || test_normalize(a, 1, 0, 1, 0.001f, 1)
52 || test_normalize(a, 1, 0, 1, 0.002f, 2)
53 || test_normalize(b, 1, 0, 0, 0.01f, 0)
54 || test_normalize(b, 1, 0, 0, 0.001f, 1)
55 || test_normalize(b, 1, 0, 0, 0.002f, 2)
56 || test_normalize(b, 1, 0, 1, 0.01f, 0)
57 || test_normalize(b, 1, 0, 1, 0.001f, 1)
58 || test_normalize(b, 1, 0, 1, 0.002f, 2);
59}
60
61static int test_normalize_1()
62{
63 ncnn::Mat a = RandomMat(5, 6, 3);
64 ncnn::Mat b = RandomMat(3, 4, 8);
65
66 return 0
67 || test_normalize(a, 0, 1, 0, 0.01f, 0)
68 || test_normalize(a, 0, 1, 0, 0.001f, 1)
69 || test_normalize(a, 0, 1, 0, 0.002f, 2)
70 || test_normalize(a, 0, 1, 1, 0.01f, 0)
71 || test_normalize(a, 0, 1, 1, 0.001f, 1)
72 || test_normalize(a, 0, 1, 1, 0.002f, 2)
73 || test_normalize(b, 0, 1, 0, 0.01f, 0)
74 || test_normalize(b, 0, 1, 0, 0.001f, 1)
75 || test_normalize(b, 0, 1, 0, 0.002f, 2)
76 || test_normalize(b, 0, 1, 1, 0.01f, 0)
77 || test_normalize(b, 0, 1, 1, 0.001f, 1)
78 || test_normalize(b, 0, 1, 1, 0.002f, 2);
79}
80
81static int test_normalize_2()
82{
83 ncnn::Mat a = RandomMat(2, 3, 5);
84 ncnn::Mat b = RandomMat(4, 6, 8);
85
86 return 0
87 || test_normalize(a, 1, 1, 0, 0.01f, 0)
88 || test_normalize(a, 1, 1, 0, 0.001f, 1)
89 || test_normalize(a, 1, 1, 0, 0.002f, 2)
90 || test_normalize(a, 1, 1, 1, 0.01f, 0)
91 || test_normalize(a, 1, 1, 1, 0.001f, 1)
92 || test_normalize(a, 1, 1, 1, 0.002f, 2)
93 || test_normalize(b, 1, 1, 0, 0.01f, 0)
94 || test_normalize(b, 1, 1, 0, 0.001f, 1)
95 || test_normalize(b, 1, 1, 0, 0.002f, 2)
96 || test_normalize(b, 1, 1, 1, 0.01f, 0)
97 || test_normalize(b, 1, 1, 1, 0.001f, 1)
98 || test_normalize(b, 1, 1, 1, 0.002f, 2);
99}
100
101int main()
102{
103 SRAND(7767517);
104
105 return 0
106 || test_normalize_0()
107 || test_normalize_1()
108 || test_normalize_2();
109}