| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "deconvolution.h" |
| 16 | |
| 17 | #include "fused_activation.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | Deconvolution::Deconvolution() |
| 22 | { |
| 23 | one_blob_only = true; |
| 24 | support_inplace = false; |
| 25 | } |
| 26 | |
| 27 | int Deconvolution::load_param(const ParamDict& pd) |
| 28 | { |
| 29 | num_output = pd.get(0, 0); |
| 30 | kernel_w = pd.get(1, 0); |
| 31 | kernel_h = pd.get(11, kernel_w); |
| 32 | dilation_w = pd.get(2, 1); |
| 33 | dilation_h = pd.get(12, dilation_w); |
| 34 | stride_w = pd.get(3, 1); |
| 35 | stride_h = pd.get(13, stride_w); |
| 36 | pad_left = pd.get(4, 0); |
| 37 | pad_right = pd.get(15, pad_left); |
| 38 | pad_top = pd.get(14, pad_left); |
| 39 | pad_bottom = pd.get(16, pad_top); |
| 40 | output_pad_right = pd.get(18, 0); |
| 41 | output_pad_bottom = pd.get(19, output_pad_right); |
| 42 | output_w = pd.get(20, 0); |
| 43 | output_h = pd.get(21, output_w); |
| 44 | bias_term = pd.get(5, 0); |
| 45 | weight_data_size = pd.get(6, 0); |
| 46 | activation_type = pd.get(9, 0); |
| 47 | activation_params = pd.get(10, Mat()); |
| 48 | |
| 49 | dynamic_weight = pd.get(28, 0); |
| 50 | |
| 51 | if (dynamic_weight) |
| 52 | { |
| 53 | one_blob_only = false; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int Deconvolution::load_model(const ModelBin& mb) |
| 60 | { |
| 61 | if (dynamic_weight) |
| 62 | return 0; |
| 63 | |
| 64 | weight_data = mb.load(weight_data_size, 0); |
| 65 | if (weight_data.empty()) |
| 66 | return -100; |
| 67 | |
| 68 | if (bias_term) |
| 69 | { |
| 70 | bias_data = mb.load(num_output, 1); |
| 71 | if (bias_data.empty()) |
| 72 | return -100; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int deconvolution(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int stride_w, int stride_h, int dilation_w, int dilation_h, int activation_type, const Mat& activation_params, const Option& opt) |
| 79 | { |
| 80 | const int outw = top_blob.w; |
| 81 | const int outch = top_blob.c; |
| 82 | |
| 83 | const int maxk = kernel_w * kernel_h; |
| 84 | |
| 85 | // kernel offsets |
| 86 | std::vector<int> _space_ofs(maxk); |
| 87 | int* space_ofs = &_space_ofs[0]; |
| 88 | { |
| 89 | int p1 = 0; |
| 90 | int p2 = 0; |
| 91 | int gap = outw * dilation_h - kernel_w * dilation_w; |
| 92 | for (int i = 0; i < kernel_h; i++) |
| 93 | { |
| 94 | for (int j = 0; j < kernel_w; j++) |
| 95 | { |
| 96 | space_ofs[p1] = p2; |
| 97 | p1++; |
| 98 | p2 += dilation_w; |
| 99 | } |
| 100 | p2 += gap; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | #pragma omp parallel for num_threads(opt.num_threads) |
| 105 | for (int p = 0; p < outch; p++) |
| 106 | { |
| 107 | Mat out = top_blob.channel(p); |
| 108 | |
| 109 | const float bias = bias_data.empty() ? 0.f : bias_data[p]; |
| 110 | |
| 111 | out.fill(bias); |
| 112 | |
| 113 | // shadowed variable for less openmp task args |
| 114 | const int w = bottom_blob.w; |
| 115 | const int h = bottom_blob.h; |
| 116 | const int inch = bottom_blob.c; |
| 117 | const int outw = top_blob.w; |
| 118 | const int outh = top_blob.h; |
| 119 | |
| 120 | for (int i = 0; i < h; i++) |
| 121 | { |
| 122 | for (int j = 0; j < w; j++) |
| 123 | { |
| 124 | float* outptr = out.row(i * stride_h) + j * stride_w; |
| 125 | |
| 126 | const float* kptr = (const float*)weight_data + maxk * inch * p; |
| 127 | |
| 128 | for (int q = 0; q < inch; q++) |
| 129 | { |
| 130 | const float val = bottom_blob.channel(q).row(i)[j]; |
| 131 | |
| 132 | for (int k = 0; k < maxk; k++) |
| 133 | { |
| 134 | float w = kptr[k]; |
| 135 | outptr[space_ofs[k]] += val * w; |
| 136 | } |
| 137 | |
| 138 | kptr += maxk; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | { |
| 144 | float* outptr = out; |
| 145 | int size = outw * outh; |
| 146 | |
| 147 | for (int i = 0; i < size; i++) |
| 148 | { |
| 149 | outptr[i] = activation_ss(outptr[i], activation_type, activation_params); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int Deconvolution::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 158 | { |
| 159 | int w = bottom_blob.w; |
| 160 | int h = bottom_blob.h; |
| 161 | size_t elemsize = bottom_blob.elemsize; |
| 162 | |
| 163 | const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1; |
| 164 | const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1; |
| 165 | |
| 166 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 167 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 168 | |
| 169 | Mat top_blob_bordered; |
| 170 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 171 | { |
| 172 | top_blob_bordered.create(outw, outh, num_output, elemsize, opt.workspace_allocator); |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | top_blob_bordered = top_blob; |
| 177 | top_blob_bordered.create(outw, outh, num_output, elemsize, opt.blob_allocator); |
| 178 | } |
| 179 | if (top_blob_bordered.empty()) |
| 180 | return -100; |
| 181 | |
| 182 | int ret = deconvolution(bottom_blob, top_blob_bordered, weight_data, bias_data, kernel_w, kernel_h, stride_w, stride_h, dilation_w, dilation_h, activation_type, activation_params, opt); |
| 183 | if (ret != 0) |
| 184 | return ret; |
| 185 | |
| 186 | cut_padding(top_blob_bordered, top_blob, opt); |
| 187 | if (top_blob.empty()) |
| 188 | return -100; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | int Deconvolution::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 194 | { |
| 195 | const Mat& bottom_blob = bottom_blobs[0]; |
| 196 | const Mat& _weight_data = bottom_blobs[1]; |
| 197 | Mat& top_blob = top_blobs[0]; |
| 198 | |
| 199 | const int _num_input = bottom_blob.c; |
| 200 | const int _kernel_w = _weight_data.w; |
| 201 | const int _kernel_h = _weight_data.h; |
| 202 | const int _num_output = _weight_data.d * 1; |
| 203 | |
| 204 | Mat weight_data_flattened; |
| 205 | flatten(_weight_data, weight_data_flattened, opt); |
| 206 | if (weight_data_flattened.empty()) |
| 207 | return -100; |
| 208 | |
| 209 | // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw |
| 210 | Mat weight_data_transposed; |
| 211 | { |
| 212 | weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / 1, 4u, opt.workspace_allocator); |
| 213 | if (weight_data_transposed.empty()) |
| 214 | return -100; |
| 215 | |
| 216 | const int outch_g = _num_output / 1; |
| 217 | const int inch_g = _num_input / 1; |
| 218 | const int maxk = _kernel_h * _kernel_w; |
| 219 | |
| 220 | for (int g = 0; g < 1; g++) |
| 221 | { |
| 222 | // reorder weight from inch-outch to outch-inch |
| 223 | float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk; |
| 224 | const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk; |
| 225 | for (int i = 0; i < outch_g; i++) |
| 226 | { |
| 227 | for (int j = 0; j < inch_g; j++) |
| 228 | { |
| 229 | for (int k = 0; k < maxk; k++) |
| 230 | { |
| 231 | wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k]; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | Mat bias_data_flattened; |
| 239 | if (bias_term) |
| 240 | { |
| 241 | const Mat& _bias_data = bottom_blobs[2]; |
| 242 | flatten(_bias_data, bias_data_flattened, opt); |
| 243 | if (bias_data_flattened.empty()) |
| 244 | return -100; |
| 245 | } |
| 246 | |
| 247 | const int w = bottom_blob.w; |
| 248 | const int h = bottom_blob.h; |
| 249 | |
| 250 | const int kernel_extent_w = dilation_w * (_kernel_w - 1) + 1; |
| 251 | const int kernel_extent_h = dilation_h * (_kernel_h - 1) + 1; |
| 252 | |
| 253 | int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right; |
| 254 | int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom; |
| 255 | |
| 256 | Mat top_blob_bordered; |
| 257 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0)) |
| 258 | { |
| 259 | top_blob_bordered.create(outw, outh, _num_output, 4u, opt.workspace_allocator); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | top_blob_bordered = top_blob; |
| 264 | top_blob_bordered.create(outw, outh, _num_output, 4u, opt.blob_allocator); |
| 265 | } |
| 266 | if (top_blob_bordered.empty()) |
| 267 | return -100; |
| 268 | |
| 269 | int ret = deconvolution(bottom_blob, top_blob_bordered, weight_data_transposed, bias_data_flattened, _kernel_w, _kernel_h, stride_w, stride_h, dilation_w, dilation_h, activation_type, activation_params, opt); |
| 270 | if (ret != 0) |
| 271 | return ret; |
| 272 | |
| 273 | cut_padding(top_blob_bordered, top_blob, opt); |
| 274 | if (top_blob.empty()) |
| 275 | return -100; |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | void Deconvolution::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const |
| 281 | { |
| 282 | if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) |
| 283 | { |
| 284 | copy_cut_border(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt); |
| 285 | } |
| 286 | else if (output_w > 0 && output_h > 0) |
| 287 | { |
| 288 | int wcut = top_blob_bordered.w - output_w; |
| 289 | int hcut = top_blob_bordered.h - output_h; |
| 290 | |
| 291 | if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233) |
| 292 | { |
| 293 | // onnx padding=SAME_UPPER |
| 294 | copy_cut_border(top_blob_bordered, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, opt); |
| 295 | } |
| 296 | else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234) |
| 297 | { |
| 298 | // onnx padding=SAME_LOWER |
| 299 | copy_cut_border(top_blob_bordered, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, opt); |
| 300 | } |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | top_blob = top_blob_bordered; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | } // namespace ncnn |