1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2017 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.
22
support_inplace = false;
25
int Crop::load_param(const ParamDict& pd)
27
woffset = pd.get(0, 0);
28
hoffset = pd.get(1, 0);
29
doffset = pd.get(13, 0);
30
coffset = pd.get(2, 0);
35
woffset2 = pd.get(6, 0);
36
hoffset2 = pd.get(7, 0);
37
doffset2 = pd.get(15, 0);
38
coffset2 = pd.get(8, 0);
40
starts = pd.get(9, Mat());
41
ends = pd.get(10, Mat());
42
axes = pd.get(11, Mat());
44
bool numpy_style_slice = !starts.empty() && !ends.empty();
46
if (outw == 0 && outh == 0 && outd == 0 && outc == 0 && woffset2 == 0 && hoffset2 == 0 && doffset2 == 0 && coffset2 == 0 && !numpy_style_slice)
48
one_blob_only = false;
55
static void copy_cut_border_image(const Mat& src, Mat& dst, int top, int left)
60
const T* ptr = src.row<T>(top) + left;
61
T* outptr = dst; //.data;
63
for (int y = 0; y < h; y++)
67
for (int x = 0; x < w; x++)
74
memcpy(outptr, ptr, w * sizeof(T));
81
int Crop::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
83
int w = bottom_blob.w;
84
int h = bottom_blob.h;
85
int d = bottom_blob.d;
86
int channels = bottom_blob.c;
87
int dims = bottom_blob.dims;
88
size_t elemsize = bottom_blob.elemsize;
90
int _woffset, _hoffset, _doffset, _coffset;
91
int _outw = -1, _outh = -1, _outd = -1, _outc;
92
resolve_crop_roi(bottom_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
98
top_blob = bottom_blob;
102
top_blob.create(_outw, elemsize, opt.blob_allocator);
103
if (top_blob.empty())
107
copy_cut_border_image<signed char>(bottom_blob, top_blob, 0, _woffset);
109
copy_cut_border_image<unsigned short>(bottom_blob, top_blob, 0, _woffset);
111
copy_cut_border_image<float>(bottom_blob, top_blob, 0, _woffset);
118
if (_outw == w && _outh == h)
120
top_blob = bottom_blob;
124
top_blob.create(_outw, _outh, elemsize, opt.blob_allocator);
125
if (top_blob.empty())
129
copy_cut_border_image<signed char>(bottom_blob, top_blob, _hoffset, _woffset);
131
copy_cut_border_image<unsigned short>(bottom_blob, top_blob, _hoffset, _woffset);
133
copy_cut_border_image<float>(bottom_blob, top_blob, _hoffset, _woffset);
140
if (_outw == w && _outh == h && _outc == channels)
142
top_blob = bottom_blob;
146
const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset, _outc);
148
if (_outw == w && _outh == h)
150
top_blob = bottom_blob_sliced.clone();
151
if (top_blob.empty())
157
top_blob.create(_outw, _outh, _outc, elemsize, opt.blob_allocator);
158
if (top_blob.empty())
161
#pragma omp parallel for num_threads(opt.num_threads)
162
for (int q = 0; q < _outc; q++)
164
const Mat m = bottom_blob_sliced.channel(q);
165
Mat borderm = top_blob.channel(q);
168
copy_cut_border_image<signed char>(m, borderm, _hoffset, _woffset);
170
copy_cut_border_image<unsigned short>(m, borderm, _hoffset, _woffset);
172
copy_cut_border_image<float>(m, borderm, _hoffset, _woffset);
180
if (_outw == w && _outh == h && _outd == d && _outc == channels)
182
top_blob = bottom_blob;
186
const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset, _outc);
188
if (_outw == w && _outh == h && _outd == d)
190
top_blob = bottom_blob_sliced.clone();
191
if (top_blob.empty())
197
top_blob.create(_outw, _outh, _outd, _outc, elemsize, opt.blob_allocator);
198
if (top_blob.empty())
201
#pragma omp parallel for num_threads(opt.num_threads)
202
for (int q = 0; q < _outc; q++)
204
for (int z = 0; z < _outd; z++)
206
const Mat m = bottom_blob_sliced.channel(q).depth(z + _doffset);
207
Mat borderm = top_blob.channel(q).depth(z);
210
copy_cut_border_image<signed char>(m, borderm, _hoffset, _woffset);
212
copy_cut_border_image<unsigned short>(m, borderm, _hoffset, _woffset);
214
copy_cut_border_image<float>(m, borderm, _hoffset, _woffset);
224
int Crop::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
226
const Mat& bottom_blob = bottom_blobs[0];
227
const Mat& reference_blob = bottom_blobs[1];
229
int w = bottom_blob.w;
230
int h = bottom_blob.h;
231
int d = bottom_blob.d;
232
int channels = bottom_blob.c;
233
int dims = bottom_blob.dims;
234
size_t elemsize = bottom_blob.elemsize;
236
Mat& top_blob = top_blobs[0];
238
int _woffset, _hoffset, _doffset, _coffset = -1;
239
int _outw = -1, _outh = -1, _outd = -1, _outc;
242
resolve_crop_roi(bottom_blob.shape(), (const int*)reference_blob, _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
246
resolve_crop_roi(bottom_blob.shape(), reference_blob.shape(), _woffset, _hoffset, _doffset, _coffset, _outw, _outh, _outd, _outc);
253
top_blob = bottom_blob;
257
top_blob.create(_outw, elemsize, opt.blob_allocator);
258
if (top_blob.empty())
262
copy_cut_border_image<signed char>(bottom_blob, top_blob, 0, _woffset);
264
copy_cut_border_image<unsigned short>(bottom_blob, top_blob, 0, _woffset);
266
copy_cut_border_image<float>(bottom_blob, top_blob, 0, _woffset);
273
if (_outw == w && _outh == h)
275
top_blob = bottom_blob;
279
top_blob.create(_outw, _outh, elemsize, opt.blob_allocator);
280
if (top_blob.empty())
284
copy_cut_border_image<signed char>(bottom_blob, top_blob, _hoffset, _woffset);
286
copy_cut_border_image<unsigned short>(bottom_blob, top_blob, _hoffset, _woffset);
288
copy_cut_border_image<float>(bottom_blob, top_blob, _hoffset, _woffset);
295
if (_outw == w && _outh == h && _outc == channels)
297
top_blob = bottom_blob;
301
const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset, _outc);
303
if (_outw == w && _outh == h)
305
top_blob = bottom_blob_sliced.clone();
306
if (top_blob.empty())
312
top_blob.create(_outw, _outh, _outc, elemsize, opt.blob_allocator);
313
if (top_blob.empty())
316
#pragma omp parallel for num_threads(opt.num_threads)
317
for (int q = 0; q < _outc; q++)
319
const Mat m = bottom_blob_sliced.channel(q);
320
Mat borderm = top_blob.channel(q);
323
copy_cut_border_image<signed char>(m, borderm, _hoffset, _woffset);
325
copy_cut_border_image<unsigned short>(m, borderm, _hoffset, _woffset);
327
copy_cut_border_image<float>(m, borderm, _hoffset, _woffset);
335
if (_outw == w && _outh == h && _outd == d && _outc == channels)
337
top_blob = bottom_blob;
341
const Mat bottom_blob_sliced = bottom_blob.channel_range(_coffset, _outc);
343
if (_outw == w && _outh == h && _outd == d)
345
top_blob = bottom_blob_sliced.clone();
346
if (top_blob.empty())
352
top_blob.create(_outw, _outh, _outd, _outc, elemsize, opt.blob_allocator);
353
if (top_blob.empty())
356
#pragma omp parallel for num_threads(opt.num_threads)
357
for (int q = 0; q < _outc; q++)
359
for (int z = 0; z < _outd; z++)
361
const Mat m = bottom_blob_sliced.channel(q).depth(z + _doffset);
362
Mat borderm = top_blob.channel(q).depth(z);
365
copy_cut_border_image<signed char>(m, borderm, _hoffset, _woffset);
367
copy_cut_border_image<unsigned short>(m, borderm, _hoffset, _woffset);
369
copy_cut_border_image<float>(m, borderm, _hoffset, _woffset);
379
void Crop::resolve_crop_roi(const Mat& bottom_blob, int& _woffset, int& _hoffset, int& _doffset, int& _coffset, int& _outw, int& _outh, int& _outd, int& _outc) const
381
int w = bottom_blob.w;
382
int h = bottom_blob.h;
383
int d = bottom_blob.d;
384
int channels = bottom_blob.c;
385
int dims = bottom_blob.dims;
387
bool numpy_style_slice = !starts.empty() && !ends.empty();
388
if (numpy_style_slice)
399
const int* starts_ptr = starts;
400
const int* ends_ptr = ends;
401
const int* axes_ptr = axes;
403
int _axes[4] = {0, 1, 2, 3};
404
int num_axis = axes.w;
411
for (int i = 0; i < num_axis; i++)
413
int axis = axes_ptr[i];
420
for (int i = 0; i < num_axis; i++)
423
int start = starts_ptr[i];
424
int end = ends_ptr[i];
426
if (dims == 1) // axis == 0
428
if (start == -233) start = 0;
429
if (end == -233) end = w;
430
_woffset = start >= 0 ? start : w + start;
431
_outw = std::min(w, end > 0 ? end : w + end) - _woffset;
437
if (start == -233) start = 0;
438
if (end == -233) end = h;
439
_hoffset = start >= 0 ? start : h + start;
440
_outh = std::min(h, end > 0 ? end : h + end) - _hoffset;
444
if (start == -233) start = 0;
445
if (end == -233) end = w;
446
_woffset = start >= 0 ? start : w + start;
447
_outw = std::min(w, end > 0 ? end : w + end) - _woffset;
454
if (start == -233) start = 0;
455
if (end == -233) end = channels;
456
_coffset = start >= 0 ? start : channels + start;
457
_outc = std::min(channels, end > 0 ? end : channels + end) - _coffset;
461
if (start == -233) start = 0;
462
if (end == -233) end = h;
463
_hoffset = start >= 0 ? start : h + start;
464
_outh = std::min(h, end > 0 ? end : h + end) - _hoffset;
468
if (start == -233) start = 0;
469
if (end == -233) end = w;
470
_woffset = start >= 0 ? start : w + start;
471
_outw = std::min(w, end > 0 ? end : w + end) - _woffset;
478
if (start == -233) start = 0;
479
if (end == -233) end = channels;
480
_coffset = start >= 0 ? start : channels + start;
481
_outc = std::min(channels, end > 0 ? end : channels + end) - _coffset;
485
if (start == -233) start = 0;
486
if (end == -233) end = d;
487
_doffset = start >= 0 ? start : d + start;
488
_outd = std::min(d, end > 0 ? end : d + end) - _doffset;
492
if (start == -233) start = 0;
493
if (end == -233) end = h;
494
_hoffset = start >= 0 ? start : h + start;
495
_outh = std::min(h, end > 0 ? end : h + end) - _hoffset;
499
if (start == -233) start = 0;
500
if (end == -233) end = w;
501
_woffset = start >= 0 ? start : w + start;
502
_outw = std::min(w, end > 0 ? end : w + end) - _woffset;
520
_outw = w - woffset - woffset2;
522
_outw = std::min(outw, _outw);
526
_outw = w - woffset - woffset2;
528
_outw = std::min(outw, _outw);
530
_outh = h - hoffset - hoffset2;
532
_outh = std::min(outh, _outh);
536
_outw = w - woffset - woffset2;
538
_outw = std::min(outw, _outw);
540
_outh = h - hoffset - hoffset2;
542
_outh = std::min(outh, _outh);
544
_outc = channels - coffset - coffset2;
546
_outc = std::min(outc, _outc);
550
_outw = w - woffset - woffset2;
552
_outw = std::min(outw, _outw);
554
_outh = h - hoffset - hoffset2;
556
_outh = std::min(outh, _outh);
558
_outd = d - doffset - doffset2;
560
_outd = std::min(outd, _outd);
562
_outc = channels - coffset - coffset2;
564
_outc = std::min(outc, _outc);
569
void Crop::resolve_crop_roi(const Mat& bottom_blob, const Mat& reference_blob, int& _woffset, int& _hoffset, int& _doffset, int& _coffset, int& _outw, int& _outh, int& _outd, int& _outc) const
571
int channels = bottom_blob.c;
572
int dims = bottom_blob.dims;
574
int ref_w = reference_blob.w;
575
int ref_h = reference_blob.h;
576
int ref_d = reference_blob.d;
577
int ref_channels = reference_blob.c;
578
int ref_dims = reference_blob.dims;
599
_outc = ref_dims == 3 ? ref_channels : channels;
610
_outc = ref_dims == 4 ? ref_channels : channels;
614
void Crop::resolve_crop_roi(const Mat& bottom_blob, const int* param_data, int& _woffset, int& _hoffset, int& _doffset, int& _coffset, int& _outw, int& _outh, int& _outd, int& _outc) const
616
int dims = bottom_blob.dims;
620
_woffset = param_data[0];
621
_outw = param_data[3];
625
_woffset = param_data[0];
626
_hoffset = param_data[1];
627
_outw = param_data[3];
628
_outh = param_data[4];
632
_woffset = param_data[0];
633
_hoffset = param_data[1];
634
_coffset = param_data[2];
635
_outw = param_data[3];
636
_outh = param_data[4];
637
_outc = param_data[5];
641
_woffset = param_data[0];
642
_hoffset = param_data[1];
643
_doffset = param_data[2];
644
_coffset = param_data[3];
645
_outw = param_data[4];
646
_outh = param_data[5];
647
_outd = param_data[6];
648
_outc = param_data[7];