input_process_config.py Source File

input_process_config.py Source File#

Mobilint SDK qb Compiler: input_process_config.py Source File
Mobilint SDK qb Compiler v1.0
MCS002-KR
input_process_config.py
1from typing import Dict, List, Any, Optional
2from typeguard import typechecked
3from . import ConfigABC
4
5
11
12
13class Uint8InputConfig(ConfigABC):
14 """
15 @brief Configuration for uint8 input handling.
16
17 @details Defines whether inputs should be treated as uint8 and which specific inputs to apply this to.
18 """
19
20 optional = True
21 DEFAULTS = {
22 "apply": False,
23 "inputs": [],
24 }
25
26 py_to_cpp_name = {
27 "apply": "apply",
28 }
29
30 min_max_check = {}
31
32 @typechecked
34 self,
35 apply: bool,
36 inputs: List[str],
37 optional: bool = optional,
38 ):
39 """
40 @brief Initialize the Uint8InputConfig.
41 @param apply bool. If true, treat specified inputs as uint8.
42 @param inputs List[str]. List of input names to treat as uint8. If empty and apply is true, applies to all inputs.
43 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
44 """
45 super().__init__(optional)
46 self.apply = apply
47 self.inputs = inputs
48 self.check_valid(except_list=["optional", "inputs"])
49
50 @classmethod
51 def default_config(cls):
52 return cls(**cls.DEFAULTS, optional=cls.optional)
53
54 @classmethod
55 def from_dict(cls, config_dict: dict):
56 """Create Uint8InputConfig from dictionary (JSON structure)"""
57 params = cls.DEFAULTS.copy()
58
59 if "apply" in config_dict:
60 params["apply"] = config_dict["apply"]
61 if "inputs" in config_dict:
62 params["inputs"] = config_dict["inputs"]
63
64 return cls(**params)
65
66 def to_dict(self):
67 return {
68 "apply": self.apply,
69 "inputs": self.inputs,
70 }
71
72
73def get_uint8_input_config(**kwargs) -> Uint8InputConfig:
74 """
75 @brief Create Uint8InputConfig with partial parameters merged with defaults.
76 """
77 params = Uint8InputConfig.DEFAULTS.copy()
78 params.update(kwargs)
79 return Uint8InputConfig(**params, optional=Uint8InputConfig.optional)
80
81
82class PreprocessingConfig(ConfigABC):
83 """
84 @brief Configuration for input preprocessing pipeline.
85
86 @details Defines preprocessing operations to be applied to model inputs,
87 including operations like resize, normalize, color conversion, etc.
88 """
89
90 optional = True
91 DEFAULTS = {
92 "apply": False,
93 "auto_convert_format": False,
94 "pipeline": [],
95 "input_configs": {},
96 }
97
98 py_to_cpp_name = {
99 "apply": "apply",
100 "auto_convert_format": "autoConvertFormat",
101 }
102
103 min_max_check = {}
104
105 @typechecked
107 self,
108 apply: bool,
109 auto_convert_format: bool,
110 pipeline: List[Dict[str, Any]],
111 input_configs: Dict[str, Any],
112 optional: bool = optional,
113 ):
114 """
115 @brief Initialize the PreprocessingConfig.
116 @param apply bool. If true, apply preprocessing pipeline.
117 @param auto_convert_format bool. If true, automatically convert input format.
118 @param pipeline List[Dict[str, Any]]. List of preprocessing operations to apply globally.
119 Each operation is a dict with "op" key and operation-specific parameters.
120 Supported operations: colorConvert, resize, centerCrop, pad, letterbox,
121 normalize, clamp, divideByScale, multiplyByScale, reshape, transpose, permute.
122 @param input_configs Dict[str, Any]. Per-input preprocessing configurations.
123 Keys are input names, values are dicts containing input-specific pipelines.
124 @param optional bool. Indicates whether this config is optional. Use the default value defined as a class variable.
125 """
126 super().__init__(optional)
127 self.apply = apply
128 self.auto_convert_format = auto_convert_format
129 self.pipeline = pipeline
130 self.input_configs = input_configs
131 self.check_valid(except_list=["optional", "pipeline", "input_configs"])
132
133 @classmethod
134 def default_config(cls):
135 return cls(**cls.DEFAULTS, optional=cls.optional)
136
137 @classmethod
138 def from_dict(cls, config_dict: dict):
139 """Create PreprocessingConfig from dictionary (JSON structure)"""
140 params = cls.DEFAULTS.copy()
141
142 if "apply" in config_dict:
143 params["apply"] = config_dict["apply"]
144 if "autoConvertFormat" in config_dict:
145 params["auto_convert_format"] = config_dict["autoConvertFormat"]
146 if "pipeline" in config_dict:
147 params["pipeline"] = config_dict["pipeline"]
148 if "inputConfigs" in config_dict:
149 params["input_configs"] = config_dict["inputConfigs"]
150
151 return cls(**params)
152
153 def to_dict(self):
154 return {
155 "apply": self.apply,
156 "autoConvertFormat": self.auto_convert_format,
157 "pipeline": self.pipeline,
158 "inputConfigs": self.input_configs,
159 }
160
161
162def get_preprocessing_config(**kwargs) -> PreprocessingConfig:
163 """
164 @brief Create PreprocessingConfig with partial parameters merged with defaults.
165 """
166 params = PreprocessingConfig.DEFAULTS.copy()
167 params.update(kwargs)
168 return PreprocessingConfig(**params, optional=PreprocessingConfig.optional)
169
170
171class InputProcessConfig(ConfigABC):
172 """
173 @brief Unified configuration for input processing.
174
175 @details Combines uint8 input handling, image channel configuration, and
176 preprocessing pipeline settings into a single configuration object.
177 """
178
179 optional = True
180 DEFAULTS = {
181 "uint8_input": None, # Will use Uint8InputConfig.default_config()
182 "image_channels": 0,
183 "preprocessing": None, # Will use PreprocessingConfig.default_config()
184 }
185
186 py_to_cpp_name = {
187 "image_channels": "imageChannels",
188 }
189
190 min_max_check = {}
191
192 @typechecked
194 self,
195 uint8_input: Optional[Uint8InputConfig],
196 image_channels: int,
197 preprocessing: Optional[PreprocessingConfig],
198 optional: bool = optional,
199 ):
200 """
201 @brief Initialize the InputProcessConfig.
202 @param uint8_input Uint8InputConfig. Configuration for uint8 input handling.
203 If None, uses default Uint8InputConfig.
204 @param image_channels int. Number of image channels (0=auto, 1=grayscale, 3=RGB, 4=RGBA).
205 @param preprocessing PreprocessingConfig. Configuration for input preprocessing pipeline.
206 If None, uses default PreprocessingConfig.
207 @param optional bool. Indicates whether this config is optional.
208 """
209 super().__init__(optional)
210 self.uint8_input = (
211 uint8_input if uint8_input is not None else Uint8InputConfig.default_config()
212 )
213 self.image_channels = image_channels
214 self.preprocessing = (
215 preprocessing
216 if preprocessing is not None
217 else PreprocessingConfig.default_config()
218 )
219 self.check_valid(
220 except_list=["optional", "uint8_input", "preprocessing"]
221 )
222
223 @classmethod
224 def default_config(cls):
225 return cls(
226 uint8_input=Uint8InputConfig.default_config(),
227 image_channels=0,
228 preprocessing=PreprocessingConfig.default_config(),
229 optional=cls.optional,
230 )
231
232 @classmethod
233 def from_dict(cls, config_dict: dict):
234 """Create InputProcessConfig from dictionary (JSON structure)"""
235 uint8_input = Uint8InputConfig.from_dict(config_dict.get("uint8Input", {}))
236 image_channels = config_dict.get("imageChannels", 0)
237 preprocessing = PreprocessingConfig.from_dict(
238 config_dict.get("preprocessing", {})
239 )
240
241 return cls(
242 uint8_input=uint8_input,
243 image_channels=image_channels,
244 preprocessing=preprocessing,
245 )
246
247 def to_dict(self):
248 return {
249 "uint8Input": self.uint8_input.to_dict(),
250 "imageChannels": self.image_channels,
251 "preprocessing": self.preprocessing.to_dict(),
252 }
253
254
256 uint8_input_config: Optional[Uint8InputConfig] = None,
257 image_channels: int = 0,
258 preprocessing_config: Optional[PreprocessingConfig] = None,
259) -> InputProcessConfig:
260 """
261 @brief Create InputProcessConfig with partial parameters merged with defaults.
262
263 @param uint8_input_config Uint8InputConfig. Configuration for uint8 input handling.
264 @param image_channels int. Number of image channels (0=auto, 1=grayscale, 3=RGB, 4=RGBA).
265 @param preprocessing_config PreprocessingConfig. Configuration for input preprocessing pipeline.
266 @return InputProcessConfig. The configured input process configuration.
267 """
268 return InputProcessConfig(
269 uint8_input=uint8_input_config,
270 image_channels=image_channels,
271 preprocessing=preprocessing_config,
272 optional=InputProcessConfig.optional,
273 )
274
275
276# @}
Configuration for input preprocessing pipeline.
InputProcessConfig get_input_process_config(Optional[Uint8InputConfig] uint8_input_config=None, int image_channels=0, Optional[PreprocessingConfig] preprocessing_config=None)
Create InputProcessConfig with partial parameters merged with defaults.
from_dict(cls, dict config_dict)
Create Uint8InputConfig from dictionary (JSON structure)
__init__(self, bool apply, List[str] inputs, bool optional=optional)
Initialize the Uint8InputConfig.
from_dict(cls, dict config_dict)
Create InputProcessConfig from dictionary (JSON structure)
Uint8InputConfig get_uint8_input_config(**kwargs)
Create Uint8InputConfig with partial parameters merged with defaults.
__init__(self, Optional[Uint8InputConfig] uint8_input, int image_channels, Optional[PreprocessingConfig] preprocessing, bool optional=optional)
Initialize the InputProcessConfig.
__init__(self, bool apply, bool auto_convert_format, List[Dict[str, Any]] pipeline, Dict[str, Any] input_configs, bool optional=optional)
Initialize the PreprocessingConfig.
PreprocessingConfig get_preprocessing_config(**kwargs)
Create PreprocessingConfig with partial parameters merged with defaults.
from_dict(cls, dict config_dict)
Create PreprocessingConfig from dictionary (JSON structure)