1 /**
2 * Copyright 2020 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
17 #include "ops/detection_post_process.h"
18 #include "mindapi/base/shared_ptr.h"
19 #include "mindapi/ir/value.h"
20 #include "mindapi/src/helper.h"
21 #include "ops/op_name.h"
22 #include "ops/primitive_c.h"
23 #include "utils/log_adapter.h"
24
25 namespace mindspore {
26 namespace ops {
27 MIND_API_OPERATOR_IMPL(DetectionPostProcess, BaseOperator);
Init(const int64_t inputSize,const std::vector<float> & scale,const float NmsIouThreshold,const float NmsScoreThreshold,const int64_t MaxDetections,const int64_t DetectionsPerClass,const int64_t MaxClassesPerDetection,const int64_t NumClasses,const bool UseRegularNms,const bool OutQuantized,const Format & format)28 void DetectionPostProcess::Init(const int64_t inputSize, const std::vector<float> &scale, const float NmsIouThreshold,
29 const float NmsScoreThreshold, const int64_t MaxDetections,
30 const int64_t DetectionsPerClass, const int64_t MaxClassesPerDetection,
31 const int64_t NumClasses, const bool UseRegularNms, const bool OutQuantized,
32 const Format &format) {
33 set_input_size(inputSize);
34 set_scale(scale);
35 set_nms_iou_threshold(NmsIouThreshold);
36 set_nms_score_threshold(NmsScoreThreshold);
37 set_max_detections(MaxDetections);
38 set_detections_per_class(DetectionsPerClass);
39 set_max_classes_per_detection(MaxClassesPerDetection);
40 set_num_classes(NumClasses);
41 set_use_regular_nms(UseRegularNms);
42 set_out_quantized(OutQuantized);
43 set_format(format);
44 }
45
set_input_size(const int64_t inputSize)46 void DetectionPostProcess::set_input_size(const int64_t inputSize) {
47 (void)this->AddAttr(kInputSize, api::MakeValue(inputSize));
48 }
49
get_input_size() const50 int64_t DetectionPostProcess::get_input_size() const {
51 auto value_ptr = this->GetAttr(kInputSize);
52 return GetValue<int64_t>(value_ptr);
53 }
54
set_scale(const std::vector<float> & scale)55 void DetectionPostProcess::set_scale(const std::vector<float> &scale) {
56 (void)this->AddAttr(kScale, api::MakeValue(scale));
57 }
get_scale() const58 std::vector<float> DetectionPostProcess::get_scale() const {
59 auto value_ptr = this->GetAttr(kScale);
60 return GetValue<std::vector<float>>(value_ptr);
61 }
62
set_nms_iou_threshold(const float NmsIouThreshold)63 void DetectionPostProcess::set_nms_iou_threshold(const float NmsIouThreshold) {
64 (void)this->AddAttr(kNmsIouThreshold, api::MakeValue(NmsIouThreshold));
65 }
get_nms_iou_threshold() const66 float DetectionPostProcess::get_nms_iou_threshold() const {
67 auto value_ptr = this->GetAttr(kNmsIouThreshold);
68 return GetValue<float>(value_ptr);
69 }
70
set_nms_score_threshold(const float NmsScoreThreshold)71 void DetectionPostProcess::set_nms_score_threshold(const float NmsScoreThreshold) {
72 (void)this->AddAttr(kNmsScoreThreshold, api::MakeValue(NmsScoreThreshold));
73 }
get_nms_score_threshold() const74 float DetectionPostProcess::get_nms_score_threshold() const {
75 auto value_ptr = this->GetAttr(kNmsScoreThreshold);
76 return GetValue<float>(value_ptr);
77 }
78
set_max_detections(const int64_t MaxDetections)79 void DetectionPostProcess::set_max_detections(const int64_t MaxDetections) {
80 (void)this->AddAttr(kMaxDetections, api::MakeValue(MaxDetections));
81 }
get_max_detections() const82 int64_t DetectionPostProcess::get_max_detections() const { return GetValue<int64_t>(GetAttr(kMaxDetections)); }
83
set_detections_per_class(const int64_t DetectionsPerClass)84 void DetectionPostProcess::set_detections_per_class(const int64_t DetectionsPerClass) {
85 (void)this->AddAttr(kDetectionsPerClass, api::MakeValue(DetectionsPerClass));
86 }
get_detections_per_class() const87 int64_t DetectionPostProcess::get_detections_per_class() const {
88 auto value_ptr = this->GetAttr(kDetectionsPerClass);
89 return GetValue<int64_t>(value_ptr);
90 }
91
set_max_classes_per_detection(const int64_t MaxClassesPerDetection)92 void DetectionPostProcess::set_max_classes_per_detection(const int64_t MaxClassesPerDetection) {
93 (void)this->AddAttr(kMaxClassesPerDetection, api::MakeValue(MaxClassesPerDetection));
94 }
get_max_classes_per_detection() const95 int64_t DetectionPostProcess::get_max_classes_per_detection() const {
96 return GetValue<int64_t>(GetAttr(kMaxClassesPerDetection));
97 }
98
set_num_classes(const int64_t NumClasses)99 void DetectionPostProcess::set_num_classes(const int64_t NumClasses) {
100 (void)this->AddAttr(kNumClasses, api::MakeValue(NumClasses));
101 }
get_num_classes() const102 int64_t DetectionPostProcess::get_num_classes() const { return GetValue<int64_t>(GetAttr(kNumClasses)); }
set_use_regular_nms(const bool UseRegularNms)103 void DetectionPostProcess::set_use_regular_nms(const bool UseRegularNms) {
104 (void)this->AddAttr(kUseRegularNms, api::MakeValue(UseRegularNms));
105 }
get_use_regular_nms() const106 bool DetectionPostProcess::get_use_regular_nms() const {
107 auto value_ptr = this->GetAttr(kUseRegularNms);
108 return GetValue<bool>(value_ptr);
109 }
110
set_out_quantized(const bool OutQuantized)111 void DetectionPostProcess::set_out_quantized(const bool OutQuantized) {
112 (void)this->AddAttr(kOutQuantized, api::MakeValue(OutQuantized));
113 }
get_out_quantized() const114 bool DetectionPostProcess::get_out_quantized() const {
115 auto value_ptr = this->GetAttr(kOutQuantized);
116 return GetValue<bool>(value_ptr);
117 }
set_format(const Format & format)118 void DetectionPostProcess::set_format(const Format &format) {
119 int64_t f = format;
120 (void)this->AddAttr(kFormat, api::MakeValue(f));
121 }
get_format() const122 Format DetectionPostProcess::get_format() const { return Format(GetValue<int64_t>(GetAttr(kFormat))); }
123
124 REGISTER_PRIMITIVE_C(kNameDetectionPostProcess, DetectionPostProcess);
125 } // namespace ops
126 } // namespace mindspore
127