| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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_squeeze(const ncnn::Mat& a, int squeeze_w, int squeeze_h, int squeeze_d, int squeeze_c) |
| 18 | { |
| 19 | ncnn::ParamDict pd; |
| 20 | pd.set(0, squeeze_w); |
| 21 | pd.set(1, squeeze_h); |
| 22 | pd.set(11, squeeze_d); |
| 23 | pd.set(2, squeeze_c); |
| 24 | |
| 25 | std::vector<ncnn::Mat> weights(0); |
| 26 | |
| 27 | int ret = test_layer("Squeeze", pd, weights, a); |
| 28 | if (ret != 0) |
| 29 | { |
| 30 | fprintf(stderr, "test_squeeze failed a.dims=%d a=(%d %d %d %d) squeeze_w=%d squeeze_h=%d squeeze_d=%d squeeze_c=%d\n", a.dims, a.w, a.h, a.d, a.c, squeeze_w, squeeze_h, squeeze_d, squeeze_c); |
| 31 | } |
| 32 | |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | static ncnn::Mat IntArrayMat(int a0) |
| 37 | { |
| 38 | ncnn::Mat m(1); |
| 39 | int* p = m; |
| 40 | p[0] = a0; |
| 41 | return m; |
| 42 | } |
| 43 | |
| 44 | static ncnn::Mat IntArrayMat(int a0, int a1) |
| 45 | { |
| 46 | ncnn::Mat m(2); |
| 47 | int* p = m; |
| 48 | p[0] = a0; |
| 49 | p[1] = a1; |
| 50 | return m; |
| 51 | } |
| 52 | |
| 53 | static ncnn::Mat IntArrayMat(int a0, int a1, int a2) |
| 54 | { |
| 55 | ncnn::Mat m(3); |
| 56 | int* p = m; |
| 57 | p[0] = a0; |
| 58 | p[1] = a1; |
| 59 | p[2] = a2; |
| 60 | return m; |
| 61 | } |
| 62 | |
| 63 | static ncnn::Mat IntArrayMat(int a0, int a1, int a2, int a3) |
| 64 | { |
| 65 | ncnn::Mat m(4); |
| 66 | int* p = m; |
| 67 | p[0] = a0; |
| 68 | p[1] = a1; |
| 69 | p[2] = a2; |
| 70 | p[3] = a3; |
| 71 | return m; |
| 72 | } |
| 73 | |
| 74 | static void print_int_array(const ncnn::Mat& a) |
| 75 | { |
| 76 | const int* pa = a; |
| 77 | |
| 78 | fprintf(stderr, "["); |
| 79 | for (int i = 0; i < a.w; i++) |
| 80 | { |
| 81 | fprintf(stderr, " %d", pa[i]); |
| 82 | } |
| 83 | fprintf(stderr, " ]"); |
| 84 | } |
| 85 | |
| 86 | static int test_squeeze_axes(const ncnn::Mat& a, const ncnn::Mat& axes) |
| 87 | { |
| 88 | ncnn::ParamDict pd; |
| 89 | pd.set(3, axes); |
| 90 | |
| 91 | std::vector<ncnn::Mat> weights(0); |
| 92 | |
| 93 | int ret = test_layer("Squeeze", pd, weights, a); |
| 94 | if (ret != 0) |
| 95 | { |
| 96 | fprintf(stderr, "test_squeeze_axes failed a.dims=%d a=(%d %d %d %d)\n", a.dims, a.w, a.h, a.d, a.c); |
| 97 | fprintf(stderr, " axes="); |
| 98 | print_int_array(axes); |
| 99 | fprintf(stderr, "\n"); |
| 100 | } |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | static int test_squeeze_all_params(const ncnn::Mat& a) |
| 106 | { |
| 107 | return 0 |
| 108 | || test_squeeze(a, 0, 0, 0, 0) |
| 109 | || test_squeeze(a, 0, 0, 0, 1) |
| 110 | || test_squeeze(a, 0, 0, 1, 0) |
| 111 | || test_squeeze(a, 0, 0, 1, 1) |
| 112 | || test_squeeze(a, 0, 1, 0, 0) |
| 113 | || test_squeeze(a, 0, 1, 0, 1) |
| 114 | || test_squeeze(a, 0, 1, 1, 0) |
| 115 | || test_squeeze(a, 0, 1, 1, 1) |
| 116 | || test_squeeze(a, 1, 0, 0, 0) |
| 117 | || test_squeeze(a, 1, 0, 0, 1) |
| 118 | || test_squeeze(a, 1, 0, 1, 0) |
| 119 | || test_squeeze(a, 1, 0, 1, 1) |
| 120 | || test_squeeze(a, 1, 1, 0, 0) |
| 121 | || test_squeeze(a, 1, 1, 0, 1) |
| 122 | || test_squeeze(a, 1, 1, 1, 0) |
| 123 | || test_squeeze(a, 1, 1, 1, 1) |
| 124 | |
| 125 | || test_squeeze_axes(a, IntArrayMat(0)) |
| 126 | || test_squeeze_axes(a, IntArrayMat(1)) |
| 127 | || test_squeeze_axes(a, IntArrayMat(2)) |
| 128 | || test_squeeze_axes(a, IntArrayMat(3)) |
| 129 | || test_squeeze_axes(a, IntArrayMat(0, 1)) |
| 130 | || test_squeeze_axes(a, IntArrayMat(0, 2)) |
| 131 | || test_squeeze_axes(a, IntArrayMat(0, 3)) |
| 132 | || test_squeeze_axes(a, IntArrayMat(1, 2)) |
| 133 | || test_squeeze_axes(a, IntArrayMat(1, 3)) |
| 134 | || test_squeeze_axes(a, IntArrayMat(2, 3)) |
| 135 | || test_squeeze_axes(a, IntArrayMat(0, 1, 2)) |
| 136 | || test_squeeze_axes(a, IntArrayMat(0, 1, 3)) |
| 137 | || test_squeeze_axes(a, IntArrayMat(0, 2, 3)) |
| 138 | || test_squeeze_axes(a, IntArrayMat(1, 2, 3)) |
| 139 | || test_squeeze_axes(a, IntArrayMat(0, 1, 2, 3)); |
| 140 | } |
| 141 | |
| 142 | static int test_squeeze_0() |
| 143 | { |
| 144 | return 0 |
| 145 | || test_squeeze_all_params(RandomMat(4, 5, 7, 16)) |
| 146 | || test_squeeze_all_params(RandomMat(4, 5, 1, 15)) |
| 147 | || test_squeeze_all_params(RandomMat(4, 1, 7, 12)) |
| 148 | || test_squeeze_all_params(RandomMat(1, 5, 7, 16)) |
| 149 | || test_squeeze_all_params(RandomMat(1, 5, 1, 15)) |
| 150 | || test_squeeze_all_params(RandomMat(1, 1, 7, 12)) |
| 151 | || test_squeeze_all_params(RandomMat(6, 1, 1, 16)) |
| 152 | || test_squeeze_all_params(RandomMat(1, 1, 1, 15)) |
| 153 | || test_squeeze_all_params(RandomMat(4, 5, 7, 1)) |
| 154 | || test_squeeze_all_params(RandomMat(4, 5, 1, 1)) |
| 155 | || test_squeeze_all_params(RandomMat(4, 1, 7, 1)) |
| 156 | || test_squeeze_all_params(RandomMat(1, 5, 7, 1)) |
| 157 | || test_squeeze_all_params(RandomMat(1, 5, 1, 1)) |
| 158 | || test_squeeze_all_params(RandomMat(1, 1, 7, 1)) |
| 159 | || test_squeeze_all_params(RandomMat(1, 1, 1, 1)); |
| 160 | } |
| 161 | |
| 162 | static int test_squeeze_1() |
| 163 | { |
| 164 | return 0 |
| 165 | || test_squeeze_all_params(RandomMat(3, 12, 16)) |
| 166 | || test_squeeze_all_params(RandomMat(3, 1, 16)) |
| 167 | || test_squeeze_all_params(RandomMat(1, 33, 15)) |
| 168 | || test_squeeze_all_params(RandomMat(1, 14, 1)) |
| 169 | || test_squeeze_all_params(RandomMat(12, 13, 1)) |
| 170 | || test_squeeze_all_params(RandomMat(1, 1, 1)); |
| 171 | } |
| 172 | |
| 173 | static int test_squeeze_2() |
| 174 | { |
| 175 | return 0 |
| 176 | || test_squeeze_all_params(RandomMat(14, 16)) |
| 177 | || test_squeeze_all_params(RandomMat(1, 14)) |
| 178 | || test_squeeze_all_params(RandomMat(11, 1)) |
| 179 | || test_squeeze_all_params(RandomMat(1, 1)); |
| 180 | } |
| 181 | |
| 182 | static int test_squeeze_3() |
| 183 | { |
| 184 | return 0 |
| 185 | || test_squeeze_all_params(RandomMat(120)) |
| 186 | || test_squeeze_all_params(RandomMat(1)); |
| 187 | } |
| 188 | |
| 189 | int main() |
| 190 | { |
| 191 | SRAND(7767517); |
| 192 | |
| 193 | return test_squeeze_0() || test_squeeze_1() || test_squeeze_2() || test_squeeze_3(); |
| 194 | } |