1313# limitations under the License.
1414
1515from typing import Optional , Dict , Union , List
16- from .detectron2 .layoutmodel import Detectron2LayoutModel
17- from .paddledetection .layoutmodel import PaddleDetectionLayoutModel
18- from .effdet .layoutmodel import EfficientDetLayoutModel
16+ from collections import defaultdict
17+
1918from .model_config import (
2019 is_lp_layout_model_config_any_format ,
2120)
21+ from ..file_utils import (
22+ is_effdet_available ,
23+ is_detectron2_available ,
24+ is_paddle_available ,
25+ )
26+
27+ ALL_AVAILABLE_BACKENDS = dict ()
28+ ALL_AVAILABLE_DATASETS = defaultdict (list )
29+
30+ if is_effdet_available ():
31+ from .effdet .layoutmodel import EfficientDetLayoutModel
32+ from .effdet .catalog import MODEL_CATALOG as _effdet_model_catalog
33+
34+ # fmt: off
35+ ALL_AVAILABLE_BACKENDS [EfficientDetLayoutModel .DETECTOR_NAME ] = EfficientDetLayoutModel
36+ for dataset_name in _effdet_model_catalog :
37+ ALL_AVAILABLE_DATASETS [dataset_name ].append (EfficientDetLayoutModel .DETECTOR_NAME )
38+ # fmt: on
39+
40+ if is_detectron2_available ():
41+ from .detectron2 .layoutmodel import Detectron2LayoutModel
42+ from .detectron2 .catalog import MODEL_CATALOG as _detectron2_model_catalog
43+
44+ # fmt: off
45+ ALL_AVAILABLE_BACKENDS [Detectron2LayoutModel .DETECTOR_NAME ] = Detectron2LayoutModel
46+ for dataset_name in _detectron2_model_catalog :
47+ ALL_AVAILABLE_DATASETS [dataset_name ].append (Detectron2LayoutModel .DETECTOR_NAME )
48+ # fmt: on
49+
50+ if is_paddle_available ():
51+ from .paddledetection .layoutmodel import PaddleDetectionLayoutModel
52+ from .paddledetection .catalog import MODEL_CATALOG as _paddle_model_catalog
2253
23- ALL_AVAILABLE_BACKENDS = {
24- Detectron2LayoutModel .DETECTOR_NAME : Detectron2LayoutModel ,
25- PaddleDetectionLayoutModel . DETECTOR_NAME : PaddleDetectionLayoutModel ,
26- EfficientDetLayoutModel . DETECTOR_NAME : EfficientDetLayoutModel ,
27- }
54+ # fmt: off
55+ ALL_AVAILABLE_BACKENDS [ PaddleDetectionLayoutModel .DETECTOR_NAME ] = PaddleDetectionLayoutModel
56+ for dataset_name in _paddle_model_catalog :
57+ ALL_AVAILABLE_DATASETS [ dataset_name ]. append ( PaddleDetectionLayoutModel . DETECTOR_NAME )
58+ # fmt: on
2859
2960
3061def AutoLayoutModel (
3162 config_path : str ,
3263 model_path : Optional [str ] = None ,
33- label_map : Optional [Dict ]= None ,
34- device : Optional [str ]= None ,
35- extra_config : Optional [Union [Dict , List ]]= None ,
64+ label_map : Optional [Dict ] = None ,
65+ device : Optional [str ] = None ,
66+ extra_config : Optional [Union [Dict , List ]] = None ,
3667) -> "BaseLayoutModel" :
3768 """[summary]
3869
@@ -50,7 +81,7 @@ def AutoLayoutModel(
5081 Defaults to `None`.
5182 device(:obj:`str`, optional):
5283 Whether to use cuda or cpu devices. If not set, LayoutParser will
53- automatically determine the device to initialize the models on.
84+ automatically determine the device to initialize the models on.
5485 extra_config (:obj:`dict`, optional):
5586 Extra configuration passed used for initializing the layout model.
5687
@@ -59,6 +90,8 @@ def AutoLayoutModel(
5990 """
6091 if not is_lp_layout_model_config_any_format (config_path ):
6192 raise ValueError (f"Invalid model config_path { config_path } " )
93+
94+ # Try to search for the model keywords
6295 for backend_name in ALL_AVAILABLE_BACKENDS :
6396 if backend_name in config_path :
6497 return ALL_AVAILABLE_BACKENDS [backend_name ](
@@ -68,3 +101,16 @@ def AutoLayoutModel(
68101 extra_config = extra_config ,
69102 device = device ,
70103 )
104+
105+ # Try to search for the dataset keywords
106+ for dataset_name in ALL_AVAILABLE_DATASETS :
107+ if dataset_name in config_path :
108+ return ALL_AVAILABLE_BACKENDS [ALL_AVAILABLE_DATASETS [dataset_name ][0 ]](
109+ config_path ,
110+ model_path = model_path ,
111+ label_map = label_map ,
112+ extra_config = extra_config ,
113+ device = device ,
114+ )
115+
116+ raise ValueError (f"No available model found for { config_path } " )
0 commit comments