Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/CL/functions/CLGenerateProposalsLayer.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/Types.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 28 | #include "src/core/CL/kernels/CLBoundingBoxTransformKernel.h" |
| 29 | #include "src/core/CL/kernels/CLDequantizationLayerKernel.h" |
| 30 | #include "src/core/CL/kernels/CLGenerateProposalsLayerKernel.h" |
| 31 | #include "src/core/CL/kernels/CLPadLayerKernel.h" |
| 32 | #include "src/core/CL/kernels/CLPermuteKernel.h" |
| 33 | #include "src/core/CL/kernels/CLQuantizationLayerKernel.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 35 | #include "support/MemorySupport.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | CLGenerateProposalsLayer::CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 40 | : _memory_group(memory_manager), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 41 | _permute_deltas_kernel(support::cpp14::make_unique<CLPermuteKernel>()), |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 42 | _flatten_deltas(), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 43 | _permute_scores_kernel(support::cpp14::make_unique<CLPermuteKernel>()), |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 44 | _flatten_scores(), |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 45 | _compute_anchors_kernel(support::cpp14::make_unique<CLComputeAllAnchorsKernel>()), |
| 46 | _bounding_box_kernel(support::cpp14::make_unique<CLBoundingBoxTransformKernel>()), |
| 47 | _pad_kernel(support::cpp14::make_unique<CLPadLayerKernel>()), |
| 48 | _dequantize_anchors(support::cpp14::make_unique<CLDequantizationLayerKernel>()), |
| 49 | _dequantize_deltas(support::cpp14::make_unique<CLDequantizationLayerKernel>()), |
| 50 | _quantize_all_proposals(support::cpp14::make_unique<CLQuantizationLayerKernel>()), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 51 | _cpp_nms(memory_manager), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 52 | _is_nhwc(false), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 53 | _is_qasymm8(false), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 54 | _deltas_permuted(), |
| 55 | _deltas_flattened(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 56 | _deltas_flattened_f32(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 57 | _scores_permuted(), |
| 58 | _scores_flattened(), |
| 59 | _all_anchors(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 60 | _all_anchors_f32(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 61 | _all_proposals(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 62 | _all_proposals_quantized(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 63 | _keeps_nms_unused(), |
| 64 | _classes_nms_unused(), |
| 65 | _proposals_4_roi_values(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 66 | _all_proposals_to_use(nullptr), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 67 | _num_valid_proposals(nullptr), |
| 68 | _scores_out(nullptr) |
| 69 | { |
| 70 | } |
| 71 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 72 | CLGenerateProposalsLayer::~CLGenerateProposalsLayer() = default; |
| 73 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 74 | void CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals, |
| 75 | const GenerateProposalsInfo &info) |
| 76 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | configure(CLKernelLibrary::get().get_compile_context(), scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info); |
| 78 | } |
| 79 | |
| 80 | void CLGenerateProposalsLayer::configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, |
| 81 | ICLTensor *scores_out, |
| 82 | ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info) |
| 83 | { |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals); |
| 85 | ARM_COMPUTE_ERROR_THROW_ON(CLGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info)); |
| 86 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 87 | _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC; |
| 88 | const DataType scores_data_type = scores->info()->data_type(); |
| 89 | _is_qasymm8 = scores_data_type == DataType::QASYMM8; |
| 90 | const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL)); |
| 91 | const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH)); |
| 92 | const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT)); |
| 93 | const int total_num_anchors = num_anchors * feat_width * feat_height; |
| 94 | const int pre_nms_topN = info.pre_nms_topN(); |
| 95 | const int post_nms_topN = info.post_nms_topN(); |
| 96 | const size_t values_per_roi = info.values_per_roi(); |
| 97 | |
| 98 | const QuantizationInfo scores_qinfo = scores->info()->quantization_info(); |
| 99 | const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type; |
| 100 | const QuantizationInfo rois_qinfo = (_is_qasymm8) ? QuantizationInfo(0.125f, 0) : scores->info()->quantization_info(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 101 | |
| 102 | // Compute all the anchors |
| 103 | _memory_group.manage(&_all_anchors); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 104 | _compute_anchors_kernel->configure(compile_context, anchors, &_all_anchors, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale())); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 105 | |
| 106 | const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 107 | _deltas_flattened.allocator()->init(TensorInfo(flatten_shape_deltas, 1, scores_data_type, deltas->info()->quantization_info())); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 108 | |
| 109 | // Permute and reshape deltas |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 110 | _memory_group.manage(&_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 111 | if(!_is_nhwc) |
| 112 | { |
| 113 | _memory_group.manage(&_deltas_permuted); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 114 | _permute_deltas_kernel->configure(compile_context, deltas, &_deltas_permuted, PermutationVector{ 2, 0, 1 }); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 115 | _flatten_deltas.configure(compile_context, &_deltas_permuted, &_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 116 | _deltas_permuted.allocator()->allocate(); |
| 117 | } |
| 118 | else |
| 119 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 120 | _flatten_deltas.configure(compile_context, deltas, &_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | const TensorShape flatten_shape_scores(1, total_num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 124 | _scores_flattened.allocator()->init(TensorInfo(flatten_shape_scores, 1, scores_data_type, scores_qinfo)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 125 | |
| 126 | // Permute and reshape scores |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 127 | _memory_group.manage(&_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 128 | if(!_is_nhwc) |
| 129 | { |
| 130 | _memory_group.manage(&_scores_permuted); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 131 | _permute_scores_kernel->configure(compile_context, scores, &_scores_permuted, PermutationVector{ 2, 0, 1 }); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 132 | _flatten_scores.configure(compile_context, &_scores_permuted, &_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 133 | _scores_permuted.allocator()->allocate(); |
| 134 | } |
| 135 | else |
| 136 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 137 | _flatten_scores.configure(compile_context, scores, &_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 140 | CLTensor *anchors_to_use = &_all_anchors; |
| 141 | CLTensor *deltas_to_use = &_deltas_flattened; |
| 142 | if(_is_qasymm8) |
| 143 | { |
| 144 | _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32)); |
| 145 | _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32)); |
| 146 | _memory_group.manage(&_all_anchors_f32); |
| 147 | _memory_group.manage(&_deltas_flattened_f32); |
| 148 | // Dequantize anchors to float |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 149 | _dequantize_anchors->configure(compile_context, &_all_anchors, &_all_anchors_f32); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 150 | _all_anchors.allocator()->allocate(); |
| 151 | anchors_to_use = &_all_anchors_f32; |
| 152 | // Dequantize deltas to float |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 153 | _dequantize_deltas->configure(compile_context, &_deltas_flattened, &_deltas_flattened_f32); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 154 | _deltas_flattened.allocator()->allocate(); |
| 155 | deltas_to_use = &_deltas_flattened_f32; |
| 156 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 157 | // Bounding box transform |
| 158 | _memory_group.manage(&_all_proposals); |
| 159 | BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 160 | _bounding_box_kernel->configure(compile_context, anchors_to_use, &_all_proposals, deltas_to_use, bbox_info); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 161 | deltas_to_use->allocator()->allocate(); |
| 162 | anchors_to_use->allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 163 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 164 | _all_proposals_to_use = &_all_proposals; |
| 165 | if(_is_qasymm8) |
| 166 | { |
| 167 | _memory_group.manage(&_all_proposals_quantized); |
| 168 | // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset |
| 169 | _all_proposals_quantized.allocator()->init(TensorInfo(_all_proposals.info()->tensor_shape(), 1, DataType::QASYMM16, QuantizationInfo(0.125f, 0))); |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 170 | _quantize_all_proposals->configure(compile_context, &_all_proposals, &_all_proposals_quantized); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 171 | _all_proposals.allocator()->allocate(); |
| 172 | _all_proposals_to_use = &_all_proposals_quantized; |
| 173 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 174 | // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort) |
| 175 | // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation. |
| 176 | // Since we are reusing the NMS layer and we don't implement any CL/sort, we let NMS do the sorting (of all the input) |
| 177 | // and the filtering |
| 178 | const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors); |
| 179 | const float min_size_scaled = info.min_size() * info.im_scale(); |
| 180 | _memory_group.manage(&_classes_nms_unused); |
| 181 | _memory_group.manage(&_keeps_nms_unused); |
| 182 | |
| 183 | // Note that NMS needs outputs preinitialized. |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 184 | auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo); |
| 185 | auto_init_if_empty(*_proposals_4_roi_values.info(), TensorShape(values_per_roi, scores_nms_size), 1, rois_data_type, rois_qinfo); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 186 | auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32); |
| 187 | |
| 188 | // Initialize temporaries (unused) outputs |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 189 | _classes_nms_unused.allocator()->init(TensorInfo(TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 190 | _keeps_nms_unused.allocator()->init(*scores_out->info()); |
| 191 | |
| 192 | // Save the output (to map and unmap them at run) |
| 193 | _scores_out = scores_out; |
| 194 | _num_valid_proposals = num_valid_proposals; |
| 195 | |
| 196 | _memory_group.manage(&_proposals_4_roi_values); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 197 | _cpp_nms.configure(&_scores_flattened, _all_proposals_to_use, nullptr, scores_out, &_proposals_4_roi_values, &_classes_nms_unused, nullptr, &_keeps_nms_unused, num_valid_proposals, |
| 198 | BoxNMSLimitInfo(0.0f, info.nms_thres(), scores_nms_size, false, NMSType::LINEAR, 0.5f, 0.001f, true, min_size_scaled, info.im_width(), info.im_height())); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 199 | _keeps_nms_unused.allocator()->allocate(); |
| 200 | _classes_nms_unused.allocator()->allocate(); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 201 | _all_proposals_to_use->allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 202 | _scores_flattened.allocator()->allocate(); |
| 203 | |
| 204 | // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 205 | _pad_kernel->configure(compile_context, &_proposals_4_roi_values, proposals, PaddingList{ { 1, 0 } }); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 206 | _proposals_4_roi_values.allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out, |
| 210 | const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info) |
| 211 | { |
| 212 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 213 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(scores, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 214 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC); |
| 215 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 216 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 217 | |
| 218 | const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL)); |
| 219 | const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH)); |
| 220 | const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT)); |
| 221 | const int num_images = scores->dimension(3); |
| 222 | const int total_num_anchors = num_anchors * feat_width * feat_height; |
| 223 | const int values_per_roi = info.values_per_roi(); |
| 224 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 225 | const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8; |
| 226 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 227 | ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1); |
| 228 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 229 | if(is_qasymm8) |
| 230 | { |
| 231 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16); |
| 232 | const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform(); |
| 233 | ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f); |
| 234 | } |
| 235 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 236 | TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 237 | ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()))); |
| 238 | |
| 239 | TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true); |
| 240 | TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true); |
| 241 | if(scores->data_layout() == DataLayout::NHWC) |
| 242 | { |
| 243 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info); |
| 244 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 })); |
| 249 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 })); |
| 250 | } |
| 251 | |
| 252 | TensorInfo deltas_flattened_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 253 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&deltas_permuted_info, &deltas_flattened_info)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 254 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 255 | TensorInfo scores_flattened_info(scores->clone()->set_tensor_shape(TensorShape(1, total_num_anchors)).set_is_resizable(true)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 256 | TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 257 | |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 258 | ARM_COMPUTE_RETURN_ON_ERROR(CLReshapeLayer::validate(&scores_permuted_info, &scores_flattened_info)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 259 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 260 | TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values; |
| 261 | TensorInfo proposals_4_roi_values_quantized(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 262 | proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16).set_quantization_info(QuantizationInfo(0.125f, 0)); |
| 263 | if(is_qasymm8) |
| 264 | { |
| 265 | TensorInfo all_anchors_f32_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32)); |
| 266 | ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&all_anchors_info, &all_anchors_f32_info)); |
| 267 | |
| 268 | TensorInfo deltas_flattened_f32_info(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32)); |
| 269 | ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&deltas_flattened_info, &deltas_flattened_f32_info)); |
| 270 | |
| 271 | TensorInfo proposals_4_roi_values_f32(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true).set_data_type(DataType::F32)); |
| 272 | ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info, |
| 273 | BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f))); |
| 274 | |
| 275 | ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayerKernel::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized)); |
| 276 | proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized; |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info, |
| 281 | BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f))); |
| 282 | } |
| 283 | |
| 284 | ARM_COMPUTE_RETURN_ON_ERROR(CLPadLayerKernel::validate(proposals_4_roi_values_to_use, proposals, PaddingList{ { 1, 0 } })); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 285 | |
| 286 | if(num_valid_proposals->total_size() > 0) |
| 287 | { |
| 288 | ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1); |
| 289 | ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1); |
| 290 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32); |
| 291 | } |
| 292 | |
| 293 | if(proposals->total_size() > 0) |
| 294 | { |
| 295 | ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2); |
| 296 | ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1); |
| 297 | ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(1) != size_t(total_num_anchors)); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 298 | if(is_qasymm8) |
| 299 | { |
| 300 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16); |
| 301 | const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform(); |
| 302 | ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f); |
| 303 | ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0); |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores); |
| 308 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | if(scores_out->total_size() > 0) |
| 312 | { |
| 313 | ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1); |
| 314 | ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors)); |
| 315 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores); |
| 316 | } |
| 317 | |
| 318 | return Status{}; |
| 319 | } |
| 320 | |
| 321 | void CLGenerateProposalsLayer::run_cpp_nms_kernel() |
| 322 | { |
| 323 | // Map inputs |
| 324 | _scores_flattened.map(true); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 325 | _all_proposals_to_use->map(true); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 326 | |
| 327 | // Map outputs |
| 328 | _scores_out->map(CLScheduler::get().queue(), true); |
| 329 | _proposals_4_roi_values.map(CLScheduler::get().queue(), true); |
| 330 | _num_valid_proposals->map(CLScheduler::get().queue(), true); |
| 331 | _keeps_nms_unused.map(true); |
| 332 | _classes_nms_unused.map(true); |
| 333 | |
| 334 | // Run nms |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 335 | _cpp_nms.run(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 336 | |
| 337 | // Unmap outputs |
| 338 | _keeps_nms_unused.unmap(); |
| 339 | _classes_nms_unused.unmap(); |
| 340 | _scores_out->unmap(CLScheduler::get().queue()); |
| 341 | _proposals_4_roi_values.unmap(CLScheduler::get().queue()); |
| 342 | _num_valid_proposals->unmap(CLScheduler::get().queue()); |
| 343 | |
| 344 | // Unmap inputs |
| 345 | _scores_flattened.unmap(); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 346 | _all_proposals_to_use->unmap(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void CLGenerateProposalsLayer::run() |
| 350 | { |
| 351 | // Acquire all the temporaries |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 352 | MemoryGroupResourceScope scope_mg(_memory_group); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 353 | |
| 354 | // Compute all the anchors |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 355 | CLScheduler::get().enqueue(*_compute_anchors_kernel, false); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 356 | |
| 357 | // Transpose and reshape the inputs |
| 358 | if(!_is_nhwc) |
| 359 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 360 | CLScheduler::get().enqueue(*_permute_deltas_kernel, false); |
| 361 | CLScheduler::get().enqueue(*_permute_scores_kernel, false); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 362 | } |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 363 | _flatten_deltas.run(); |
| 364 | _flatten_scores.run(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 365 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 366 | if(_is_qasymm8) |
| 367 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 368 | CLScheduler::get().enqueue(*_dequantize_anchors, false); |
| 369 | CLScheduler::get().enqueue(*_dequantize_deltas, false); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 372 | // Build the boxes |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 373 | CLScheduler::get().enqueue(*_bounding_box_kernel, false); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 374 | |
| 375 | if(_is_qasymm8) |
| 376 | { |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 377 | CLScheduler::get().enqueue(*_quantize_all_proposals, false); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 380 | // Non maxima suppression |
| 381 | run_cpp_nms_kernel(); |
| 382 | // Add dummy batch indexes |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 383 | CLScheduler::get().enqueue(*_pad_kernel, true); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 384 | } |
| 385 | } // namespace arm_compute |