37 Status
validate_arguments(
const ITensorInfo *input_loc,
const ITensorInfo *input_conf,
const ITensorInfo *input_priorbox,
const ITensorInfo *output, DetectionOutputLayerInfo
info)
48 const int num_priors = input_priorbox->tensor_shape()[0] / 4;
49 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.");
50 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.");
53 if(output->total_size() != 0)
55 const unsigned int max_size =
info.keep_top_k() * (input_loc->num_dimensions() > 1 ? input_loc->dimension(1) : 1);
66 bool SortScorePairDescend(
const std::pair<float, T> &pair1,
67 const std::pair<float, T> &pair2)
69 return pair1.first > pair2.first;
83 void retrieve_all_loc_predictions(
const ITensor *input_loc,
const int num,
84 const int num_priors,
const int num_loc_classes,
85 const bool share_location, std::vector<LabelBBox> &all_location_predictions)
87 for(
int i = 0; i < num; ++i)
89 for(
int c = 0; c < num_loc_classes; ++c)
91 int label = share_location ? -1 : c;
92 if(all_location_predictions[i].find(label) == all_location_predictions[i].
end())
94 all_location_predictions[i][label].resize(num_priors);
98 ARM_COMPUTE_ERROR_ON(all_location_predictions[i][label].size() != static_cast<size_t>(num_priors));
103 for(
int i = 0; i < num; ++i)
105 for(
int p = 0; p < num_priors; ++p)
107 for(
int c = 0; c < num_loc_classes; ++c)
109 const int label = share_location ? -1 : c;
110 const int base_ptr = i * num_priors * num_loc_classes * 4 + p * num_loc_classes * 4 + c * 4;
112 all_location_predictions[i][label][p][0] = *reinterpret_cast<float *>(input_loc->ptr_to_element(Coordinates(base_ptr)));
113 all_location_predictions[i][label][p][1] = *reinterpret_cast<float *>(input_loc->ptr_to_element(Coordinates(base_ptr + 1)));
114 all_location_predictions[i][label][p][2] = *reinterpret_cast<float *>(input_loc->ptr_to_element(Coordinates(base_ptr + 2)));
115 all_location_predictions[i][label][p][3] = *reinterpret_cast<float *>(input_loc->ptr_to_element(Coordinates(base_ptr + 3)));
131 void retrieve_all_conf_scores(
const ITensor *input_conf,
const int num,
132 const int num_priors,
const int num_classes,
133 std::vector<
std::map<
int, std::vector<float>>> &all_confidence_scores)
135 std::vector<float> tmp_buffer;
136 tmp_buffer.resize(num * num_priors * num_classes);
137 for(
int i = 0; i < num; ++i)
139 for(
int c = 0; c < num_classes; ++c)
141 for(
int p = 0; p < num_priors; ++p)
143 tmp_buffer[i * num_classes * num_priors + c * num_priors + p] =
144 *reinterpret_cast<float *>(input_conf->ptr_to_element(Coordinates(i * num_classes * num_priors + p * num_classes + c)));
148 for(
int i = 0; i < num; ++i)
150 for(
int c = 0; c < num_classes; ++c)
152 all_confidence_scores[i][c].resize(num_priors);
153 all_confidence_scores[i][c].assign(&tmp_buffer[i * num_classes * num_priors + c * num_priors],
154 &tmp_buffer[i * num_classes * num_priors + c * num_priors + num_priors]);
169 void retrieve_all_priorbox(
const ITensor *input_priorbox,
170 const int num_priors,
171 std::vector<BBox> &all_prior_bboxes,
172 std::vector<std::array<float, 4>> &all_prior_variances)
174 for(
int i = 0; i < num_priors; ++i)
176 all_prior_bboxes[i] =
179 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4))),
180 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 1))),
181 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 2))),
182 *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates(i * 4 + 3)))
187 std::array<float, 4> var({ { 0, 0, 0, 0 } });
188 for(
int i = 0; i < num_priors; ++i)
190 for(
int j = 0; j < 4; ++j)
192 var[j] = *reinterpret_cast<float *>(input_priorbox->ptr_to_element(Coordinates((num_priors + i) * 4 + j)));
194 all_prior_variances[i] = var;
209 void DecodeBBox(
const BBox &prior_bbox,
const std::array<float, 4> &prior_variance,
211 const bool clip_bbox,
const BBox &bbox,
BBox &decode_bbox)
219 decode_bbox[0] = prior_bbox[0] + (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]);
220 decode_bbox[1] = prior_bbox[1] + (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]);
221 decode_bbox[2] = prior_bbox[2] + (variance_encoded_in_target ? bbox[2] : prior_variance[2] * bbox[2]);
222 decode_bbox[3] = prior_bbox[3] + (variance_encoded_in_target ? bbox[3] : prior_variance[3] * bbox[3]);
228 const float prior_width = prior_bbox[2] - prior_bbox[0];
229 const float prior_height = prior_bbox[3] - prior_bbox[1];
235 const float prior_center_x = (prior_bbox[0] + prior_bbox[2]) / 2.;
236 const float prior_center_y = (prior_bbox[1] + prior_bbox[3]) / 2.;
238 const float decode_bbox_center_x = (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]) * prior_width + prior_center_x;
239 const float decode_bbox_center_y = (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]) * prior_height + prior_center_y;
240 const float decode_bbox_width = (variance_encoded_in_target ? std::exp(bbox[2]) : std::exp(prior_variance[2] * bbox[2])) * prior_width;
241 const float decode_bbox_height = (variance_encoded_in_target ? std::exp(bbox[3]) : std::exp(prior_variance[3] * bbox[3])) * prior_height;
243 decode_bbox[0] = (decode_bbox_center_x - decode_bbox_width / 2.f);
244 decode_bbox[1] = (decode_bbox_center_y - decode_bbox_height / 2.f);
245 decode_bbox[2] = (decode_bbox_center_x + decode_bbox_width / 2.f);
246 decode_bbox[3] = (decode_bbox_center_y + decode_bbox_height / 2.f);
252 const float prior_width = prior_bbox[2] - prior_bbox[0];
253 const float prior_height = prior_bbox[3] - prior_bbox[1];
259 decode_bbox[0] = prior_bbox[0] + (variance_encoded_in_target ? bbox[0] : prior_variance[0] * bbox[0]) * prior_width;
260 decode_bbox[1] = prior_bbox[1] + (variance_encoded_in_target ? bbox[1] : prior_variance[1] * bbox[1]) * prior_height;
261 decode_bbox[2] = prior_bbox[2] + (variance_encoded_in_target ? bbox[2] : prior_variance[2] * bbox[2]) * prior_width;
262 decode_bbox[3] = prior_bbox[3] + (variance_encoded_in_target ? bbox[3] : prior_variance[3] * bbox[3]) * prior_height;
272 for(
auto &d_bbox : decode_bbox)
290 void ApplyNMSFast(
const std::vector<BBox> &bboxes,
291 const std::vector<float> &scores,
const float score_threshold,
292 const float nms_threshold,
const float eta,
const int top_k,
293 std::vector<int> &indices)
298 std::list<std::pair<float, int>> score_index_vec;
301 for(
size_t i = 0; i < scores.size(); ++i)
303 if(scores[i] > score_threshold)
305 score_index_vec.emplace_back(std::make_pair(scores[i], i));
310 score_index_vec.sort(SortScorePairDescend<int>);
313 const int score_index_vec_size = score_index_vec.size();
314 if(top_k > -1 && top_k < score_index_vec_size)
316 score_index_vec.resize(top_k);
320 float adaptive_threshold = nms_threshold;
323 while(!score_index_vec.empty())
325 const int idx = score_index_vec.front().second;
327 for(
int kept_idx : indices)
332 BBox intersect_bbox = std::array<float, 4>({ 0, 0, 0, 0 });
333 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])
335 intersect_bbox = std::array<float, 4>({ { 0, 0, 0, 0 } });
339 intersect_bbox = std::array<float, 4>({ {
340 std::max(bboxes[idx][0], bboxes[kept_idx][0]),
341 std::max(bboxes[idx][1], bboxes[kept_idx][1]),
342 std::min(bboxes[idx][2], bboxes[kept_idx][2]),
343 std::min(bboxes[idx][3], bboxes[kept_idx][3])
348 float intersect_width = intersect_bbox[2] - intersect_bbox[0];
349 float intersect_height = intersect_bbox[3] - intersect_bbox[1];
352 if(intersect_width > 0 && intersect_height > 0)
354 float intersect_size = intersect_width * intersect_height;
355 float bbox1_size = (bboxes[idx][2] < bboxes[idx][0]
356 || bboxes[idx][3] < bboxes[idx][1]) ?
358 (bboxes[idx][2] - bboxes[idx][0]) * (bboxes[idx][3] - bboxes[idx][1]);
359 float bbox2_size = (bboxes[kept_idx][2] < bboxes[kept_idx][0]
360 || bboxes[kept_idx][3] < bboxes[kept_idx][1]) ?
362 (bboxes[kept_idx][2] - bboxes[kept_idx][0]) * (bboxes[kept_idx][3] - bboxes[kept_idx][1]);
363 overlap = intersect_size / (bbox1_size + bbox2_size - intersect_size);
365 keep = (overlap <= adaptive_threshold);
374 indices.push_back(idx);
376 score_index_vec.erase(score_index_vec.begin());
377 if(keep && eta < 1.f && adaptive_threshold > 0.5f)
379 adaptive_threshold *= eta;
386 : _input_loc(nullptr), _input_conf(nullptr), _input_priorbox(nullptr), _output(nullptr), _info(), _num_priors(), _num(), _all_location_predictions(), _all_confidence_scores(), _all_prior_bboxes(),
387 _all_prior_variances(), _all_decode_bboxes(), _all_indices()
404 _input_loc = input_loc;
405 _input_conf = input_conf;
406 _input_priorbox = input_priorbox;
412 _all_location_predictions.resize(_num);
413 _all_confidence_scores.resize(_num);
414 _all_prior_bboxes.resize(_num_priors);
415 _all_prior_variances.resize(_num_priors);
416 _all_decode_bboxes.resize(_num);
418 for(
int i = 0; i < _num; ++i)
428 _all_decode_bboxes[i][label].resize(_num_priors);
431 _all_indices.resize(_num);
450 retrieve_all_conf_scores(_input_conf, _num, _num_priors, _info.
num_classes(), _all_confidence_scores);
453 retrieve_all_priorbox(_input_priorbox, _num_priors, _all_prior_bboxes, _all_prior_variances);
456 const bool clip_bbox =
false;
457 for(
int i = 0; i < _num; ++i)
467 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);
469 const std::vector<BBox> &label_loc_preds = _all_location_predictions[i].find(label)->second;
471 const int num_bboxes = _all_prior_bboxes.size();
474 for(
int j = 0; j < num_bboxes; ++j)
476 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]);
483 for(
int i = 0; i < _num; ++i)
485 const LabelBBox &decode_bboxes = _all_decode_bboxes[i];
486 const std::map<int, std::vector<float>> &conf_scores = _all_confidence_scores[i];
488 std::map<int, std::vector<int>> indices;
498 if(conf_scores.find(c) == conf_scores.end() || decode_bboxes.find(label) == decode_bboxes.end())
502 const std::vector<float> &scores = conf_scores.find(c)->second;
503 const std::vector<BBox> &bboxes = decode_bboxes.find(label)->second;
507 num_det += indices[c].size();
513 std::vector<std::pair<float, std::pair<int, int>>> score_index_pairs;
514 for(
auto const &it : indices)
516 const int label = it.first;
517 const std::vector<int> &label_indices = it.second;
519 if(conf_scores.find(label) == conf_scores.end())
524 const std::vector<float> &scores = conf_scores.find(label)->second;
525 for(
auto idx : label_indices)
528 score_index_pairs.emplace_back(std::make_pair(scores[idx], std::make_pair(label, idx)));
533 std::sort(score_index_pairs.begin(), score_index_pairs.end(), SortScorePairDescend<std::pair<int, int>>);
538 std::map<int, std::vector<int>> new_indices;
539 for(
auto score_index_pair : score_index_pairs)
541 int label = score_index_pair.second.first;
542 int idx = score_index_pair.second.second;
543 new_indices[label].push_back(idx);
545 _all_indices[i] = new_indices;
550 _all_indices[i] = indices;
551 num_to_add = num_det;
553 num_kept += num_to_add;
560 for(
int i = 0; i < _num; ++i)
562 const std::map<int, std::vector<float>> &conf_scores = _all_confidence_scores[i];
563 const LabelBBox &decode_bboxes = _all_decode_bboxes[i];
564 for(
auto &it : _all_indices[i])
566 const int label = it.first;
567 const std::vector<float> &scores = conf_scores.find(label)->second;
569 if(conf_scores.find(label) == conf_scores.end() || decode_bboxes.find(loc_label) == decode_bboxes.end())
575 const std::vector<BBox> &bboxes = decode_bboxes.find(loc_label)->second;
576 const std::vector<int> &indices = it.second;
578 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,...)
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
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_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.