Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 9204646 | 2020-03-07 22:15:55 +0000 | [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" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 28 | |
| 29 | namespace arm_compute |
| 30 | { |
| 31 | CLGenerateProposalsLayer::CLGenerateProposalsLayer(std::shared_ptr<IMemoryManager> memory_manager) |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 32 | : _memory_group(memory_manager), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 33 | _permute_deltas_kernel(), |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 34 | _flatten_deltas(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 35 | _permute_scores_kernel(), |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 36 | _flatten_scores(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 37 | _compute_anchors_kernel(), |
| 38 | _bounding_box_kernel(), |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 39 | _pad_kernel(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 40 | _dequantize_anchors(), |
| 41 | _dequantize_deltas(), |
| 42 | _quantize_all_proposals(), |
| 43 | _cpp_nms(memory_manager), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 44 | _is_nhwc(false), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 45 | _is_qasymm8(false), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 46 | _deltas_permuted(), |
| 47 | _deltas_flattened(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 48 | _deltas_flattened_f32(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 49 | _scores_permuted(), |
| 50 | _scores_flattened(), |
| 51 | _all_anchors(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 52 | _all_anchors_f32(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 53 | _all_proposals(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 54 | _all_proposals_quantized(), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 55 | _keeps_nms_unused(), |
| 56 | _classes_nms_unused(), |
| 57 | _proposals_4_roi_values(), |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 58 | _all_proposals_to_use(nullptr), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 59 | _num_valid_proposals(nullptr), |
| 60 | _scores_out(nullptr) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void CLGenerateProposalsLayer::configure(const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, ICLTensor *scores_out, ICLTensor *num_valid_proposals, |
| 65 | const GenerateProposalsInfo &info) |
| 66 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 67 | configure(CLKernelLibrary::get().get_compile_context(), scores, deltas, anchors, proposals, scores_out, num_valid_proposals, info); |
| 68 | } |
| 69 | |
| 70 | void CLGenerateProposalsLayer::configure(const CLCompileContext &compile_context, const ICLTensor *scores, const ICLTensor *deltas, const ICLTensor *anchors, ICLTensor *proposals, |
| 71 | ICLTensor *scores_out, |
| 72 | ICLTensor *num_valid_proposals, const GenerateProposalsInfo &info) |
| 73 | { |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(scores, deltas, anchors, proposals, scores_out, num_valid_proposals); |
| 75 | ARM_COMPUTE_ERROR_THROW_ON(CLGenerateProposalsLayer::validate(scores->info(), deltas->info(), anchors->info(), proposals->info(), scores_out->info(), num_valid_proposals->info(), info)); |
| 76 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 77 | _is_nhwc = scores->info()->data_layout() == DataLayout::NHWC; |
| 78 | const DataType scores_data_type = scores->info()->data_type(); |
| 79 | _is_qasymm8 = scores_data_type == DataType::QASYMM8; |
| 80 | const int num_anchors = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::CHANNEL)); |
| 81 | const int feat_width = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::WIDTH)); |
| 82 | const int feat_height = scores->info()->dimension(get_data_layout_dimension_index(scores->info()->data_layout(), DataLayoutDimension::HEIGHT)); |
| 83 | const int total_num_anchors = num_anchors * feat_width * feat_height; |
| 84 | const int pre_nms_topN = info.pre_nms_topN(); |
| 85 | const int post_nms_topN = info.post_nms_topN(); |
| 86 | const size_t values_per_roi = info.values_per_roi(); |
| 87 | |
| 88 | const QuantizationInfo scores_qinfo = scores->info()->quantization_info(); |
| 89 | const DataType rois_data_type = (_is_qasymm8) ? DataType::QASYMM16 : scores_data_type; |
| 90 | 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] | 91 | |
| 92 | // Compute all the anchors |
| 93 | _memory_group.manage(&_all_anchors); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 94 | _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] | 95 | |
| 96 | const TensorShape flatten_shape_deltas(values_per_roi, total_num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 97 | _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] | 98 | |
| 99 | // Permute and reshape deltas |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 100 | _memory_group.manage(&_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 101 | if(!_is_nhwc) |
| 102 | { |
| 103 | _memory_group.manage(&_deltas_permuted); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 104 | _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] | 105 | _flatten_deltas.configure(compile_context, &_deltas_permuted, &_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 106 | _deltas_permuted.allocator()->allocate(); |
| 107 | } |
| 108 | else |
| 109 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 110 | _flatten_deltas.configure(compile_context, deltas, &_deltas_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | const TensorShape flatten_shape_scores(1, total_num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 114 | _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] | 115 | |
| 116 | // Permute and reshape scores |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 117 | _memory_group.manage(&_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 118 | if(!_is_nhwc) |
| 119 | { |
| 120 | _memory_group.manage(&_scores_permuted); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 121 | _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] | 122 | _flatten_scores.configure(compile_context, &_scores_permuted, &_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 123 | _scores_permuted.allocator()->allocate(); |
| 124 | } |
| 125 | else |
| 126 | { |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 127 | _flatten_scores.configure(compile_context, scores, &_scores_flattened); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 130 | CLTensor *anchors_to_use = &_all_anchors; |
| 131 | CLTensor *deltas_to_use = &_deltas_flattened; |
| 132 | if(_is_qasymm8) |
| 133 | { |
| 134 | _all_anchors_f32.allocator()->init(TensorInfo(_all_anchors.info()->tensor_shape(), 1, DataType::F32)); |
| 135 | _deltas_flattened_f32.allocator()->init(TensorInfo(_deltas_flattened.info()->tensor_shape(), 1, DataType::F32)); |
| 136 | _memory_group.manage(&_all_anchors_f32); |
| 137 | _memory_group.manage(&_deltas_flattened_f32); |
| 138 | // Dequantize anchors to float |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 139 | _dequantize_anchors.configure(compile_context, &_all_anchors, &_all_anchors_f32); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 140 | _all_anchors.allocator()->allocate(); |
| 141 | anchors_to_use = &_all_anchors_f32; |
| 142 | // Dequantize deltas to float |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 143 | _dequantize_deltas.configure(compile_context, &_deltas_flattened, &_deltas_flattened_f32); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 144 | _deltas_flattened.allocator()->allocate(); |
| 145 | deltas_to_use = &_deltas_flattened_f32; |
| 146 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 147 | // Bounding box transform |
| 148 | _memory_group.manage(&_all_proposals); |
| 149 | BoundingBoxTransformInfo bbox_info(info.im_width(), info.im_height(), 1.f); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 150 | _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] | 151 | deltas_to_use->allocator()->allocate(); |
| 152 | anchors_to_use->allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 153 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 154 | _all_proposals_to_use = &_all_proposals; |
| 155 | if(_is_qasymm8) |
| 156 | { |
| 157 | _memory_group.manage(&_all_proposals_quantized); |
| 158 | // Requantize all_proposals to QASYMM16 with 0.125 scale and 0 offset |
| 159 | _all_proposals_quantized.allocator()->init(TensorInfo(_all_proposals.info()->tensor_shape(), 1, DataType::QASYMM16, QuantizationInfo(0.125f, 0))); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 160 | _quantize_all_proposals.configure(compile_context, &_all_proposals, &_all_proposals_quantized); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 161 | _all_proposals.allocator()->allocate(); |
| 162 | _all_proposals_to_use = &_all_proposals_quantized; |
| 163 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 164 | // The original layer implementation first selects the best pre_nms_topN anchors (thus having a lightweight sort) |
| 165 | // that are then transformed by bbox_transform. The boxes generated are then fed into a non-sorting NMS operation. |
| 166 | // 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) |
| 167 | // and the filtering |
| 168 | const int scores_nms_size = std::min<int>(std::min<int>(post_nms_topN, pre_nms_topN), total_num_anchors); |
| 169 | const float min_size_scaled = info.min_size() * info.im_scale(); |
| 170 | _memory_group.manage(&_classes_nms_unused); |
| 171 | _memory_group.manage(&_keeps_nms_unused); |
| 172 | |
| 173 | // Note that NMS needs outputs preinitialized. |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 174 | auto_init_if_empty(*scores_out->info(), TensorShape(scores_nms_size), 1, scores_data_type, scores_qinfo); |
| 175 | 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] | 176 | auto_init_if_empty(*num_valid_proposals->info(), TensorShape(1), 1, DataType::U32); |
| 177 | |
| 178 | // Initialize temporaries (unused) outputs |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 179 | _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] | 180 | _keeps_nms_unused.allocator()->init(*scores_out->info()); |
| 181 | |
| 182 | // Save the output (to map and unmap them at run) |
| 183 | _scores_out = scores_out; |
| 184 | _num_valid_proposals = num_valid_proposals; |
| 185 | |
| 186 | _memory_group.manage(&_proposals_4_roi_values); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 187 | _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, |
| 188 | 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] | 189 | _keeps_nms_unused.allocator()->allocate(); |
| 190 | _classes_nms_unused.allocator()->allocate(); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 191 | _all_proposals_to_use->allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 192 | _scores_flattened.allocator()->allocate(); |
| 193 | |
| 194 | // Add the first column that represents the batch id. This will be all zeros, as we don't support multiple images |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 195 | _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] | 196 | _proposals_4_roi_values.allocator()->allocate(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | Status CLGenerateProposalsLayer::validate(const ITensorInfo *scores, const ITensorInfo *deltas, const ITensorInfo *anchors, const ITensorInfo *proposals, const ITensorInfo *scores_out, |
| 200 | const ITensorInfo *num_valid_proposals, const GenerateProposalsInfo &info) |
| 201 | { |
| 202 | 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] | 203 | 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] | 204 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(scores, DataLayout::NCHW, DataLayout::NHWC); |
| 205 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(scores, deltas); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 206 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores, deltas); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 207 | |
| 208 | const int num_anchors = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::CHANNEL)); |
| 209 | const int feat_width = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::WIDTH)); |
| 210 | const int feat_height = scores->dimension(get_data_layout_dimension_index(scores->data_layout(), DataLayoutDimension::HEIGHT)); |
| 211 | const int num_images = scores->dimension(3); |
| 212 | const int total_num_anchors = num_anchors * feat_width * feat_height; |
| 213 | const int values_per_roi = info.values_per_roi(); |
| 214 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 215 | const bool is_qasymm8 = scores->data_type() == DataType::QASYMM8; |
| 216 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 217 | ARM_COMPUTE_RETURN_ERROR_ON(num_images > 1); |
| 218 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 219 | if(is_qasymm8) |
| 220 | { |
| 221 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(anchors, 1, DataType::QSYMM16); |
| 222 | const UniformQuantizationInfo anchors_qinfo = anchors->quantization_info().uniform(); |
| 223 | ARM_COMPUTE_RETURN_ERROR_ON(anchors_qinfo.scale != 0.125f); |
| 224 | } |
| 225 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 226 | TensorInfo all_anchors_info(anchors->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 227 | ARM_COMPUTE_RETURN_ON_ERROR(CLComputeAllAnchorsKernel::validate(anchors, &all_anchors_info, ComputeAnchorsInfo(feat_width, feat_height, info.spatial_scale()))); |
| 228 | |
| 229 | TensorInfo deltas_permuted_info = deltas->clone()->set_tensor_shape(TensorShape(values_per_roi * num_anchors, feat_width, feat_height)).set_is_resizable(true); |
| 230 | TensorInfo scores_permuted_info = scores->clone()->set_tensor_shape(TensorShape(num_anchors, feat_width, feat_height)).set_is_resizable(true); |
| 231 | if(scores->data_layout() == DataLayout::NHWC) |
| 232 | { |
| 233 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(deltas, &deltas_permuted_info); |
| 234 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(scores, &scores_permuted_info); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(deltas, &deltas_permuted_info, PermutationVector{ 2, 0, 1 })); |
| 239 | ARM_COMPUTE_RETURN_ON_ERROR(CLPermuteKernel::validate(scores, &scores_permuted_info, PermutationVector{ 2, 0, 1 })); |
| 240 | } |
| 241 | |
| 242 | 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] | 243 | 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] | 244 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 245 | 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] | 246 | TensorInfo proposals_4_roi_values(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 247 | |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 248 | 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] | 249 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 250 | TensorInfo *proposals_4_roi_values_to_use = &proposals_4_roi_values; |
| 251 | TensorInfo proposals_4_roi_values_quantized(deltas->clone()->set_tensor_shape(TensorShape(values_per_roi, total_num_anchors)).set_is_resizable(true)); |
| 252 | proposals_4_roi_values_quantized.set_data_type(DataType::QASYMM16).set_quantization_info(QuantizationInfo(0.125f, 0)); |
| 253 | if(is_qasymm8) |
| 254 | { |
| 255 | 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)); |
| 256 | ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&all_anchors_info, &all_anchors_f32_info)); |
| 257 | |
| 258 | 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)); |
| 259 | ARM_COMPUTE_RETURN_ON_ERROR(CLDequantizationLayerKernel::validate(&deltas_flattened_info, &deltas_flattened_f32_info)); |
| 260 | |
| 261 | 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)); |
| 262 | ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_f32_info, &proposals_4_roi_values_f32, &deltas_flattened_f32_info, |
| 263 | BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f))); |
| 264 | |
| 265 | ARM_COMPUTE_RETURN_ON_ERROR(CLQuantizationLayerKernel::validate(&proposals_4_roi_values_f32, &proposals_4_roi_values_quantized)); |
| 266 | proposals_4_roi_values_to_use = &proposals_4_roi_values_quantized; |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | ARM_COMPUTE_RETURN_ON_ERROR(CLBoundingBoxTransformKernel::validate(&all_anchors_info, &proposals_4_roi_values, &deltas_flattened_info, |
| 271 | BoundingBoxTransformInfo(info.im_width(), info.im_height(), 1.f))); |
| 272 | } |
| 273 | |
| 274 | 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] | 275 | |
| 276 | if(num_valid_proposals->total_size() > 0) |
| 277 | { |
| 278 | ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->num_dimensions() > 1); |
| 279 | ARM_COMPUTE_RETURN_ERROR_ON(num_valid_proposals->dimension(0) > 1); |
| 280 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(num_valid_proposals, 1, DataType::U32); |
| 281 | } |
| 282 | |
| 283 | if(proposals->total_size() > 0) |
| 284 | { |
| 285 | ARM_COMPUTE_RETURN_ERROR_ON(proposals->num_dimensions() > 2); |
| 286 | ARM_COMPUTE_RETURN_ERROR_ON(proposals->dimension(0) != size_t(values_per_roi) + 1); |
| 287 | 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] | 288 | if(is_qasymm8) |
| 289 | { |
| 290 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(proposals, 1, DataType::QASYMM16); |
| 291 | const UniformQuantizationInfo proposals_qinfo = proposals->quantization_info().uniform(); |
| 292 | ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.scale != 0.125f); |
| 293 | ARM_COMPUTE_RETURN_ERROR_ON(proposals_qinfo.offset != 0); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(proposals, scores); |
| 298 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | if(scores_out->total_size() > 0) |
| 302 | { |
| 303 | ARM_COMPUTE_RETURN_ERROR_ON(scores_out->num_dimensions() > 1); |
| 304 | ARM_COMPUTE_RETURN_ERROR_ON(scores_out->dimension(0) != size_t(total_num_anchors)); |
| 305 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(scores_out, scores); |
| 306 | } |
| 307 | |
| 308 | return Status{}; |
| 309 | } |
| 310 | |
| 311 | void CLGenerateProposalsLayer::run_cpp_nms_kernel() |
| 312 | { |
| 313 | // Map inputs |
| 314 | _scores_flattened.map(true); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 315 | _all_proposals_to_use->map(true); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 316 | |
| 317 | // Map outputs |
| 318 | _scores_out->map(CLScheduler::get().queue(), true); |
| 319 | _proposals_4_roi_values.map(CLScheduler::get().queue(), true); |
| 320 | _num_valid_proposals->map(CLScheduler::get().queue(), true); |
| 321 | _keeps_nms_unused.map(true); |
| 322 | _classes_nms_unused.map(true); |
| 323 | |
| 324 | // Run nms |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 325 | _cpp_nms.run(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 326 | |
| 327 | // Unmap outputs |
| 328 | _keeps_nms_unused.unmap(); |
| 329 | _classes_nms_unused.unmap(); |
| 330 | _scores_out->unmap(CLScheduler::get().queue()); |
| 331 | _proposals_4_roi_values.unmap(CLScheduler::get().queue()); |
| 332 | _num_valid_proposals->unmap(CLScheduler::get().queue()); |
| 333 | |
| 334 | // Unmap inputs |
| 335 | _scores_flattened.unmap(); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 336 | _all_proposals_to_use->unmap(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void CLGenerateProposalsLayer::run() |
| 340 | { |
| 341 | // Acquire all the temporaries |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 342 | MemoryGroupResourceScope scope_mg(_memory_group); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 343 | |
| 344 | // Compute all the anchors |
| 345 | CLScheduler::get().enqueue(_compute_anchors_kernel, false); |
| 346 | |
| 347 | // Transpose and reshape the inputs |
| 348 | if(!_is_nhwc) |
| 349 | { |
| 350 | CLScheduler::get().enqueue(_permute_deltas_kernel, false); |
| 351 | CLScheduler::get().enqueue(_permute_scores_kernel, false); |
| 352 | } |
Michalis Spyrou | 2aad21a | 2020-07-02 12:43:53 +0100 | [diff] [blame] | 353 | _flatten_deltas.run(); |
| 354 | _flatten_scores.run(); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 355 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 356 | if(_is_qasymm8) |
| 357 | { |
| 358 | CLScheduler::get().enqueue(_dequantize_anchors, false); |
| 359 | CLScheduler::get().enqueue(_dequantize_deltas, false); |
| 360 | } |
| 361 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 362 | // Build the boxes |
| 363 | CLScheduler::get().enqueue(_bounding_box_kernel, false); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 364 | |
| 365 | if(_is_qasymm8) |
| 366 | { |
| 367 | CLScheduler::get().enqueue(_quantize_all_proposals, false); |
| 368 | } |
| 369 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 370 | // Non maxima suppression |
| 371 | run_cpp_nms_kernel(); |
| 372 | // Add dummy batch indexes |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 373 | CLScheduler::get().enqueue(_pad_kernel, true); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 374 | } |
| 375 | } // namespace arm_compute |