1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NEURAL_NETWORK_RUNTIME_DETECTION_POST_PROCESS_BUILDER_H
17 #define NEURAL_NETWORK_RUNTIME_DETECTION_POST_PROCESS_BUILDER_H
18 
19 #include "ops_builder.h"
20 
21 namespace OHOS {
22 namespace NeuralNetworkRuntime {
23 namespace Ops {
24 class DetectionPostProcessBuilder : public OpsBuilder {
25 public:
26     typedef DetectionPostProcessBuilder DPPBuilder;
27     typedef OH_NN_ReturnCode (DPPBuilder::*FuncPtr)(const std::shared_ptr<NNTensor>&);
28 
29     DetectionPostProcessBuilder();
30     ~DetectionPostProcessBuilder() override;
31     OH_NN_ReturnCode Build(const std::vector<uint32_t>& paramsIndex,
32                            const std::vector<uint32_t>& inputsIndex,
33                            const std::vector<uint32_t>& outputsIndex,
34                            const std::vector<std::shared_ptr<NNTensor>>& allTensors) override;
35 
36     LiteGraphPrimitvePtr GetPrimitive() override;
37 
38 private:
39     OH_NN_ReturnCode SetInputSize(const std::shared_ptr<NNTensor>& tensor);
40     OH_NN_ReturnCode SetScale(const std::shared_ptr<NNTensor>& tensor);
41     OH_NN_ReturnCode SetNmsIoUThreshold(const std::shared_ptr<NNTensor>& tensor);
42     OH_NN_ReturnCode SetNmsScoreThreshold(const std::shared_ptr<NNTensor>& tensor);
43     OH_NN_ReturnCode SetMaxDetections(const std::shared_ptr<NNTensor>& tensor);
44     OH_NN_ReturnCode SetDetectionsPerClass(const std::shared_ptr<NNTensor>& tensor);
45     OH_NN_ReturnCode SetMaxClassesPerDetection(const std::shared_ptr<NNTensor>& tensor);
46     OH_NN_ReturnCode SetNumClasses(const std::shared_ptr<NNTensor>& tensor);
47     OH_NN_ReturnCode SetUseRegularNms(const std::shared_ptr<NNTensor>& tensor);
48     OH_NN_ReturnCode SetOutQuantized(const std::shared_ptr<NNTensor>& tensor);
49 
50 private:
51     int64_t m_inputSize {0};
52     std::vector<float> m_scale;
53     float m_nmsIoUThreshold {0.0f};
54     float m_nmsScoreThreshold {0.0f};
55     int64_t m_maxDetections {0};
56     int64_t m_detectionsPerClass {0};
57     int64_t m_maxClassesPerDetection {0};
58     int64_t m_numClasses {0};
59     bool m_useRegularNms {false};
60     bool m_outQuantized {false};
61     std::unordered_map<OH_NN_TensorType, FuncPtr> m_paramMap = {
62         {OH_NN_DETECTION_POST_PROCESS_INPUT_SIZE, &DPPBuilder::SetInputSize},
63         {OH_NN_DETECTION_POST_PROCESS_SCALE, &DPPBuilder::SetScale},
64         {OH_NN_DETECTION_POST_PROCESS_NMS_IOU_THRESHOLD, &DPPBuilder::SetNmsIoUThreshold},
65         {OH_NN_DETECTION_POST_PROCESS_NMS_SCORE_THRESHOLD, &DPPBuilder::SetNmsScoreThreshold},
66         {OH_NN_DETECTION_POST_PROCESS_MAX_DETECTIONS, &DPPBuilder::SetMaxDetections},
67         {OH_NN_DETECTION_POST_PROCESS_DETECTIONS_PER_CLASS, &DPPBuilder::SetDetectionsPerClass},
68         {OH_NN_DETECTION_POST_PROCESS_MAX_CLASSES_PER_DETECTION, &DPPBuilder::SetMaxClassesPerDetection},
69         {OH_NN_DETECTION_POST_PROCESS_NUM_CLASSES, &DPPBuilder::SetNumClasses},
70         {OH_NN_DETECTION_POST_PROCESS_USE_REGULAR_NMS, &DPPBuilder::SetUseRegularNms},
71         {OH_NN_DETECTION_POST_PROCESS_OUT_QUANTIZED, &DPPBuilder::SetOutQuantized}
72     };
73 };
74 } // namespace Ops
75 } // namespace NeuralNetworkRuntime
76 } // namespace OHOS
77 
78 #endif // NEURAL_NETWORK_RUNTIME_DETECTION_POST_PROCESS_BUILDER_H
79