13
13
# limitations under the License.
14
14
15
15
from 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
+
19
18
from .model_config import (
20
19
is_lp_layout_model_config_any_format ,
21
20
)
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
22
53
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
28
59
29
60
30
61
def AutoLayoutModel (
31
62
config_path : str ,
32
63
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 ,
36
67
) -> "BaseLayoutModel" :
37
68
"""[summary]
38
69
@@ -50,7 +81,7 @@ def AutoLayoutModel(
50
81
Defaults to `None`.
51
82
device(:obj:`str`, optional):
52
83
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.
54
85
extra_config (:obj:`dict`, optional):
55
86
Extra configuration passed used for initializing the layout model.
56
87
@@ -59,6 +90,8 @@ def AutoLayoutModel(
59
90
"""
60
91
if not is_lp_layout_model_config_any_format (config_path ):
61
92
raise ValueError (f"Invalid model config_path { config_path } " )
93
+
94
+ # Try to search for the model keywords
62
95
for backend_name in ALL_AVAILABLE_BACKENDS :
63
96
if backend_name in config_path :
64
97
return ALL_AVAILABLE_BACKENDS [backend_name ](
@@ -68,3 +101,16 @@ def AutoLayoutModel(
68
101
extra_config = extra_config ,
69
102
device = device ,
70
103
)
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