• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_COMBINED_NON_MAX_SUPPRESSION_CPU_KERNEL_H_
17 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_COMBINED_NON_MAX_SUPPRESSION_CPU_KERNEL_H_
18 #include <algorithm>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <utility>
25 #include <vector>
26 #include <queue>
27 #include "plugin/device/cpu/kernel/cpu_kernel.h"
28 #include "plugin/factory/ms_factory.h"
29 
30 namespace non_max_suppression_local {
31 struct score_index {
32   int box_index{0};
33   float score{0};
34   int suppress_begin_index{0};
score_indexscore_index35   score_index() {}
score_indexscore_index36   score_index(int bi, float s, int sbi) : box_index(bi), score(s), suppress_begin_index(sbi) {}
37   bool operator<(const score_index &b) const {
38     return (score < b.score) || (mindspore::common::IsFloatEqual(score, b.score) && (box_index > b.box_index));
39   }
40 };
41 struct result_para {
42   int box_index;
43   float score;
44   int class_idx;
45   float box_coord[4];
46 };
47 
result_cmp(const result_para & a,const result_para & b)48 bool result_cmp(const result_para &a, const result_para &b) { return a.score > b.score; }
49 }  // namespace non_max_suppression_local
50 
51 namespace mindspore {
52 namespace kernel {
53 class CombinedNonMaxSuppressionCpuKernelMod : public NativeCpuKernelMod {
54  public:
55   CombinedNonMaxSuppressionCpuKernelMod() = default;
56   ~CombinedNonMaxSuppressionCpuKernelMod() override = default;
57 
58   bool Init(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &outputs) override;
59 
60   int Resize(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &outputs) override;
61 
62   bool Launch(const std::vector<KernelTensor *> &inputs, const std::vector<KernelTensor *> &workspace,
63               const std::vector<KernelTensor *> &outputs) override;
64 
65  protected:
66   std::vector<KernelAttr> GetOpSupport() override;
67 
68  private:
69   size_t nms_perbath(float *, float *, float *, float *, float *, int *);
70   void regular_input2buffer(std::vector<std::vector<float>> *const, const float *, const int) const;
71   float IOU(std::vector<std::vector<float>> *const, const int, const int) const;
72   void non_max_suppression(std::vector<std::vector<float>> *const, std::vector<float> *const, std::vector<int> &) const;
73   void nms_perclass(float *, float *, std::vector<non_max_suppression_local::result_para> &, int &) const;
74   void CheckInput();
75   void CheckOutput();
76 
77   int num_bath_ = 0;
78   int num_boxes_ = 0;
79   int q_ = 0;
80   int num_class_ = 0;
81   int num_detection_ = 0;
82   int max_total_size_ = 0;
83   // The length of each type of selection defined by the user
84   int max_output_size_per_class_ = 0;
85   // Calculation num_detection length
86   int size_per_class_ = 0;
87   // When lower than a score_threshold, delete the relevant box
88   float score_threshold_ = 0.0;
89   // When it is higher than the threshold value, according to the soft_nms_sigma determines deletion or decay
90   float iou_threshold_ = 0.0;
91   float soft_nms_sigma_ = 0.0;
92   bool pad_per_class_ = 0;
93   bool clip_boxes_ = 1;
94 
95   std::vector<int64_t> input0_shape_;
96   std::vector<int64_t> input1_shape_;
97   std::vector<int64_t> input2_shape_;
98   std::vector<int64_t> input3_shape_;
99   std::vector<int64_t> input4_shape_;
100   std::vector<int64_t> input5_shape_;
101   std::vector<int64_t> output0_shape_;
102   std::vector<int64_t> output1_shape_;
103   std::vector<int64_t> output2_shape_;
104   std::vector<int64_t> output3_shape_;
105 };
106 }  // namespace kernel
107 }  // namespace mindspore
108 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_COMBINED_NON_MAX_SUPPRESSION_CPU_KERNEL_H_
109