pipeline_arguments.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  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. from ast import literal_eval
  15. from pydantic import TypeAdapter, ValidationError
  16. from functools import wraps
  17. from typing import Dict, List, Tuple, Union, Literal, Optional
  18. def custom_type(cli_expected_type):
  19. """Create validator for CLI input conversion and type checking"""
  20. def validator(cli_input: str) -> cli_expected_type:
  21. try:
  22. parsed = literal_eval(cli_input)
  23. except (ValueError, SyntaxError, TypeError, MemoryError, RecursionError) as exc:
  24. err = f"""Malformed input:
  25. - Input: {cli_input!r}
  26. - Error: {exc}"""
  27. raise ValueError(err) from exc
  28. try:
  29. return TypeAdapter(cli_expected_type).validate_python(parsed)
  30. except ValidationError as exc:
  31. err = f"""Invalid input type:
  32. - Expected: {cli_expected_type}
  33. - Received: {cli_input!r}
  34. """
  35. raise ValueError(err) from exc
  36. return validator
  37. PIPELINE_ARGUMENTS = {
  38. "OCR": [
  39. {
  40. "name": "--use_doc_orientation_classify",
  41. "type": bool,
  42. "help": "Determines whether to use document orientation classification",
  43. },
  44. {
  45. "name": "--use_doc_unwarping",
  46. "type": bool,
  47. "help": "Determines whether to use document unwarping",
  48. },
  49. {
  50. "name": "--use_textline_orientation",
  51. "type": bool,
  52. "help": "Determines whether to consider text line orientation",
  53. },
  54. {
  55. "name": "--text_det_limit_side_len",
  56. "type": int,
  57. "help": "Sets the side length limit for text detection.",
  58. },
  59. {
  60. "name": "--text_det_limit_type",
  61. "type": str,
  62. "help": "Sets the limit type for text detection.",
  63. },
  64. {
  65. "name": "--text_det_thresh",
  66. "type": float,
  67. "help": "Sets the threshold for text detection.",
  68. },
  69. {
  70. "name": "--text_det_box_thresh",
  71. "type": float,
  72. "help": "Sets the box threshold for text detection.",
  73. },
  74. {
  75. "name": "--text_det_unclip_ratio",
  76. "type": float,
  77. "help": "Sets the unclip ratio for text detection.",
  78. },
  79. {
  80. "name": "--text_rec_score_thresh",
  81. "type": float,
  82. "help": "Sets the score threshold for text recognition.",
  83. },
  84. ],
  85. "object_detection": [
  86. {
  87. "name": "--threshold",
  88. "type": float,
  89. "help": "Sets the threshold for object detection.",
  90. },
  91. ],
  92. "image_classification": [
  93. {
  94. "name": "--topk",
  95. "type": int,
  96. "help": "Sets the Top-K value for image classification.",
  97. },
  98. ],
  99. "image_multilabel_classification": [
  100. {
  101. "name": "--threshold",
  102. "type": float,
  103. "help": "Sets the threshold for image multilabel classification.",
  104. },
  105. ],
  106. "pedestrian_attribute_recognition": [
  107. {
  108. "name": "--det_threshold",
  109. "type": float,
  110. "help": "Sets the threshold for human detection.",
  111. },
  112. {
  113. "name": "--cls_threshold",
  114. "type": float,
  115. "help": "Sets the threshold for pedestrian attribute recognition.",
  116. },
  117. ],
  118. "vehicle_attribute_recognition": [
  119. {
  120. "name": "--det_threshold",
  121. "type": float,
  122. "help": "Sets the threshold for vehicle detection.",
  123. },
  124. {
  125. "name": "--cls_threshold",
  126. "type": float,
  127. "help": "Sets the threshold for vehicle attribute recognition.",
  128. },
  129. ],
  130. "table_recognition": None,
  131. "layout_parsing": None,
  132. "seal_recognition": [
  133. {
  134. "name": "--use_doc_orientation_classify",
  135. "type": bool,
  136. "help": "Determines whether to use document preprocessing",
  137. },
  138. {
  139. "name": "--use_doc_unwarping",
  140. "type": bool,
  141. "help": "Determines whether to use document unwarping",
  142. },
  143. {
  144. "name": "--use_layout_detection",
  145. "type": bool,
  146. "help": "Determines whether to use document layout detection",
  147. },
  148. {
  149. "name": "--layout_threshold",
  150. "type": float,
  151. "help": "Determines confidence threshold for layout detection",
  152. },
  153. {
  154. "name": "--layout_nms",
  155. "type": bool,
  156. "help": "Determines whether to use non maximum suppression",
  157. },
  158. {
  159. "name": "--layout_unclip_ratio",
  160. "type": float,
  161. "help": "Determines unclip ratio for layout detection boxes",
  162. },
  163. {
  164. "name": "--layout_merge_bboxes_mode",
  165. "type": str,
  166. "help": "Determines merge mode for layout detection bboxes, 'union', 'large' or 'small'",
  167. },
  168. {
  169. "name": "--seal_det_limit_side_len",
  170. "type": int,
  171. "help": "Sets the side length limit for text detection.",
  172. },
  173. {
  174. "name": "--seal_det_limit_type",
  175. "type": str,
  176. "help": "Sets the limit type for text detection, 'min', 'max'.",
  177. },
  178. {
  179. "name": "--seal_det_thresh",
  180. "type": float,
  181. "help": "Sets the threshold for text detection.",
  182. },
  183. {
  184. "name": "--seal_det_box_thresh",
  185. "type": float,
  186. "help": "Sets the box threshold for text detection.",
  187. },
  188. {
  189. "name": "--seal_det_unclip_ratio",
  190. "type": float,
  191. "help": "Sets the unclip ratio for text detection.",
  192. },
  193. {
  194. "name": "--seal_rec_score_thresh",
  195. "type": float,
  196. "help": "Sets the score threshold for text recognition.",
  197. },
  198. ],
  199. "ts_forecast": None,
  200. "ts_anomaly_detection": None,
  201. "ts_classification": None,
  202. "formula_recognition": [
  203. {
  204. "name": "--use_layout_detection",
  205. "type": bool,
  206. "help": "Determines whether to use layout detection",
  207. },
  208. {
  209. "name": "--use_doc_orientation_classify",
  210. "type": bool,
  211. "help": "Determines whether to use document orientation classification",
  212. },
  213. {
  214. "name": "--use_doc_unwarping",
  215. "type": bool,
  216. "help": "Determines whether to use document unwarping",
  217. },
  218. {
  219. "name": "--layout_threshold",
  220. "type": float,
  221. "help": "Sets the layout threshold for layout detection.",
  222. },
  223. {
  224. "name": "--layout_nms",
  225. "type": bool,
  226. "help": "Determines whether to use layout nms",
  227. },
  228. {
  229. "name": "--layout_unclip_ratio",
  230. "type": float,
  231. "help": "Sets the layout unclip ratio for layout detection.",
  232. },
  233. {
  234. "name": "--layout_merge_bboxes_mode",
  235. "type": str,
  236. "help": "Sets the layout merge bboxes mode for layout detection.",
  237. },
  238. ],
  239. "instance_segmentation": [
  240. {
  241. "name": "--threshold",
  242. "type": custom_type(Optional[float]),
  243. "help": "Sets the threshold for instance segmentation.",
  244. },
  245. ],
  246. "semantic_segmentation": [
  247. {
  248. "name": "--target_size",
  249. "type": custom_type(Optional[Union[int, Tuple[int, int], Literal[-1]]]),
  250. "help": "Sets the inference image resolution for semantic segmentation.",
  251. },
  252. ],
  253. "small_object_detection": [
  254. {
  255. "name": "--threshold",
  256. "type": custom_type(Optional[Union[float, dict[int, float]]]),
  257. "help": "Sets the threshold for small object detection.",
  258. },
  259. ],
  260. "anomaly_detection": None,
  261. "video_classification": [
  262. {
  263. "name": "--topk",
  264. "type": int,
  265. "help": "Sets the Top-K value for video classification.",
  266. },
  267. ],
  268. "video_detection": [
  269. {
  270. "name": "--nms_thresh",
  271. "type": float,
  272. "help": "Sets the NMS threshold for video detection.",
  273. },
  274. {
  275. "name": "--score_thresh",
  276. "type": float,
  277. "help": "Sets the confidence threshold for video detection.",
  278. },
  279. ],
  280. "doc_preprocessor": [
  281. {
  282. "name": "--use_doc_orientation_classify",
  283. "type": bool,
  284. "help": "Determines whether to use document orientation classification.",
  285. },
  286. {
  287. "name": "--use_doc_unwarping",
  288. "type": bool,
  289. "help": "Determines whether to use document unwarping.",
  290. },
  291. ],
  292. "rotated_object_detection": [
  293. {
  294. "name": "--threshold",
  295. "type": custom_type(Optional[Union[float, dict[int, float]]]),
  296. "help": "Sets the threshold for rotated object detection.",
  297. },
  298. ],
  299. "open_vocabulary_detection": [
  300. {
  301. "name": "--thresholds",
  302. "type": custom_type(dict[str, float]),
  303. "help": "Sets the thresholds for open vocabulary detection.",
  304. },
  305. {
  306. "name": "--prompt",
  307. "type": str,
  308. "help": "Sets the prompt for open vocabulary detection.",
  309. },
  310. ],
  311. "open_vocabulary_segmentation": [
  312. {
  313. "name": "--prompt_type",
  314. "type": str,
  315. "help": "Sets the prompt type for open vocabulary segmentation.",
  316. },
  317. {
  318. "name": "--prompt",
  319. "type": custom_type(list[list[float]]),
  320. "help": "Sets the prompt for open vocabulary segmentation.",
  321. },
  322. ],
  323. }