39 Status validate_arguments(
const ITensorInfo *input_loc,
const ITensorInfo *input_conf,
const ITensorInfo *input_priorbox,
const ITensorInfo *output, DetectionOutputLayerInfo
info)
50 const int num_priors = input_priorbox->tensor_shape()[0] / 4;
51 ARM_COMPUTE_RETURN_ERROR_ON_MSG(static_cast<size_t>((num_priors * info.num_loc_classes() * 4)) != input_loc->tensor_shape()[0],
"Number of priors must match number of location predictions.");
52 ARM_COMPUTE_RETURN_ERROR_ON_MSG(static_cast<size_t>((num_priors * info.num_classes())) != input_conf->tensor_shape()[0],
"Number of priors must match number of confidence predictions.");
55 if(output->total_size() != 0)
57 const unsigned int max_size = info.keep_top_k() * (input_loc->num_dimensions() > 1 ? input_loc->dimension(1) : 1);
68 bool SortScorePairDescend(
const std::pair<float, T> &pair1,
69 const std::pair<float, T> &pair2)
71 return pair1.first > pair2.first;
85 void retrieve_all_loc_predictions(
const ITensor *input_loc,
const int num,
86 const int num_priors,
const int num_loc_classes,
87 const bool share_location, std::vector<LabelBBox> &all_location_predictions)
89 for(
int i = 0; i < num; ++i)
91 for(
int c = 0; c < num_loc_classes; ++c)
93 int label = share_location ? -1 : c;
94 if(all_location_predictions[i].find(label) == all_location_predictions[i].
end())
96 all_location_predictions[i][label].resize(num_priors);
100 ARM_COMPUTE_ERROR_ON(all_location_predictions[i][label].size() != static_cast<size_t>(num_priors));
105 for(
int i = 0; i < num; ++i)
107 for(
int p = 0; p < num_priors; ++p)
109 for(
int c = 0; c < num_loc_classes; ++c)
111 const int label = share_location ? -1 : c;
112 const int base_ptr = i * num_priors * num_loc_classes * 4 + p * num_loc_classes * 4 + c * 4;
114 all_location_predictions[i][label][p][0] = *
reinterpret_cast<float *
>(input_loc->ptr_to_element(Coordinates(base_ptr)));
115 all_location_predictions[i][label][p][1] = *
reinterpret_cast<float *
>(input_loc->ptr_to_element(Coordinates(base_ptr + 1)));
116 all_location_predictions[i][label][p][2] = *
reinterpret_cast<float *
>(input_loc->ptr_to_element(Coordinates(base_ptr + 2)));
117 all_location_predictions[i][label][p][3] = *
reinterpret_cast<float *
>(input_loc->ptr_to_element(Coordinates(base_ptr + 3)));
133 void retrieve_all_conf_scores(
const ITensor *input_conf,
const int num,
134 const int num_priors,
const int num_classes,
135 std::vector<
std::map<
int, std::vector<float>>> &all_confidence_scores)
137 std::vector<float> tmp_buffer;
138 tmp_buffer.resize(num * num_priors * num_classes);
139 for(
int i = 0; i < num; ++i)
141 for(
int c = 0; c < num_classes; ++c)
143 for(
int p = 0; p < num_priors; ++p)
145 tmp_buffer[i * num_classes * num_priors + c * num_priors + p] =
146 *
reinterpret_cast<float *
>(input_conf->ptr_to_element(Coordinates(i * num_classes * num_priors + p * num_classes + c)));
150 for(
int i = 0; i < num; ++i)
152 for(
int c = 0; c < num_classes; ++c)
154 all_confidence_scores[i][c].resize(num_priors);
155 all_confidence_scores[i][c].assign(&tmp_buffer[i * num_classes * num_priors + c * num_priors],
156 &tmp_buffer[i * num_classes * num_priors + c * num_priors + num_priors]);
171 void retrieve_all_priorbox(
const ITensor *input_priorbox,
172 const int num_priors,
173 std::vector<BBox> &all_prior_bboxes,
174 std::vector<std::array<float, 4>> &all_prior_variances)
176 for(
int i = 0; i < num_priors; ++i)
178 all_prior_bboxes[i] =
181 *
reinterpret_cast<float *
>(input_priorbox->ptr_to_element(Coordinates(i * 4))),
182 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 1))),
183 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 2))),
184 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 3)))
189 std::array<float, 4> var({ { 0, 0, 0, 0 } });
190 for(
int i = 0; i < num_priors; ++i)
192 for(
int j = 0; j < 4; ++j)
194 var[j] = *
reinterpret_cast<float *
>(input_priorbox->ptr_to_element(Coordinates((num_priors + i) * 4 + j)));
196 all_prior_variances[i] = var;
211 void DecodeBBox(
const BBox &prior_bbox,
const std::array<float, 4> &prior_variance,
213 const bool clip_bbox,
const BBox &bbox,
BBox &decode_bbox)
221 decode_bbox[0] = prior_bbox[0] + (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]);
222 decode_bbox[1] = prior_bbox[1] + (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]);
223 decode_bbox[2] = prior_bbox[2] + (variance_encoded_in_target ? bbox[2] : prior_variance[2] * bbox[2]);
224 decode_bbox[3] = prior_bbox[3] + (variance_encoded_in_target ? bbox[3] : prior_variance[3] * bbox[3]);
230 const float prior_width = prior_bbox[2] - prior_bbox[0];
231 const float prior_height = prior_bbox[3] - prior_bbox[1];
237 const float prior_center_x = (prior_bbox[0] + prior_bbox[2]) / 2.;
238 const float prior_center_y = (prior_bbox[1] + prior_bbox[3]) / 2.;
240 const float decode_bbox_center_x = (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]) * prior_width + prior_center_x;
241 const float decode_bbox_center_y = (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]) * prior_height + prior_center_y;
242 const float decode_bbox_width = (variance_encoded_in_target ? std::exp(bbox[2]) :
std::exp(prior_variance[2] * bbox[2])) * prior_width;
243 const float decode_bbox_height = (variance_encoded_in_target ? std::exp(bbox[3]) :
std::exp(prior_variance[3] * bbox[3])) * prior_height;
245 decode_bbox[0] = (decode_bbox_center_x - decode_bbox_width / 2.f);
246 decode_bbox[1] = (decode_bbox_center_y - decode_bbox_height / 2.f);
247 decode_bbox[2] = (decode_bbox_center_x + decode_bbox_width / 2.f);
248 decode_bbox[3] = (decode_bbox_center_y + decode_bbox_height / 2.f);
254 const float prior_width = prior_bbox[2] - prior_bbox[0];
255 const float prior_height = prior_bbox[3] - prior_bbox[1];
261 decode_bbox[0] = prior_bbox[0] + (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]) * prior_width;
262 decode_bbox[1] = prior_bbox[1] + (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]) * prior_height;
263 decode_bbox[2] = prior_bbox[2] + (variance_encoded_in_target ? bbox[2] : prior_variance[2] * bbox[2]) * prior_width;
264 decode_bbox[3] = prior_bbox[3] + (variance_encoded_in_target ? bbox[3] : prior_variance[3] * bbox[3]) * prior_height;
274 for(
auto &d_bbox : decode_bbox)
292 void ApplyNMSFast(
const std::vector<BBox> &bboxes,
293 const std::vector<float> &scores,
const float score_threshold,
294 const float nms_threshold,
const float eta,
const int top_k,
295 std::vector<int> &indices)
300 std::list<std::pair<float, int>> score_index_vec;
303 for(
size_t i = 0; i < scores.size(); ++i)
305 if(scores[i] > score_threshold)
307 score_index_vec.emplace_back(std::make_pair(scores[i], i));
312 score_index_vec.sort(SortScorePairDescend<int>);
315 const int score_index_vec_size = score_index_vec.size();
316 if(top_k > -1 && top_k < score_index_vec_size)
318 score_index_vec.resize(top_k);
322 float adaptive_threshold = nms_threshold;
325 while(!score_index_vec.empty())
327 const int idx = score_index_vec.front().second;
329 for(
int kept_idx : indices)
334 BBox intersect_bbox = std::array<float, 4>({ 0, 0, 0, 0 });
335 if(bboxes[kept_idx][0] > bboxes[idx][2] || bboxes[kept_idx][2] < bboxes[idx][0] || bboxes[kept_idx][1] > bboxes[idx][3] || bboxes[kept_idx][3] < bboxes[idx][1])
337 intersect_bbox = std::array<float, 4>({ { 0, 0, 0, 0 } });
341 intersect_bbox = std::array<float, 4>({ {
342 std::max(bboxes[idx][0], bboxes[kept_idx][0]),
343 std::max(bboxes[idx][1], bboxes[kept_idx][1]),
344 std::min(bboxes[idx][2], bboxes[kept_idx][2]),
345 std::min(bboxes[idx][3], bboxes[kept_idx][3])
350 float intersect_width = intersect_bbox[2] - intersect_bbox[0];
351 float intersect_height = intersect_bbox[3] - intersect_bbox[1];
354 if(intersect_width > 0 && intersect_height > 0)
356 float intersect_size = intersect_width * intersect_height;
357 float bbox1_size = (bboxes[idx][2] < bboxes[idx][0]
358 || bboxes[idx][3] < bboxes[idx][1]) ?
360 (bboxes[idx][2] - bboxes[idx][0]) * (bboxes[idx][3] - bboxes[idx][1]);
361 float bbox2_size = (bboxes[kept_idx][2] < bboxes[kept_idx][0]
362 || bboxes[kept_idx][3] < bboxes[kept_idx][1]) ?
364 (bboxes[kept_idx][2] - bboxes[kept_idx][0]) * (bboxes[kept_idx][3] - bboxes[kept_idx][1]);
365 overlap = intersect_size / (bbox1_size + bbox2_size - intersect_size);
367 keep = (overlap <= adaptive_threshold);
376 indices.push_back(idx);
378 score_index_vec.erase(score_index_vec.begin());
379 if(keep && eta < 1.f && adaptive_threshold > 0.5f)
381 adaptive_threshold *= eta;
388 : _input_loc(nullptr), _input_conf(nullptr), _input_priorbox(nullptr), _output(nullptr), _info(), _num_priors(), _num(), _all_location_predictions(), _all_confidence_scores(), _all_prior_bboxes(),
389 _all_prior_variances(), _all_decode_bboxes(), _all_indices()
409 _input_loc = input_loc;
410 _input_conf = input_conf;
411 _input_priorbox = input_priorbox;
417 _all_location_predictions.resize(_num);
418 _all_confidence_scores.resize(_num);
419 _all_prior_bboxes.resize(_num_priors);
420 _all_prior_variances.resize(_num_priors);
421 _all_decode_bboxes.resize(_num);
423 for(
int i = 0; i < _num; ++i)
433 _all_decode_bboxes[i][label].resize(_num_priors);
436 _all_indices.resize(_num);
455 retrieve_all_conf_scores(_input_conf, _num, _num_priors, _info.
num_classes(), _all_confidence_scores);
458 retrieve_all_priorbox(_input_priorbox, _num_priors, _all_prior_bboxes, _all_prior_variances);
461 const bool clip_bbox =
false;
462 for(
int i = 0; i < _num; ++i)
472 ARM_COMPUTE_ERROR_ON_MSG_VAR(_all_location_predictions[i].find(label) == _all_location_predictions[i].
end(),
"Could not find location predictions for label %d.", label);
474 const std::vector<BBox> &label_loc_preds = _all_location_predictions[i].find(label)->second;
476 const int num_bboxes = _all_prior_bboxes.size();
479 for(
int j = 0; j < num_bboxes; ++j)
481 DecodeBBox(_all_prior_bboxes[j], _all_prior_variances[j], _info.
code_type(), _info.
variance_encoded_in_target(), clip_bbox, label_loc_preds[j], _all_decode_bboxes[i][label][j]);
488 for(
int i = 0; i < _num; ++i)
490 const LabelBBox &decode_bboxes = _all_decode_bboxes[i];
491 const std::map<int, std::vector<float>> &conf_scores = _all_confidence_scores[i];
493 std::map<int, std::vector<int>> indices;
503 if(conf_scores.find(c) == conf_scores.end() || decode_bboxes.find(label) == decode_bboxes.end())
507 const std::vector<float> &scores = conf_scores.find(c)->second;
508 const std::vector<BBox> &bboxes = decode_bboxes.find(label)->second;
512 num_det += indices[c].size();
518 std::vector<std::pair<float, std::pair<int, int>>> score_index_pairs;
519 for(
auto const &it : indices)
521 const int label = it.first;
522 const std::vector<int> &label_indices = it.second;
524 if(conf_scores.find(label) == conf_scores.end())
529 const std::vector<float> &scores = conf_scores.find(label)->second;
530 for(
auto idx : label_indices)
533 score_index_pairs.emplace_back(std::make_pair(scores[idx], std::make_pair(label, idx)));
538 std::sort(score_index_pairs.begin(), score_index_pairs.end(), SortScorePairDescend<std::pair<int, int>>);
543 std::map<int, std::vector<int>> new_indices;
544 for(
auto score_index_pair : score_index_pairs)
546 int label = score_index_pair.second.first;
547 int idx = score_index_pair.second.second;
548 new_indices[label].push_back(idx);
550 _all_indices[i] = new_indices;
555 _all_indices[i] = indices;
556 num_to_add = num_det;
558 num_kept += num_to_add;
565 for(
int i = 0; i < _num; ++i)
567 const std::map<int, std::vector<float>> &conf_scores = _all_confidence_scores[i];
568 const LabelBBox &decode_bboxes = _all_decode_bboxes[i];
569 for(
auto &it : _all_indices[i])
571 const int label = it.first;
572 const std::vector<float> &scores = conf_scores.find(label)->second;
574 if(conf_scores.find(label) == conf_scores.end() || decode_bboxes.find(loc_label) == decode_bboxes.end())
580 const std::vector<BBox> &bboxes = decode_bboxes.find(loc_label)->second;
581 const std::vector<int> &indices = it.second;
583 for(
auto idx : indices)
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
int num_classes() const
Get num classes.
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
CPPDetectionOutputLayer()
Default constructor.
bool share_location() const
Get share location.
Use box centers and size.
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
void configure(const ITensor *input_loc, const ITensor *input_conf, const ITensor *input_priorbox, ITensor *output, DetectionOutputLayerInfo info=DetectionOutputLayerInfo())
Configure the detection output layer CPP kernel.
std::map< int, std::vector< BBox > > LabelBBox
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
1 channel, 1 F32 per channel
#define ARM_COMPUTE_ERROR_VAR(msg,...)
Print the given message then throw an std::runtime_error.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
#define ARM_COMPUTE_ERROR_ON_MSG_VAR(cond, msg,...)
float eta() const
Get eta.
DetectionOutputLayerCodeType
Available Detection Output code types.
Interface for CPU tensor.
void run() override
Run the kernels contained in the function.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
int top_k() const
Get top K.
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
DataType clamp(const DataType &n, const DataType &lower=std::numeric_limits< RangeType >::lowest(), const DataType &upper=std::numeric_limits< RangeType >::max())
Performs clamping among a lower and upper value.
bool variance_encoded_in_target() const
Get if variance encoded in target.
float nms_threshold() const
Get nms threshold.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
std::array< float, 4 > BBox
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
DetectionOutputLayerCodeType code_type() const
Get detection output code type.
void end(TokenStream &in, bool &valid)
int keep_top_k() const
Get the number of total bounding boxes to be kept per image.
Detection Output layer info.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
void map(T &tensor, bool blocking)
Maps a tensor if needed.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
static Status validate(const ITensorInfo *input_loc, const ITensorInfo *input_conf, const ITensorInfo *input_priorbox, const ITensorInfo *output, DetectionOutputLayerInfo info=DetectionOutputLayerInfo())
Static function to check if given info will lead to a valid configuration of CPPDetectionOutputLayer...
Use box centers and size.
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
#define ARM_COMPUTE_LOG_PARAMS(...)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Container for valid region of a window.
int background_label_id() const
Get background label ID.
float confidence_threshold() const
Get confidence threshold.
int num_loc_classes() const
Get number of location classes.