# NonMaxSuppression — MindSpore Signal+ 使用手册 alpha 文档

# NonMaxSuppression

对检测框进行非极大值抑制（Non-Maximum Suppression, NMS），用于在目标检测中选取得分较高且重叠度低的边界框，并去除那些重叠度较高的低得分框。其目的是减少冗余边界框，提高目标检测的准确性。

\[ \begin{align}\begin{aligned}\text{对于每个类别 } c, \text{在同一批次下保留置信度较高且 IoU 小于阈值的框:}\\\text{若 } \mathrm{IoU}(box_i, box_j) > \text{iou\_threshold} \Rightarrow \text{抑制 } box_j,\end{aligned}\end{align} \]
\[IoU=\frac{intersec\_area}{(cand.get\_area() + box.get\_area() - intersec\_area)}\]

输入：

  - NmsParam - 输入参数结构体。

  - core_mask(int, 可选) - 核掩码（仅适用于共享存储版本）。

  结构体定义：

       1typedef struct {
       2    float* boxes;               // 形状为 (batch, num_boxes, 4)
       3    float* score;               // 形状为 (batch, class, num_boxes)
       4    int* output;                // 输出缓冲区
       5    float* candidates;          // 候选框临时缓冲区
       6    int batch_num;              // 批次数量
       7    int class_num;              // 类别数量
       8    int box_num;                // 每个类别的框数量
       9    int center_point_box;       // 0: corner box, 1: center point box
      10    bool simple_out;            // true: 仅输出index, false: 输出 batch, class, index
      11    int max_output_per_class;   // 每个类别最大输出框数
      12    float iou_threshold;        // IoU 阈值
      13    float score_threshold;      // 分数阈值
      14} NmsParamFp32;
      15typedef struct {
      16    int8_t* boxes;               // 形状为 (batch, num_boxes, 4)
      17    int8_t* score;               // 形状为 (batch, class, num_boxes)
      18    int* output;                // 输出缓冲区
      19    float* candidates;          // 候选框临时缓冲区
      20    int batch_num;              // 批次数量
      21    int class_num;              // 类别数量
      22    int box_num;                // 每个类别的框数量
      23    int center_point_box;       // 0: corner box, 1: center point box
      24    bool simple_out;            // true: 仅输出index, false: 输出 batch, class, index
      25    int max_output_per_class;   // 每个类别最大输出框数
      26    float iou_threshold;        // IoU 阈值
      27    int8_t score_threshold;      // 分数阈值
      28} NmsParamInt8;
      29typedef struct {
      30    float16* boxes;
      31    float16* score;
      32    int* output;
      33    float16* candidates;
      34    uint64_t batch_num;
      35    uint64_t class_num;
      36    uint64_t box_num;
      37    uint64_t center_point_box;
      38    uint64_t simple_out;
      39    uint64_t max_output_per_class;
      40    float iou_threshold_bits;
      41    float score_threshold_bits;
      42} NmsParamFp16;

支持平台：

  `FT78NE` `MT7004`

备注

- FT78NE 支持 fp32, int8

- MT7004 支持 fp16, fp32

- MT7004中使用结构体作为参数传入。

共享存储版本:

void i8_non_max_suppression_s(NmsParamInt8 *param, int core_mask)

void fp_non_max_suppression_s(NmsParamFp32 *param, int core_mask)

void hp_non_max_suppression_s(NmsParamFp16 *param, int core_mask)

  C调用示例：

       1//MT7004示例
       2#include <stdio.h>
       3
       4int main(int argc, char* argv[]) {
       5    float *boxes = (float *)0xA0000000;
       6    float *score = (float *)0xA1000000;
       7    int *output = (int *)0xA2000000;
       8    NmsParamFp32* param = (NmsParamFp32*)0xA3000000;
       9
      10    param->boxes = boxes;
      11    param->score = score;
      12    param->output = output;
      13    param->candidates = candidates;
      14    param->batch_num = 1;
      15    param->class_num = 3;
      16    param->box_num = 100;
      17    param->center_point_box = 0;
      18    param->simple_out = 1;
      19    param->max_output_per_class = 50;
      20    param->iou_threshold_bits = 0.5;
      21    param->score_threshold_bits = 0.1;
      22
      23    int core_mask = 0xff;
      24    fp_non_max_suppression_s(param, core_mask);
      25    return 0;
      26}

私有存储版本:

void i8_non_max_suppression_p(NmsParamInt8 *param)

void fp_non_max_suppression_p(NmsParamFp32 *param)

void hp_non_max_suppression_p(NmsParamFp16 *param)

  C调用示例：

       1//FT78NE示例
       2#include <stdio.h>
       3
       4int main(int argc, char* argv[]) {
       5    float *boxes = (float *)0x1000000;
       6    float *score = (float *)0xA1000000;
       7    int *output = (int *)0xA2000000;
       8    NmsParamFp32* param = (NmsParamFp32*)0xA3000000;
       9
      10    param->boxes = boxes;
      11    param->score = score;
      12    param->output = output;
      13    param->candidates = candidates;
      14    param->batch_num = 1;
      15    param->class_num = 3;
      16    param->box_num = 100;
      17    param->center_point_box = 0;
      18    param->simple_out = 1;
      19    param->max_output_per_class = 50;
      20    param->iou_threshold_bits = 0.5;
      21    param->score_threshold_bits = 0.1;
      22
      23    fp_non_max_suppression_s(param);
      24    return 0;
      25}