-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.py
More file actions
86 lines (63 loc) · 1.99 KB
/
configuration.py
File metadata and controls
86 lines (63 loc) · 1.99 KB
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
import os
from dataclasses import dataclass as og_dataclass
from dataclasses import is_dataclass
import yaml
def dataclass(*args, **kwargs):
"""
Creates a dataclass that can handle nested dataclasses
and automatically convert dictionaries to dataclasses.
"""
def wrapper(cls):
cls = og_dataclass(cls, **kwargs)
original_init = cls.__init__
def __init__(self, *args, **kwargs):
for name, value in kwargs.items():
field_type = cls.__annotations__.get(name, None)
if is_dataclass(field_type) and isinstance(value, dict):
new_obj = field_type(**value)
kwargs[name] = new_obj
original_init(self, *args, **kwargs)
cls.__init__ = __init__
return cls
return wrapper(args[0]) if args else wrapper
@dataclass
class LLavaConfig:
model: str
@dataclass
class DatasetConfig:
json_path: str
image_dir: str
mask_dir: str
def __post_init__(self):
self.json_path = os.path.expanduser(self.json_path)
self.image_dir = os.path.expanduser(self.image_dir)
self.mask_dir = os.path.expanduser(self.mask_dir)
@dataclass
class SAMConfig:
model: str
checkpoint_dir: str
resize: int
n_masks: int
def __post_init__(self):
self.checkpoint_dir = os.path.expanduser(self.checkpoint_dir)
@dataclass
class AlphaCLIPConfig:
model: str
checkpoint_dir: str
def __post_init__(self):
self.checkpoint_dir = os.path.expanduser(self.checkpoint_dir)
@dataclass
class OthersConfig:
wandb_token: str
def __post_init__(self):
self.wandb_token = os.path.expanduser(self.wandb_token)
@dataclass
class ProjectConfig:
llava: LLavaConfig
dataset: DatasetConfig
sam: SAMConfig
alphaclip: AlphaCLIPConfig
others: OthersConfig
def load_yaml_config(path) -> ProjectConfig:
with open(path) as file:
return ProjectConfig(**yaml.load(file, Loader=yaml.FullLoader))