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

# DetectionPostProcess

DetectionPostProcess 算子用于目标检测模型的后处理阶段，对网络预测的候选框和置信度进行解码、筛选和非极大值抑制（NMS），输出最终检测结果。 其主要功能包括： 1. 根据 anchor 信息对候选框进行解码； 2. 对各类别的候选框执行 NMS 操作； 3. 输出选中的检测框、类别及分数。

输入：

  - input_boxes - 检测框坐标数据地址，形状为 `[num_boxes, 4]`。

  - input_scores - 每个检测框对应的置信度分数，形状为 `[num_boxes, num_classes_with_bg_]`。

  - anchors - Anchor 坐标数据地址，形状为 `[num_boxes, 4]`。

  - num_boxes - 候选框数量。

  - num_classes_with_bg - 含背景类在内的类别数量。

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

  - param - 算子参数结构体，包含 NMS 阈值、检测数量限制、坐标缩放因子等。

  结构体定义：

       1typedef struct {
       2    bool use_regular_nms;
       3    int num_classes;
       4    int max_detections;
       5    int max_classes_per_detection;  // Fast NMS使用
       6    int detections_per_class;       // Regular NMS使用
       7    float nms_score_threshold;
       8    float nms_iou_threshold;
       9    float y_scale;
      10    float x_scale;
      11    float h_scale;
      12    float w_scale;
      13    int num_boxes;
      14    int num_classes_with_bg;
      15
      16    void *decoded_boxes;
      17    uint8_t *nms_candidate;
      18    int32_t *selected;
      19    float *scores;
      20    int32_t *indexes;
      21    float *all_class_scores;
      22    int32_t *all_class_indexes;
      23    int32_t *single_class_indexes;
      24
      25    // INT8 量化参数（仅在 Int8 模式下使用）
      26    float boxes_scale;
      27    int32_t boxes_zero_point;
      28    float scores_scale;
      29    int32_t scores_zero_point;
      30
      31} DetectionPostProcessParameter;

输出：

  - output_boxes - 输出检测框坐标。

  - output_classes - 输出检测框类别索引。

  - output_scores - 输出检测框对应的置信度。

  - output_num - 实际输出的检测框数量。

支持平台：

  `FT78NE` `MT7004`

备注

- FT78NE 支持 fp32, int8

- MT7004 支持 fp16, fp32

共享存储版本:

void fp_detection_post_process_s(const float *input_boxes, const float *input_scores, const float *anchors, float *output_boxes, float *output_classes, float *output_scores, float *output_num, DetectionPostProcessParameter *param, int core_mask)

void i8_detection_post_process_s(const int8_t *input_boxes, const int8_t *input_scores, const float *anchors, float *output_boxes, float *output_classes, float *output_scores, float *output_num, DetectionPostProcessParameter *param, int core_mask)

void hp_detection_post_process_s(const half *input_boxes, const half *input_scores, const half *anchors, half *output_boxes, float *output_classes, half *output_scores, float *output_num, DetectionPostProcessParameter *param, int core_mask)

  C调用示例：

       1//FT78NE示例
       2#include <stdio.h>
       3
       4int main(int argc, char* argv[]) {
       5    float *input_boxes = (float *)0xA0000000;
       6    float *input_scores = (float *)0xA1000000;
       7    float *anchors = (float *)0xA2000000;
       8    float *output_boxes = (float *)0xA3000000;
       9    float *output_classes = (float *)0xA4000000;
      10    float *output_scores = (float *)0xA5000000;
      11    float *output_num = (float *)0xA6000000;
      12    DetectionPostProcessParameter* param = (DetectionPostProcessParameter*)0xA7000000;
      13
      14    param->use_regular_nms = true;
      15    param->num_classes = 3;
      16    param->max_detections = 50;
      17    param->max_classes_per_detection = 1;
      18    param->detections_per_class = 50;
      19    param->nms_score_threshold = 0.1f;
      20    param->nms_iou_threshold = 0.5f;
      21    param->y_scale = 10.0f;
      22    param->x_scale = 10.0f;
      23    param->h_scale = 5.0f;
      24    param->w_scale = 5.0f;
      25    param->num_boxes = 100;
      26    param->num_classes_with_bg = 4;
      27
      28    int core_mask = 0xff;
      29
      30    fp_detection_post_process_s(input_boxes, input_scores, anchors, output_boxes, output_classes, output_scores, output_num, param, core_mask);
      31    return 0;
      32}

私有存储版本:

void fp_detection_post_process_p(const float *input_boxes, const float *input_scores, const float *anchors, float *output_boxes, float *output_classes, float *output_scores, float *output_num, DetectionPostProcessParameter *param)

void i8_detection_post_process_p(const int8_t *input_boxes, const int8_t *input_scores, const float *anchors, float *output_boxes, float *output_classes, float *output_scores, float *output_num, DetectionPostProcessParameter *param)

void hp_detection_post_process_p(const half *input_boxes, const half *input_scores, const half *anchors, half *output_boxes, float *output_classes, half *output_scores, float *output_num, DetectionPostProcessParameter *param)

  C调用示例：

       1//MT7004示例
       2#include <stdio.h>
       3
       4int main(int argc, char* argv[]) {
       5    DetectionPostProcessParameter param;
       6
       7    param.use_regular_nms = 1;
       8    param.num_classes = 3;
       9    param.max_detections = 50;
      10    param.max_classes_per_detection = 1;
      11    param.detections_per_class = 50;
      12    param.nms_score_threshold = 0.1f;
      13    param.nms_iou_threshold = 0.5f;
      14    param.y_scale = 10.0f;
      15    param.x_scale = 10.0f;
      16    param.h_scale = 5.0f;
      17    param.w_scale = 5.0f;
      18    param.num_boxes = 1917;
      19    param.num_classes_with_bg = 4;
      20
      21    float *input_boxes = (float *)0xA0000000;
      22    float *input_scores = (float *)0xA1000000;
      23    float *anchors = (float *)0xA2000000;
      24    float *output_boxes = (float *)0xA3000000;
      25    float *output_classes = (float *)0xA4000000;
      26    float *output_scores = (float *)0xA5000000;
      27    float *output_num = (float *)0xA6000000;
      28
      29    fp_detection_post_process_p(input_boxes, input_scores, anchors, output_boxes, output_classes, output_scores, output_num, param);
      30    return 0;
      31}