| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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_deconvolution(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_pad_bottom, int output_w, int output_h) |
| 18 | { |
| 19 | ncnn::Mat a = RandomMat(w, h, c); |
| 20 | |
| 21 | if (output_w > 0 && output_h > 0 && pad != -233 && pad != -234) |
| 22 | { |
| 23 | pad = -233; |
| 24 | } |
| 25 | |
| 26 | ncnn::ParamDict pd; |
| 27 | pd.set(0, outch); // num_output |
| 28 | pd.set(1, kernel); // kernel_w |
| 29 | pd.set(2, dilation); // dilation_w |
| 30 | pd.set(3, stride); // stride_w |
| 31 | pd.set(4, pad); // pad_w |
| 32 | pd.set(5, bias); // bias_term |
| 33 | pd.set(6, outch * c * kernel * kernel); |
| 34 | |
| 35 | int activation_type = RAND() % 5; // 0 1 2 3 4 |
| 36 | ncnn::Mat activation_params(2); |
| 37 | activation_params[0] = RandomFloat(-1, 0); // alpha |
| 38 | activation_params[1] = RandomFloat(0, 1); // beta |
| 39 | pd.set(9, activation_type); |
| 40 | pd.set(10, activation_params); |
| 41 | |
| 42 | pd.set(18, output_pad_right); |
| 43 | pd.set(19, output_pad_bottom); |
| 44 | pd.set(20, output_w); |
| 45 | pd.set(21, output_h); |
| 46 | |
| 47 | std::vector<ncnn::Mat> weights(2); |
| 48 | weights[0] = RandomMat(outch * c * kernel * kernel); |
| 49 | weights[1] = RandomMat(outch); |
| 50 | |
| 51 | int ret = test_layer("Deconvolution", pd, weights, a); |
| 52 | if (ret != 0) |
| 53 | { |
| 54 | fprintf(stderr, "test_deconvolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h); |
| 55 | } |
| 56 | |
| 57 | { |
| 58 | ncnn::Option opt; |
| 59 | opt.num_threads = 1; |
| 60 | opt.use_packing_layout = true; |
| 61 | opt.use_fp16_packed = false; |
| 62 | opt.use_fp16_storage = false; |
| 63 | opt.use_fp16_arithmetic = false; |
| 64 | opt.use_bf16_storage = false; |
| 65 | opt.use_shader_pack8 = false; |
| 66 | opt.use_image_storage = false; |
| 67 | opt.use_sgemm_convolution = false; |
| 68 | opt.use_winograd_convolution = false; |
| 69 | |
| 70 | ret = test_layer_opt("Deconvolution", pd, weights, opt, a); |
| 71 | if (ret != 0) |
| 72 | { |
| 73 | fprintf(stderr, "test_deconvolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | ncnn::Option opt; |
| 79 | opt.num_threads = 1; |
| 80 | opt.use_packing_layout = true; |
| 81 | opt.use_fp16_packed = true; |
| 82 | opt.use_fp16_storage = true; |
| 83 | opt.use_fp16_arithmetic = true; |
| 84 | opt.use_bf16_storage = true; |
| 85 | opt.use_shader_pack8 = true; |
| 86 | opt.use_image_storage = true; |
| 87 | opt.use_sgemm_convolution = false; |
| 88 | opt.use_winograd_convolution = false; |
| 89 | |
| 90 | ret = test_layer_opt("Deconvolution", pd, weights, opt, a); |
| 91 | if (ret != 0) |
| 92 | { |
| 93 | fprintf(stderr, "test_deconvolution failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int test_deconvolution_0() |
| 101 | { |
| 102 | static const int kdsp[16][4] = { |
| 103 | {1, 1, 1, 0}, |
| 104 | {1, 1, 2, 0}, |
| 105 | {2, 1, 1, 1}, |
| 106 | {2, 1, 2, -233}, |
| 107 | {3, 1, 1, 1}, |
| 108 | {3, 1, 2, 1}, |
| 109 | {3, 2, 1, 1}, |
| 110 | {4, 1, 1, -233}, |
| 111 | {4, 1, 2, -234}, |
| 112 | {4, 2, 1, -234}, |
| 113 | {5, 1, 1, 2}, |
| 114 | {5, 1, 2, 2}, |
| 115 | {5, 2, 2, 2}, |
| 116 | {7, 1, 1, 3}, |
| 117 | {7, 1, 2, 3}, |
| 118 | {7, 2, 1, -233}, |
| 119 | }; |
| 120 | |
| 121 | for (int i = 0; i < 16; i++) |
| 122 | { |
| 123 | const int k = kdsp[i][0]; |
| 124 | const int d = kdsp[i][1]; |
| 125 | const int s = kdsp[i][2]; |
| 126 | const int p = kdsp[i][3]; |
| 127 | |
| 128 | int ret = 0 |
| 129 | || test_deconvolution(9, 7, 1, 1, k, d, s, p, 1, 0, 0, 0, 0) |
| 130 | || test_deconvolution(9, 7, 4, 13, k, d, s, p, 0, 1, 1, 7, 5) |
| 131 | || test_deconvolution(9, 7, 13, 4, k, d, s, p, 1, 1, 0, 0, 0) |
| 132 | || test_deconvolution(9, 7, 4, 8, k, d, s, p, 0, 0, 1, 0, 0) |
| 133 | || test_deconvolution(9, 7, 8, 4, k, d, s, p, 1, 0, 0, 7, 5) |
| 134 | || test_deconvolution(7, 7, 12, 12, k, d, s, p, 1, 0, 1, 0, 0) |
| 135 | || test_deconvolution(4, 5, 12, 11, k, d, s, p, 0, 0, 1, 1, 0) |
| 136 | || test_deconvolution(9, 7, 8, 13, k, d, s, p, 0, 2, 2, 0, 0) |
| 137 | || test_deconvolution(9, 7, 13, 8, k, d, s, p, 1, 2, 0, 0, 0) |
| 138 | || test_deconvolution(9, 7, 16, 16, k, d, s, p, 0, 0, 2, 7, 5); |
| 139 | |
| 140 | if (ret != 0) |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | return 0 |
| 145 | || test_deconvolution(7, 5, 24, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0) |
| 146 | || test_deconvolution(7, 5, 32, 24, 4, 2, 2, 2, 1, 0, 0, 0, 0) |
| 147 | || test_deconvolution(7, 5, 28, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0) |
| 148 | || test_deconvolution(7, 5, 32, 28, 4, 2, 2, 2, 1, 0, 0, 0, 0) |
| 149 | || test_deconvolution(7, 5, 26, 32, 4, 2, 2, 2, 1, 0, 0, 0, 0) |
| 150 | || test_deconvolution(7, 5, 32, 26, 4, 2, 2, 2, 1, 0, 0, 0, 0); |
| 151 | } |
| 152 | |
| 153 | static int test_deconvolution_dynamic(int w, int h, int c, int outch, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_pad_bottom, int output_w, int output_h) |
| 154 | { |
| 155 | ncnn::Mat a = RandomMat(w, h, c); |
| 156 | |
| 157 | if (output_w > 0 && output_h > 0 && pad != -233 && pad != -234) |
| 158 | { |
| 159 | pad = -233; |
| 160 | } |
| 161 | |
| 162 | ncnn::ParamDict pd; |
| 163 | pd.set(0, 0); |
| 164 | pd.set(1, 0); |
| 165 | pd.set(2, dilation); |
| 166 | pd.set(3, stride); |
| 167 | pd.set(4, pad); |
| 168 | pd.set(5, bias); |
| 169 | pd.set(6, 0); |
| 170 | pd.set(28, 1); // dynamic weight |
| 171 | |
| 172 | int activation_type = RAND() % 7; // 0 1 2 3 4 5 6 |
| 173 | ncnn::Mat activation_params(2); |
| 174 | activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha |
| 175 | activation_params[1] = RandomFloat(0, 1); // beta |
| 176 | pd.set(9, activation_type); |
| 177 | pd.set(10, activation_params); |
| 178 | |
| 179 | pd.set(18, output_pad_right); |
| 180 | pd.set(19, output_pad_bottom); |
| 181 | pd.set(20, output_w); |
| 182 | pd.set(21, output_h); |
| 183 | |
| 184 | std::vector<ncnn::Mat> as(bias ? 3 : 2); |
| 185 | as[0] = a; |
| 186 | as[1] = RandomMat(kernel, kernel, outch, c); |
| 187 | if (bias) |
| 188 | as[2] = RandomMat(outch); |
| 189 | |
| 190 | std::vector<ncnn::Mat> weights(0); |
| 191 | |
| 192 | int ret = test_layer("Deconvolution", pd, weights, as); |
| 193 | if (ret != 0) |
| 194 | { |
| 195 | fprintf(stderr, "test_deconvolution_dynamic failed w=%d h=%d c=%d outch=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_pad_bottom=%d output_w=%d output_h=%d\n", w, h, c, outch, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_pad_bottom, output_w, output_h); |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int test_deconvolution_1() |
| 202 | { |
| 203 | static const int kdsp[7][4] = { |
| 204 | {1, 1, 1, 0}, |
| 205 | {1, 1, 2, 0}, |
| 206 | {2, 1, 1, 1}, |
| 207 | {2, 1, 2, -233}, |
| 208 | {3, 1, 1, 1}, |
| 209 | {3, 1, 2, 1}, |
| 210 | {3, 2, 1, -234}, |
| 211 | }; |
| 212 | |
| 213 | for (int i = 0; i < 7; i++) |
| 214 | { |
| 215 | const int k = kdsp[i][0]; |
| 216 | const int d = kdsp[i][1]; |
| 217 | const int s = kdsp[i][2]; |
| 218 | const int p = kdsp[i][3]; |
| 219 | |
| 220 | int ret = 0 |
| 221 | || test_deconvolution_dynamic(9, 7, 1, 1, k, d, s, p, 1, 0, 0, 0, 0) |
| 222 | || test_deconvolution_dynamic(9, 7, 4, 13, k, d, s, p, 0, 1, 1, 7, 5) |
| 223 | || test_deconvolution_dynamic(9, 7, 13, 4, k, d, s, p, 1, 1, 0, 0, 0) |
| 224 | || test_deconvolution_dynamic(9, 7, 4, 8, k, d, s, p, 0, 0, 1, 0, 0) |
| 225 | || test_deconvolution_dynamic(9, 7, 8, 4, k, d, s, p, 1, 0, 0, 7, 5) |
| 226 | || test_deconvolution_dynamic(7, 7, 12, 12, k, d, s, p, 1, 0, 1, 0, 0) |
| 227 | || test_deconvolution_dynamic(4, 5, 12, 11, k, d, s, p, 0, 0, 1, 1, 0) |
| 228 | || test_deconvolution_dynamic(9, 7, 8, 13, k, d, s, p, 0, 2, 2, 0, 0) |
| 229 | || test_deconvolution_dynamic(9, 7, 13, 8, k, d, s, p, 1, 2, 0, 0, 0) |
| 230 | || test_deconvolution_dynamic(9, 7, 16, 16, k, d, s, p, 0, 0, 2, 7, 5); |
| 231 | |
| 232 | if (ret != 0) |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | int main() |
| 240 | { |
| 241 | SRAND(7767517); |
| 242 | |
| 243 | return test_deconvolution_0() || test_deconvolution_1(); |
| 244 | } |