Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 12.8 KiBRawPermalink
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 "padding.h"
16
17namespace ncnn {
18
19Padding::Padding()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int Padding::load_param(const ParamDict& pd)
26{
27 top = pd.get(0, 0);
28 bottom = pd.get(1, 0);
29 left = pd.get(2, 0);
30 right = pd.get(3, 0);
31 type = pd.get(4, 0);
32 value = pd.get(5, 0.f);
33 per_channel_pad_data_size = pd.get(6, 0);
34 front = pd.get(7, 0);
35 behind = pd.get(8, 0);
36
37 return 0;
38}
39
40int Padding::load_model(const ModelBin& mb)
41{
42 if (per_channel_pad_data_size)
43 {
44 per_channel_pad_data = mb.load(per_channel_pad_data_size, 1);
45 }
46
47 return 0;
48}
49
50template<typename T>
51static void copy_make_border_image(const Mat& src, Mat& dst, int top, int left, int type, T v)
52{
53 int w = dst.w;
54 int h = dst.h;
55
56 const T* ptr = src;
57 T* outptr = dst;
58
59 if (type == 0)
60 {
61 int y = 0;
62 // fill top
63 for (; y < top; y++)
64 {
65 int x = 0;
66 for (; x < w; x++)
67 {
68 outptr[x] = v;
69 }
70 outptr += w;
71 }
72 // fill center
73 for (; y < (top + src.h); y++)
74 {
75 int x = 0;
76 for (; x < left; x++)
77 {
78 outptr[x] = v;
79 }
80 if (src.w < 12)
81 {
82 for (; x < (left + src.w); x++)
83 {
84 outptr[x] = ptr[x - left];
85 }
86 }
87 else
88 {
89 memcpy(outptr + left, ptr, src.w * sizeof(T));
90 x += src.w;
91 }
92 for (; x < w; x++)
93 {
94 outptr[x] = v;
95 }
96 ptr += src.w;
97 outptr += w;
98 }
99 // fill bottom
100 for (; y < h; y++)
101 {
102 int x = 0;
103 for (; x < w; x++)
104 {
105 outptr[x] = v;
106 }
107 outptr += w;
108 }
109 }
110
111 if (type == 1)
112 {
113 int y = 0;
114 // fill top
115 for (; y < top; y++)
116 {
117 int x = 0;
118 for (; x < left; x++)
119 {
120 outptr[x] = ptr[0];
121 }
122 if (src.w < 12)
123 {
124 for (; x < (left + src.w); x++)
125 {
126 outptr[x] = ptr[x - left];
127 }
128 }
129 else
130 {
131 memcpy(outptr + left, ptr, src.w * sizeof(T));
132 x += src.w;
133 }
134 for (; x < w; x++)
135 {
136 outptr[x] = ptr[src.w - 1];
137 }
138 outptr += w;
139 }
140 // fill center
141 for (; y < (top + src.h); y++)
142 {
143 int x = 0;
144 for (; x < left; x++)
145 {
146 outptr[x] = ptr[0];
147 }
148 if (src.w < 12)
149 {
150 for (; x < (left + src.w); x++)
151 {
152 outptr[x] = ptr[x - left];
153 }
154 }
155 else
156 {
157 memcpy(outptr + left, ptr, src.w * sizeof(T));
158 x += src.w;
159 }
160 for (; x < w; x++)
161 {
162 outptr[x] = ptr[src.w - 1];
163 }
164 ptr += src.w;
165 outptr += w;
166 }
167 // fill bottom
168 ptr -= src.w;
169 for (; y < h; y++)
170 {
171 int x = 0;
172 for (; x < left; x++)
173 {
174 outptr[x] = ptr[0];
175 }
176 if (src.w < 12)
177 {
178 for (; x < (left + src.w); x++)
179 {
180 outptr[x] = ptr[x - left];
181 }
182 }
183 else
184 {
185 memcpy(outptr + left, ptr, src.w * sizeof(T));
186 x += src.w;
187 }
188 for (; x < w; x++)
189 {
190 outptr[x] = ptr[src.w - 1];
191 }
192 outptr += w;
193 }
194 }
195
196 if (type == 2)
197 {
198 int y = 0;
199 // fill top
200 ptr += top * src.w;
201 for (; y < top; y++)
202 {
203 int x = 0;
204 for (; x < left; x++)
205 {
206 outptr[x] = ptr[left - x];
207 }
208 if (src.w < 12)
209 {
210 for (; x < (left + src.w); x++)
211 {
212 outptr[x] = ptr[x - left];
213 }
214 }
215 else
216 {
217 memcpy(outptr + left, ptr, src.w * sizeof(T));
218 x += src.w;
219 }
220 for (; x < w; x++)
221 {
222 outptr[x] = ptr[src.w - (x - left - src.w) - 2];
223 }
224 outptr += w;
225 ptr -= src.w;
226 }
227 // fill center
228 for (; y < (top + src.h); y++)
229 {
230 int x = 0;
231 for (; x < left; x++)
232 {
233 outptr[x] = ptr[left - x];
234 }
235 if (src.w < 12)
236 {
237 for (; x < (left + src.w); x++)
238 {
239 outptr[x] = ptr[x - left];
240 }
241 }
242 else
243 {
244 memcpy(outptr + left, ptr, src.w * sizeof(T));
245 x += src.w;
246 }
247 for (; x < w; x++)
248 {
249 outptr[x] = ptr[src.w - (x - left - src.w) - 2];
250 }
251 ptr += src.w;
252 outptr += w;
253 }
254 // fill bottom
255 ptr -= 2 * src.w;
256 for (; y < h; y++)
257 {
258 int x = 0;
259 for (; x < left; x++)
260 {
261 outptr[x] = ptr[left - x];
262 }
263 if (src.w < 12)
264 {
265 for (; x < (left + src.w); x++)
266 {
267 outptr[x] = ptr[x - left];
268 }
269 }
270 else
271 {
272 memcpy(outptr + left, ptr, src.w * sizeof(T));
273 x += src.w;
274 }
275 for (; x < w; x++)
276 {
277 outptr[x] = ptr[src.w - (x - left - src.w) - 2];
278 }
279 outptr += w;
280 ptr -= src.w;
281 }
282 }
283}
284
285int Padding::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
286{
287 if (top == 0 && bottom == 0 && left == 0 && right == 0 && front == 0 && behind == 0)
288 {
289 top_blob = bottom_blob;
290 return 0;
291 }
292
293 int w = bottom_blob.w;
294 int h = bottom_blob.h;
295 int d = bottom_blob.d;
296 int channels = bottom_blob.c;
297 int dims = bottom_blob.dims;
298 size_t elemsize = bottom_blob.elemsize;
299
300 int outw = w + left + right;
301
302 if (dims == 1)
303 {
304 top_blob.create(outw, elemsize, opt.blob_allocator);
305 if (top_blob.empty())
306 return -100;
307
308 if (elemsize == 1)
309 copy_make_border_image<signed char>(bottom_blob, top_blob, 0, left, type, static_cast<signed char>(value));
310 if (elemsize == 2)
311 copy_make_border_image<unsigned short>(bottom_blob, top_blob, 0, left, type, support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(value) : float32_to_bfloat16(value));
312 if (elemsize == 4)
313 copy_make_border_image<float>(bottom_blob, top_blob, 0, left, type, value);
314
315 return 0;
316 }
317
318 int outh = h + top + bottom;
319
320 if (dims == 2)
321 {
322 top_blob.create(outw, outh, elemsize, opt.blob_allocator);
323 if (top_blob.empty())
324 return -100;
325
326 if (elemsize == 1)
327 copy_make_border_image<signed char>(bottom_blob, top_blob, top, left, type, static_cast<signed char>(value));
328 if (elemsize == 2)
329 copy_make_border_image<unsigned short>(bottom_blob, top_blob, top, left, type, support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(value) : float32_to_bfloat16(value));
330 if (elemsize == 4)
331 copy_make_border_image<float>(bottom_blob, top_blob, top, left, type, value);
332
333 return 0;
334 }
335
336 if (dims == 3)
337 {
338 int outc = channels + front + behind;
339
340 top_blob.create(outw, outh, outc, elemsize, opt.blob_allocator);
341 if (top_blob.empty())
342 return -100;
343
344 #pragma omp parallel for num_threads(opt.num_threads)
345 for (int q = 0; q < outc; q++)
346 {
347 Mat borderm = top_blob.channel(q);
348
349 float pad_value = per_channel_pad_data_size ? per_channel_pad_data[q] : value;
350
351 //Channel padding
352 if (((q < front) || (q >= (channels + front))) && type == 0)
353 {
354 if (elemsize == 1)
355 {
356 borderm.fill(static_cast<signed char>(pad_value));
357 }
358 if (elemsize == 2)
359 {
360 borderm.fill(support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(pad_value) : float32_to_bfloat16(pad_value));
361 }
362 if (elemsize == 4)
363 {
364 borderm.fill(pad_value);
365 }
366 }
367 else
368 {
369 int q_ = q - front;
370
371 if (type == 1)
372 {
373 q_ = q_ <= 0 ? 0 : q_;
374 q_ = q_ >= channels - 1 ? channels - 1 : q_;
375 }
376 if (type == 2)
377 {
378 q_ = abs(q_);
379 q_ = (channels - 1) - abs(q_ - (channels - 1));
380 }
381 const Mat m = bottom_blob.channel(q_);
382 if (elemsize == 1)
383 copy_make_border_image<signed char>(m, borderm, top, left, type, static_cast<signed char>(pad_value));
384 if (elemsize == 2)
385 copy_make_border_image<unsigned short>(m, borderm, top, left, type, support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(pad_value) : float32_to_bfloat16(pad_value));
386 if (elemsize == 4)
387 copy_make_border_image<float>(m, borderm, top, left, type, pad_value);
388 }
389 }
390
391 return 0;
392 }
393
394 if (dims == 4)
395 {
396 int outd = d + front + behind;
397
398 top_blob.create(outw, outh, outd, channels, elemsize, opt.blob_allocator);
399 if (top_blob.empty())
400 return -100;
401
402 #pragma omp parallel for num_threads(opt.num_threads)
403 for (int q = 0; q < channels; q++)
404 {
405 float pad_value = per_channel_pad_data_size ? per_channel_pad_data[q] : value;
406
407 for (int z = 0; z < outd; z++)
408 {
409 Mat borderm = top_blob.channel(q).depth(z);
410
411 // depth padding
412 if (((z < front) || (z >= (d + front))) && type == 0)
413 {
414 if (elemsize == 1)
415 {
416 borderm.fill(static_cast<signed char>(pad_value));
417 }
418 if (elemsize == 2)
419 {
420 borderm.fill(support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(pad_value) : float32_to_bfloat16(pad_value));
421 }
422 if (elemsize == 4)
423 {
424 borderm.fill(pad_value);
425 }
426 }
427 else
428 {
429 int z_ = z - front;
430
431 if (type == 1)
432 {
433 z_ = z_ <= 0 ? 0 : z_;
434 z_ = z_ >= d - 1 ? d - 1 : z_;
435 }
436 if (type == 2)
437 {
438 z_ = abs(z_);
439 z_ = (d - 1) - abs(z_ - (d - 1));
440 }
441 const Mat m = bottom_blob.channel(q).depth(z_);
442 if (elemsize == 1)
443 copy_make_border_image<signed char>(m, borderm, top, left, type, static_cast<signed char>(pad_value));
444 if (elemsize == 2)
445 copy_make_border_image<unsigned short>(m, borderm, top, left, type, support_fp16_storage && opt.use_fp16_storage ? float32_to_float16(pad_value) : float32_to_bfloat16(pad_value));
446 if (elemsize == 4)
447 copy_make_border_image<float>(m, borderm, top, left, type, pad_value);
448 }
449 }
450 }
451
452 return 0;
453 }
454
455 return 0;
456}
457
458} // namespace ncnn