/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NON_MAX_SUPPRESSION_H_ #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NON_MAX_SUPPRESSION_H_ #include <algorithm> #include <cmath> #include <deque> #include <queue> namespace tflite { namespace reference_ops { // A pair of diagonal corners of the box. struct BoxCornerEncoding { … }; inline float ComputeIntersectionOverUnion(const float* boxes, const int i, const int j) { … } // Implements (Single-Class) Soft NMS (with Gaussian weighting). // Supports functionality of TensorFlow ops NonMaxSuppressionV4 & V5. // Reference: "Soft-NMS - Improving Object Detection With One Line of Code" // [Bodla et al, https://arxiv.org/abs/1704.04503] // Implementation adapted from the TensorFlow NMS code at // tensorflow/core/kernels/non_max_suppression_op.cc. // // Arguments: // boxes: box encodings in format [y1, x1, y2, x2], shape: [num_boxes, 4] // num_boxes: number of candidates // scores: scores for candidate boxes, in the same order. shape: [num_boxes] // max_output_size: the maximum number of selections. // iou_threshold: Intersection-over-Union (IoU) threshold for NMS // score_threshold: All candidate scores below this value are rejected // soft_nms_sigma: Soft NMS parameter, used for decaying scores // // Outputs: // selected_indices: all the selected indices. Underlying array must have // length >= max_output_size. Cannot be null. // selected_scores: scores of selected indices. Defer from original value for // Soft NMS. If not null, array must have length >= max_output_size. // num_selected_indices: Number of selections. Only these many elements are // set in selected_indices, selected_scores. Cannot be null. // // Assumes inputs are valid (for eg, iou_threshold must be >= 0). inline void NonMaxSuppression(const float* boxes, const int num_boxes, const float* scores, const int max_output_size, const float iou_threshold, const float score_threshold, const float soft_nms_sigma, int* selected_indices, float* selected_scores, int* num_selected_indices) { … } } // namespace reference_ops } // namespace tflite #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_NON_MAX_SUPPRESSION_H_