Skip to content

Commit ca187f4

Browse files
committed
Improve the directory structure for models
1 parent c167fba commit ca187f4

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import catalog as _UNUSED
2+
# A trick learned from
3+
# https://github.com/facebookresearch/detectron2/blob/62cf3a2b6840734d2717abdf96e2dd57ed6612a6/detectron2/checkpoint/__init__.py#L6
4+
from .layoutmodel import Detectron2LayoutModel

src/layoutparser/models/catalog.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from fvcore.common.file_io import PathHandler, PathManager, HTTPURLHandler
2+
3+
MODEL_CATALOG = {
4+
'HJDataset': {
5+
'faster_rcnn_R_50_FPN_3x': 'https://www.dropbox.com/s/6icw6at8m28a2ho/model_final.pth?dl=1',
6+
'mask_rcnn_R_50_FPN_3x': 'https://www.dropbox.com/s/893paxpy5suvlx9/model_final.pth?dl=1',
7+
'retinanet_R_50_FPN_3x': 'https://www.dropbox.com/s/yxsloxu3djt456i/model_final.pth?dl=1'
8+
}
9+
}
10+
11+
CONFIG_CATALOG = {
12+
'HJDataset': {
13+
'faster_rcnn_R_50_FPN_3x': 'https://www.dropbox.com/s/j4yseny2u0hn22r/config.yml?dl=1',
14+
'mask_rcnn_R_50_FPN_3x': 'https://www.dropbox.com/s/4jmr3xanmxmjcf8/config.yml?dl=0',
15+
'retinanet_R_50_FPN_3x': 'https://www.dropbox.com/s/z8a8ywozuyc5c2x/config.yml?dl=0'
16+
}
17+
}
18+
19+
class DropboxHandler(HTTPURLHandler):
20+
"""
21+
Supports download and file check for dropbox links
22+
"""
23+
def _get_supported_prefixes(self):
24+
return ["https://www.dropbox.com"]
25+
26+
def _isfile(self, path):
27+
return path in self.cache_map
28+
29+
30+
class LayoutParserHandler(PathHandler):
31+
"""
32+
Resolve anything that's in LayoutParser model zoo.
33+
"""
34+
35+
PREFIX = "lp://"
36+
37+
def _get_supported_prefixes(self):
38+
return [self.PREFIX]
39+
40+
def _get_local_path(self, path):
41+
model_name = path[len(self.PREFIX):]
42+
dataset_name, *model_name, data_type = model_name.split('/')
43+
44+
if data_type == 'weight':
45+
model_url = MODEL_CATALOG[dataset_name]['/'.join(model_name)]
46+
elif data_type == 'config':
47+
model_url = CONFIG_CATALOG[dataset_name]['/'.join(model_name)]
48+
else:
49+
raise ValueError(f"Unknown data_type {data_type}")
50+
return PathManager.get_local_path(model_url)
51+
52+
def _open(self, path, mode="r", **kwargs):
53+
return PathManager.open(self._get_local_path(path), mode, **kwargs)
54+
55+
56+
PathManager.register_handler(DropboxHandler())
57+
PathManager.register_handler(LayoutParserHandler())
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import torch
44
from detectron2.config import get_cfg
55
from detectron2.engine import DefaultPredictor
6-
from .elements import *
6+
from ..elements import *
7+
from fvcore.common.file_io import PathManager
8+
79

810
__all__ = ['Detectron2LayoutModel']
911

@@ -16,13 +18,14 @@ def detect(self): pass
1618

1719
class Detectron2LayoutModel(BaseLayoutModel):
1820

19-
def __init__(self, config_name,
21+
def __init__(self, config_path,
2022
model_path = None,
2123
label_map = None,
2224
extra_config= []):
2325

2426
cfg = get_cfg()
25-
cfg.merge_from_file(config_name)
27+
config_path = PathManager.get_local_path(config_path)
28+
cfg.merge_from_file(config_path)
2629
cfg.merge_from_list(extra_config)
2730

2831
if model_path is not None:

0 commit comments

Comments
 (0)