Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 7.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 "deconvolution3d.h"
16
17#include "fused_activation.h"
18
19namespace ncnn {
20
21Deconvolution3D::Deconvolution3D()
22{
23 one_blob_only = true;
24 support_inplace = false;
25}
26
27int Deconvolution3D::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 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());
55
56 return 0;
57}
58
59int Deconvolution3D::load_model(const ModelBin& mb)
60{
61 weight_data = mb.load(weight_data_size, 0);
62 if (weight_data.empty())
63 return -100;
64
65 if (bias_term)
66 {
67 bias_data = mb.load(num_output, 1);
68 if (bias_data.empty())
69 return -100;
70 }
71
72 return 0;
73}
74
75static 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)
76{
77 const int outw = top_blob.w;
78 const int outh = top_blob.h;
79 const int outch = top_blob.c;
80
81 const int maxk = kernel_w * kernel_h * kernel_d;
82
83 // kernel offsets
84 std::vector<int> _space_ofs(maxk);
85 int* space_ofs = &_space_ofs[0];
86 {
87 int p1 = 0;
88 int p2 = 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++)
92 {
93 for (int i = 0; i < kernel_h; i++)
94 {
95 for (int j = 0; j < kernel_w; j++)
96 {
97 space_ofs[p1] = p2;
98 p1++;
99 p2 += dilation_w;
100 }
101 p2 += gap0;
102 }
103 p2 += gap1;
104 }
105 }
106
107 #pragma omp parallel for num_threads(opt.num_threads)
108 for (int p = 0; p < outch; p++)
109 {
110 Mat out = top_blob.channel(p);
111
112 const float bias = bias_data.empty() ? 0.f : bias_data[p];
113
114 out.fill(bias);
115
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;
124
125 for (int z = 0; z < d; z++)
126 {
127 for (int i = 0; i < h; i++)
128 {
129 for (int j = 0; j < w; j++)
130 {
131 float* outptr = out.depth(z * stride_d).row(i * stride_h) + j * stride_w;
132
133 const float* kptr = (const float*)weight_data + maxk * inch * p;
134
135 for (int q = 0; q < inch; q++)
136 {
137 const float val = bottom_blob.channel(q).depth(z).row(i)[j];
138
139 for (int k = 0; k < maxk; k++)
140 {
141 float w = kptr[k];
142 outptr[space_ofs[k]] += val * w;
143 }
144
145 kptr += maxk;
146 }
147 }
148 }
149 }
150
151 {
152 float* outptr = out;
153 int size = outw * outh * outd;
154
155 for (int i = 0; i < size; i++)
156 {
157 outptr[i] = activation_ss(outptr[i], activation_type, activation_params);
158 }
159 }
160 }
161
162 return 0;
163}
164
165int Deconvolution3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
166{
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;
171
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;
175
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;
179
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))
182 {
183 top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.workspace_allocator);
184 }
185 else
186 {
187 top_blob_bordered = top_blob;
188 top_blob_bordered.create(outw, outh, outd, num_output, elemsize, opt.blob_allocator);
189 }
190 if (top_blob_bordered.empty())
191 return -100;
192
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);
194 if (ret != 0)
195 return ret;
196
197 cut_padding(top_blob_bordered, top_blob, opt);
198 if (top_blob.empty())
199 return -100;
200
201 return 0;
202}
203
204void Deconvolution3D::cut_padding(const Mat& top_blob_bordered, Mat& top_blob, const Option& opt) const
205{
206 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || pad_front > 0 || pad_behind > 0)
207 {
208 copy_cut_border_3d(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, pad_front, pad_behind, opt);
209 }
210 else if (output_w > 0 && output_h > 0 && output_d > 0)
211 {
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;
215
216 if (pad_left == -233 || pad_right == -233 || pad_top == -233 || pad_bottom == -233 || pad_front == -233 || pad_behind == -233)
217 {
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);
220 }
221 else if (pad_left == -234 || pad_right == -234 || pad_top == -234 || pad_bottom == -234 || pad_front == -234 || pad_behind == -234)
222 {
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);
225 }
226 }
227 else
228 {
229 top_blob = top_blob_bordered;
230 }
231}
232
233} // namespace ncnn