15
#include "deconvolutiondepthwise3d.h"
17
#include "fused_activation.h"
21
DeconvolutionDepthWise3D::DeconvolutionDepthWise3D()
24
support_inplace = false;
27
int DeconvolutionDepthWise3D::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);
54
activation_type = pd.get(9, 0);
55
activation_params = pd.get(10, Mat());
60
int DeconvolutionDepthWise3D::load_model(const ModelBin& mb)
62
weight_data = mb.load(weight_data_size, 0);
63
if (weight_data.empty())
68
bias_data = mb.load(num_output, 1);
69
if (bias_data.empty())
76
static int deconvolutiondepthwise3d(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 group, int activation_type, const Mat& activation_params, const Option& opt)
78
const int inch = bottom_blob.c;
80
const int outw = top_blob.w;
81
const int outh = top_blob.h;
82
const int outch = top_blob.c;
84
const int maxk = kernel_w * kernel_h * kernel_d;
87
std::vector<int> _space_ofs(maxk);
88
int* space_ofs = &_space_ofs[0];
92
int gap0 = outw * dilation_h - kernel_w * dilation_w;
93
int gap1 = outh * outw * dilation_d - outw * kernel_h * dilation_h;
94
for (int z = 0; z < kernel_d; z++)
96
for (int i = 0; i < kernel_h; i++)
98
for (int j = 0; j < kernel_w; j++)
111
if (inch == group && group == outch)
113
#pragma omp parallel for num_threads(opt.num_threads)
114
for (int g = 0; g < group; g++)
116
const float* inptr = bottom_blob.channel(g);
117
const float* kptr = (const float*)weight_data + maxk * g;
118
Mat out = top_blob.channel(g);
120
const float bias = bias_data.empty() ? 0.f : bias_data[g];
125
const int w = bottom_blob.w;
126
const int h = bottom_blob.h;
127
const int d = bottom_blob.d;
128
const int outw = top_blob.w;
129
const int outh = top_blob.h;
130
const int outd = top_blob.d;
132
for (int z = 0; z < d; z++)
134
for (int i = 0; i < h; i++)
136
for (int j = 0; j < w; j++)
138
float* outptr = out.depth(z * stride_d).row(i * stride_h) + j * stride_w;
140
const float val = inptr[z * w * h + i * w + j];
142
for (int k = 0; k < maxk; k++)
145
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);
164
const int inch_g = inch / group;
165
const int outch_g = outch / group;
168
#pragma omp parallel for num_threads(opt.num_threads)
170
#pragma omp parallel for collapse(2) num_threads(opt.num_threads)
172
for (int g = 0; g < group; g++)
174
for (int p = 0; p < outch_g; p++)
176
Mat out = top_blob.channel(g * outch_g + p);
178
const float* weight_data_ptr = (const float*)weight_data + maxk * inch_g * outch_g * g;
180
const float bias = bias_data.empty() ? 0.f : bias_data[g * outch_g + p];
185
const int w = bottom_blob.w;
186
const int h = bottom_blob.h;
187
const int d = bottom_blob.d;
188
const int outw = top_blob.w;
189
const int outh = top_blob.h;
190
const int outd = top_blob.d;
192
for (int z = 0; z < d; z++)
194
for (int i = 0; i < h; i++)
196
for (int j = 0; j < w; j++)
198
float* outptr = out.depth(z * stride_d).row(i * stride_h) + j * stride_w;
200
const float* kptr = weight_data_ptr + maxk * inch_g * p;
202
for (int q = 0; q < inch_g; q++)
204
const float val = bottom_blob.channel(inch_g * g + q).depth(z).row(i)[j];
206
for (int k = 0; k < maxk; k++)
208
outptr[space_ofs[k]] += val * kptr[k];
219
int size = outw * outh * outd;
221
for (int i = 0; i < size; i++)
223
outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
233
int DeconvolutionDepthWise3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
235
int w = bottom_blob.w;
236
int h = bottom_blob.h;
237
int d = bottom_blob.d;
238
size_t elemsize = bottom_blob.elemsize;
240
const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
241
const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
242
const int kernel_extent_d = dilation_d * (kernel_d - 1) + 1;
244
int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
245
int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
246
int outd = (d - 1) * stride_d + kernel_extent_d + output_pad_behind;
248
Mat top_blob_bordered;
249
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))
251
top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.workspace_allocator);
255
top_blob_bordered = top_blob;
256
top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.blob_allocator);
258
if (top_blob_bordered.empty())
261
int ret = deconvolutiondepthwise3d(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, group, activation_type, activation_params, opt);
265
cut_padding(top_blob_bordered, top_blob, opt);
266
if (top_blob.empty())
272
void DeconvolutionDepthWise3D::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
274
if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || pad_front > 0 || pad_behind > 0)
276
copy_cut_border_3d(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, pad_front, pad_behind, opt);
278
else if (output_w > 0 && output_h > 0 && output_d > 0)
280
int wcut = top_blob_bordered.w - output_w;
281
int hcut = top_blob_bordered.h - output_h;
282
int dcut = top_blob_bordered.d - output_d;
284
if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233 || pad_front == -233 || pad_behind == -233)
287
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);
289
else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234 || pad_front == -234 || pad_behind == -234)
292
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);
297
top_blob = top_blob_bordered;