forked from facebookresearch/vissl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
146 lines (113 loc) · 4.36 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import logging
import os
import re
import sys
from typing import Any, Callable, List
import pkg_resources
from omegaconf import OmegaConf
from vissl.utils.hydra_config import compose_hydra_configuration
logger = logging.getLogger("vissl")
# List all the config files, used to generate the unit tests on the fly
def list_config_files(
dir_path: str, is_valid_file: Callable[[str], bool] = lambda x: True
):
"""
Recursively list all YAML files under the folder "configs / dir_path"
excluding all paths in which one of the terms in "exclude_folders" appear
"""
resource_name = "configs"
assert pkg_resources.resource_isdir(resource_name, dir_path)
all_items = pkg_resources.resource_listdir(resource_name, dir_path)
config_files = []
for item in all_items:
subpath = f"{dir_path}/{item}"
if pkg_resources.resource_isdir(resource_name, subpath):
config_files.extend(list_config_files(subpath, is_valid_file))
elif subpath.endswith(".yaml") and is_valid_file(subpath):
config_files.append(subpath)
return config_files
def exclude_folders(folders: List[str]) -> Callable[[str], bool]:
"""
Predicate for list_config_files allowing to list all files
but the ones in specific list of invalid folders
"""
def is_path_allowed(path: str) -> bool:
if exclude_folders and any(x in path for x in folders):
return False
return True
return is_path_allowed
def only_benchmark_models(path: str) -> bool:
"""
Predicate for list_config_files allowing to list all files
that are benchmarks or models used in benchmarks
"""
dir_path, file_name = os.path.split(path)
return file_name.startswith("eval_") or dir_path.endswith("models")
def only_pretrain_models(path: str) -> bool:
"""
Predicate for list_config_files allowing to list all files
that are pre-training methods or models plugged in those methods
"""
dir_path, _ = os.path.split(path)
return dir_path.endswith("pretrain") or dir_path.endswith("models")
def create_valid_input(input_list: List[str]) -> List[str]:
return [re.sub("config/", "config=", item) for item in input_list]
# we skip object detection configs since they are for detectron2 codebase
BENCHMARK_CONFIGS = create_valid_input(
list_config_files("config/benchmark", exclude_folders(["object_detection"]))
)
BENCHMARK_MODEL_CONFIGS = create_valid_input(
list_config_files("config/benchmark", only_benchmark_models)
)
PRETRAIN_CONFIGS = create_valid_input(list_config_files("config/pretrain"))
PRETRAIN_MODEL_CONFIGS = create_valid_input(
list_config_files("config/pretrain", only_pretrain_models)
)
INTEGRATION_TEST_CONFIGS = create_valid_input(
list_config_files("config/test/integration_test")
)
ROOT_CONFIGS = create_valid_input(
list_config_files(
"config", exclude_folders(["models", "optimization", "object_detection"])
)
)
ROOT_OSS_CONFIGS = create_valid_input(
list_config_files(
"config", exclude_folders(["models", "optimization", "object_detection", "fb"])
)
)
# configs that require loss optimization and hence trainable
ROOT_LOSS_CONFIGS = create_valid_input(
list_config_files(
"config",
exclude_folders(
[
"models",
"optimization",
"object_detection",
"nearest_neighbor",
"feature_extraction",
"fb",
"test/transforms",
]
),
)
)
UNIT_TEST_CONFIGS = create_valid_input(list_config_files("config/test/cpu_test"))
class SSLHydraConfig(object):
def __init__(self, overrides: List[Any] = None):
self.overrides = []
if overrides is not None and len(overrides) > 0:
self.overrides.extend(overrides)
cfg = compose_hydra_configuration(self.overrides)
self.default_cfg = cfg
@classmethod
def from_configs(cls, config_files: List[Any] = None):
return cls(config_files)
def override(self, config_files: List[Any]):
sys.argv = config_files
cli_conf = OmegaConf.from_cli(config_files)
self.default_cfg = OmegaConf.merge(self.default_cfg, cli_conf)