1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
15
#include "deconvolution3d.h"
17
#include "fused_activation.h"
21
Deconvolution3D::Deconvolution3D()
24
support_inplace = false;
27
int Deconvolution3D::load_param(const ParamDict& pd)
29
num_output = pd.get(0, 0);
30
kernel_w = pd.get(1, 0);
31
kernel_h = pd.get(11, kernel_w);
32
kernel_d = pd.get(21, kernel_w);
33
dilation_w = pd.get(2, 1);
34
dilation_h = pd.get(12, dilation_w);
35
dilation_d = pd.get(22, dilation_w);
36
stride_w = pd.get(3, 1);
37
stride_h = pd.get(13, stride_w);
38
stride_d = pd.get(23, stride_w);
39
pad_left = pd.get(4, 0);
40
pad_right = pd.get(15, pad_left);
41
pad_top = pd.get(14, pad_left);
42
pad_bottom = pd.get(16, pad_top);
43
pad_front = pd.get(24, pad_left);
44
pad_behind = pd.get(17, pad_front);
45
output_pad_right = pd.get(18, 0);
46
output_pad_bottom = pd.get(19, output_pad_right);
47
output_pad_behind = pd.get(20, output_pad_right);
48
output_w = pd.get(25, 0);
49
output_h = pd.get(26, output_w);
50
output_d = pd.get(27, output_w);
51
bias_term = pd.get(5, 0);
52
weight_data_size = pd.get(6, 0);
53
activation_type = pd.get(9, 0);
54
activation_params = pd.get(10, Mat());
59
int Deconvolution3D::load_model(const ModelBin& mb)
61
weight_data = mb.load(weight_data_size, 0);
62
if (weight_data.empty())
67
bias_data = mb.load(num_output, 1);
68
if (bias_data.empty())
75
static int deconvolution3d(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data, const Mat& bias_data, int kernel_w, int kernel_h, int kernel_d, int stride_w, int stride_h, int stride_d, int dilation_w, int dilation_h, int dilation_d, int activation_type, const Mat& activation_params, const Option& opt)
77
const int outw = top_blob.w;
78
const int outh = top_blob.h;
79
const int outch = top_blob.c;
81
const int maxk = kernel_w * kernel_h * kernel_d;
84
std::vector<int> _space_ofs(maxk);
85
int* space_ofs = &_space_ofs[0];
89
int gap0 = outw * dilation_h - kernel_w * dilation_w;
90
int gap1 = outh * outw * dilation_d - outw * kernel_h * dilation_h;
91
for (int z = 0; z < kernel_d; z++)
93
for (int i = 0; i < kernel_h; i++)
95
for (int j = 0; j < kernel_w; j++)
107
#pragma omp parallel for num_threads(opt.num_threads)
108
for (int p = 0; p < outch; p++)
110
Mat out = top_blob.channel(p);
112
const float bias = bias_data.empty() ? 0.f : bias_data[p];
116
// shadowed variable for less openmp task args
117
const int w = bottom_blob.w;
118
const int h = bottom_blob.h;
119
const int d = bottom_blob.d;
120
const int inch = bottom_blob.c;
121
const int outw = top_blob.w;
122
const int outh = top_blob.h;
123
const int outd = top_blob.d;
125
for (int z = 0; z < d; z++)
127
for (int i = 0; i < h; i++)
129
for (int j = 0; j < w; j++)
131
float* outptr = out.depth(z * stride_d).row(i * stride_h) + j * stride_w;
133
const float* kptr = (const float*)weight_data + maxk * inch * p;
135
for (int q = 0; q < inch; q++)
137
const float val = bottom_blob.channel(q).depth(z).row(i)[j];
139
for (int k = 0; k < maxk; k++)
142
outptr[space_ofs[k]] += val * w;
153
int size = outw * outh * outd;
155
for (int i = 0; i < size; i++)
157
outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
165
int Deconvolution3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
167
int w = bottom_blob.w;
168
int h = bottom_blob.h;
169
int d = bottom_blob.d;
170
size_t elemsize = bottom_blob.elemsize;
172
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
173
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
174
const int kernel_extent_d = dilation_d * (kernel_d - 1) + 1;
176
int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
177
int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
178
int outd = (d - 1) * stride_d + kernel_extent_d + output_pad_behind;
180
Mat top_blob_bordered;
181
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || pad_front > 0 || pad_behind > 0 || (output_w > 0 && output_h > 0 && output_d > 0))
183
top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.workspace_allocator);
187
top_blob_bordered = top_blob;
188
top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.blob_allocator);
190
if (top_blob_bordered.empty())
193
int ret = deconvolution3d(bottom_blob, top_blob_bordered, weight_data, bias_data, kernel_w, kernel_h, kernel_d, stride_w, stride_h, stride_d, dilation_w, dilation_h, dilation_d, activation_type, activation_params, opt);
197
cut_padding(top_blob_bordered, top_blob, opt);
198
if (top_blob.empty())
204
void Deconvolution3D::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
206
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || pad_front > 0 || pad_behind > 0)
208
copy_cut_border_3d(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, pad_front, pad_behind, opt);
210
else if (output_w > 0 && output_h > 0 && output_d > 0)
212
int wcut = top_blob_bordered.w - output_w;
213
int hcut = top_blob_bordered.h - output_h;
214
int dcut = top_blob_bordered.d - output_d;
216
if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233 || pad_front == -233 || pad_behind == -233)
218
// onnx padding=SAME_UPPER
219
copy_cut_border_3d(top_blob_bordered, top_blob, hcut / 2, hcut - hcut / 2, wcut / 2, wcut - wcut / 2, dcut / 2, dcut - dcut / 2, opt);
221
else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234 || pad_front == -234 || pad_behind == -234)
223
// onnx padding=SAME_LOWER
224
copy_cut_border_3d(top_blob_bordered, top_blob, hcut - hcut / 2, hcut / 2, wcut - wcut / 2, wcut / 2, dcut - dcut / 2, dcut / 2, opt);
229
top_blob = top_blob_bordered;