| 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 | |
| 17 | static int test_batchnorm(const ncnn::Mat& a, float eps) |
| 18 | { |
| 19 | int channels; |
| 20 | if (a.dims == 1) channels = a.w; |
| 21 | if (a.dims == 2) channels = a.h; |
| 22 | if (a.dims == 3 || a.dims == 4) channels = a.c; |
| 23 | |
| 24 | ncnn::ParamDict pd; |
| 25 | pd.set(0, channels); // channels |
| 26 | pd.set(1, eps); // eps |
| 27 | |
| 28 | std::vector<ncnn::Mat> weights(4); |
| 29 | weights[0] = RandomMat(channels); |
| 30 | weights[1] = RandomMat(channels); |
| 31 | weights[2] = RandomMat(channels); |
| 32 | weights[3] = RandomMat(channels); |
| 33 | |
| 34 | // var must be positive |
| 35 | Randomize(weights[2], 0.001f, 2.f); |
| 36 | |
| 37 | int ret = test_layer("BatchNorm", pd, weights, a); |
| 38 | if (ret != 0) |
| 39 | { |
| 40 | fprintf(stderr, "test_batchnorm failed a.dims=%d a=(%d %d %d %d) eps=%f\n", a.dims, a.w, a.h, a.d, a.c, eps); |
| 41 | } |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static int test_batchnorm_0() |
| 47 | { |
| 48 | return 0 |
| 49 | || test_batchnorm(RandomMat(5, 6, 7, 24), 0.f) |
| 50 | || test_batchnorm(RandomMat(5, 6, 7, 24), 0.01f) |
| 51 | || test_batchnorm(RandomMat(7, 8, 9, 12), 0.f) |
| 52 | || test_batchnorm(RandomMat(7, 8, 9, 12), 0.001f) |
| 53 | || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f) |
| 54 | || test_batchnorm(RandomMat(3, 4, 5, 13), 0.f) |
| 55 | || test_batchnorm(RandomMat(3, 4, 6, 32), 0.f) |
| 56 | || test_batchnorm(RandomMat(3, 4, 5, 32), 0.001f); |
| 57 | } |
| 58 | |
| 59 | static int test_batchnorm_1() |
| 60 | { |
| 61 | return 0 |
| 62 | || test_batchnorm(RandomMat(5, 7, 24), 0.f) |
| 63 | || test_batchnorm(RandomMat(5, 7, 24), 0.01f) |
| 64 | || test_batchnorm(RandomMat(7, 9, 12), 0.f) |
| 65 | || test_batchnorm(RandomMat(7, 9, 12), 0.001f) |
| 66 | || test_batchnorm(RandomMat(3, 5, 13), 0.f) |
| 67 | || test_batchnorm(RandomMat(3, 5, 13), 0.001f) |
| 68 | || test_batchnorm(RandomMat(3, 5, 16), 0.001f) |
| 69 | || test_batchnorm(RandomMat(3, 5, 32), 0.001f); |
| 70 | } |
| 71 | |
| 72 | static int test_batchnorm_2() |
| 73 | { |
| 74 | return 0 |
| 75 | || test_batchnorm(RandomMat(15, 24), 0.f) |
| 76 | || test_batchnorm(RandomMat(15, 24), 0.01f) |
| 77 | || test_batchnorm(RandomMat(17, 12), 0.f) |
| 78 | || test_batchnorm(RandomMat(17, 12), 0.001f) |
| 79 | || test_batchnorm(RandomMat(19, 15), 0.f) |
| 80 | || test_batchnorm(RandomMat(19, 15), 0.001f) |
| 81 | || test_batchnorm(RandomMat(128, 16), 0.f) |
| 82 | || test_batchnorm(RandomMat(16, 128), 0.001f); |
| 83 | } |
| 84 | |
| 85 | static int test_batchnorm_3() |
| 86 | { |
| 87 | return 0 |
| 88 | || test_batchnorm(RandomMat(128), 0.f) |
| 89 | || test_batchnorm(RandomMat(128), 0.001f) |
| 90 | || test_batchnorm(RandomMat(124), 0.f) |
| 91 | || test_batchnorm(RandomMat(124), 0.1f) |
| 92 | || test_batchnorm(RandomMat(127), 0.f) |
| 93 | || test_batchnorm(RandomMat(127), 0.1f); |
| 94 | } |
| 95 | |
| 96 | int main() |
| 97 | { |
| 98 | SRAND(7767517); |
| 99 | |
| 100 | return 0 |
| 101 | || test_batchnorm_0() |
| 102 | || test_batchnorm_1() |
| 103 | || test_batchnorm_2() |
| 104 | || test_batchnorm_3(); |
| 105 | } |