1 /*
2  * Copyright (c) 2021 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 /**
17  * @addtogroup feature_processor
18  * @{
19  *
20  * @brief Defines the basic functions for FeatureProcessor, including the supported data types
21  * and other related configuration parameters.
22  *
23  * @since 2.2
24  * @version 1.0
25  */
26 
27 /**
28  * @file norm_processor.h
29  *
30  * @brief Defines NormProcessor that normalizes and quantizes features based on the inference model.
31  *
32  * @since 2.2
33  * @version 1.0
34  */
35 
36 #ifndef PREPROCESS_NORM_PROCESSOR_H
37 #define PREPROCESS_NORM_PROCESSOR_H
38 
39 #include <cstdint>
40 #include <memory>
41 #include <string>
42 
43 #include "feature_processor.h"
44 #include "type_converter.h"
45 
46 namespace OHOS {
47 namespace AI {
48 namespace Feature {
49 /**
50  * @brief Specifies the structure for the NormProcessor configuration.
51  *
52  * @since 2.2
53  * @version 1.0
54  */
55 struct NormProcessorConfig : FeatureProcessorConfig {
56     /** Local address for the mean file of NormProcessor */
57     std::string meanFilePath;
58     /** Local address for the standard deviation file of NormProcessor */
59     std::string stdFilePath;
60     /** Number of channels for normalized parameters. The value must be greater than <b>0</b>
61      * and can be exactly divided by <b>inputSize</b>. */
62     size_t numChannels;
63     /** Sample size for the input data.
64      * The value must be greater than <b>0</b> but less than or equal to {@link MAX_SAMPLE_SIZE}. */
65     size_t inputSize;
66     /** Scaling coefficient. */
67     float scale = 1.0f;
68 };
69 
70 /**
71  * @brief Defines the functions for NormProcessor.
72  *
73  * @since 2.2
74  * @version 1.0
75  */
76 class NormProcessor : public FeatureProcessor {
77 public:
78     /**
79      * @brief Defines the constructor for NormProcessor.
80      *
81      * @since 2.2
82      * @version 1.0
83      */
84     NormProcessor();
85 
86     /**
87      * @brief Defines the destructor for NormProcessor.
88      *
89      * @since 2.2
90      * @version 1.0
91      */
92     virtual ~NormProcessor();
93 
94     /**
95      * @brief Initializes NormProcessor.
96      *
97      * @param config Indicates the pointer to the basic configuration of FeatureProcessor.
98      * The caller needs to pass in a pointer address defined by {@link NormProcessorConfig} and
99      * release the pointer after using it.
100      * @return Returns {@link RETCODE_SUCCESS} if the operation is successfull;
101      * returns {@link RETCODE_FAILURE} otherwise
102      *
103      * @since 2.2
104      * @version 1.0
105      */
106     int32_t Init(const FeatureProcessorConfig *config) override;
107 
108     /**
109      * @brief Performs feature processing.
110      *
111      * @param input Indicates the input data for FeatureProcessor.
112      * The caller can input FeatureData of any data type defined by {@link DataType} except UNKNOWN.
113      * However the address and data length must meet the configuration requirements.
114      * @param output Indicates the output data for FeatureProcessor.
115      * The caller must pass in FeatureData of the FLOAT type defined by {@link DataType}.
116      * If and only if its address is empty and the data length is <b>0</b>,
117      * data will be filled by the FeatureProcessor.
118      * @return Returns {@link RETCODE_SUCCESS} if the operation is successful;
119      * returns {@link RETCODE_FAILURE} otherwise.
120      *
121      * @since 2.2
122      * @version 1.0
123      */
124     int32_t Process(const FeatureData &input, FeatureData &output) override;
125 
126     /**
127      * @brief Releases resources.
128      *
129      * @since 2.2
130      * @version 1.0
131      */
132     void Release() override;
133 
134 private:
135     bool isInitialized_;
136     float *workBuffer_;
137     float *mean_;
138     float *std_;
139     NormProcessorConfig config_;
140     std::unique_ptr<TypeConverter> converter_;
141 };
142 } // namespace Feature
143 } // namespace AI
144 } // namespace OHOS
145 #endif // PREPROCESS_NORM_PROCESSOR_H
146 /** @} */