-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
163 lines (135 loc) · 5.31 KB
/
config.py
File metadata and controls
163 lines (135 loc) · 5.31 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Configuration management for MegaFS
Centralized configuration for easier debugging and analysis
"""
import os
import yaml
from pathlib import Path
from typing import Dict, Any, Optional
from dataclasses import dataclass
@dataclass
class ModelConfig:
"""Model configuration parameters"""
size: int = 1024
latent_split: list = None
num_latents: int = 18
swap_indice: int = 4
num_blocks: int = 1
def __post_init__(self):
if self.latent_split is None:
self.latent_split = [4, 6, 8]
@dataclass
class PathConfig:
"""Path configuration for datasets and weights"""
dataset_root: str = ""
img_root: str = ""
mask_root: str = ""
checkpoint_dir: str = "weights"
data_map_path: str = "data_map.json"
def __post_init__(self):
if not self.dataset_root and self.img_root:
self.dataset_root = os.path.dirname(self.img_root)
@dataclass
class SwapConfig:
"""Swap method configuration"""
swap_type: str = "ftm" # ftm, injection, lcr
refine: bool = True
@property
def is_valid_swap_type(self) -> bool:
return self.swap_type in ["ftm", "injection", "lcr"]
class Config:
"""Main configuration class"""
def __init__(self,
swap_type: str = "ftm",
dataset_root: str = "",
img_root: str = "",
mask_root: str = "",
checkpoint_dir: str = "weights",
validate_paths: bool = True):
self.swap = SwapConfig(swap_type=swap_type)
self.paths = PathConfig(
dataset_root=dataset_root,
img_root=img_root,
mask_root=mask_root,
checkpoint_dir=checkpoint_dir
)
self.model = ModelConfig()
self._validate_paths = validate_paths
# Validate configuration
self._validate()
def _validate(self):
"""Validate configuration parameters"""
if not self.swap.is_valid_swap_type:
raise ValueError(f"Invalid swap_type: {self.swap.swap_type}. Must be one of: ftm, injection, lcr")
if self._validate_paths and not os.path.exists(self.paths.checkpoint_dir):
print(f"WARNING: Checkpoint directory not found: {self.paths.checkpoint_dir}")
def get_weight_filename(self) -> str:
"""Get the weight filename for the current swap type"""
return f"{self.swap.swap_type}_final.pth"
def get_stylegan2_filename(self) -> str:
"""Get the StyleGAN2 weight filename"""
return "stylegan2-ffhq-config-f.pth"
def print_config(self):
"""Print current configuration"""
print("INFO: MegaFS Configuration:")
print(f" Swap Type: {self.swap.swap_type}")
print(f" Dataset Root: {self.paths.dataset_root}")
print(f" Image Root: {self.paths.img_root}")
print(f" Mask Root: {self.paths.mask_root}")
print(f" Checkpoint Dir: {self.paths.checkpoint_dir}")
print(f" Weight File: {self.get_weight_filename()}")
print(f" StyleGAN2 File: {self.get_stylegan2_filename()}")
@classmethod
def from_yaml(cls, yaml_path: str, *, validate_paths: bool = True) -> 'Config':
"""Load configuration from YAML file"""
with open(yaml_path, 'r') as f:
config_dict = yaml.safe_load(f)
return cls(
swap_type=config_dict.get('swap_type', 'ftm'),
dataset_root=config_dict.get('dataset_root', ''),
img_root=config_dict.get('img_root', ''),
mask_root=config_dict.get('mask_root', ''),
checkpoint_dir=config_dict.get('checkpoint_dir', 'weights'),
validate_paths=validate_paths
)
def to_yaml(self, yaml_path: str):
"""Save configuration to YAML file"""
config_dict = {
'swap_type': self.swap.swap_type,
'dataset_root': self.paths.dataset_root,
'img_root': self.paths.img_root,
'mask_root': self.paths.mask_root,
'checkpoint_dir': self.paths.checkpoint_dir
}
# Ensure directory exists
Path(yaml_path).parent.mkdir(parents=True, exist_ok=True)
with open(yaml_path, 'w') as f:
yaml.dump(config_dict, f, default_flow_style=False)
print(f"Configuration saved to: {yaml_path}")
# Default configurations for different environments
DEFAULT_CONFIGS = {
"colab": Config(
swap_type="ftm",
dataset_root="/content/CelebAMask-HQ",
img_root="/content/CelebAMask-HQ/CelebA-HQ-img",
mask_root="/content/CelebAMask-HQ/CelebAMask-HQ-mask-anno",
checkpoint_dir="/content/drive/MyDrive/Datasets/weights",
validate_paths=False
),
"local": Config(
swap_type="ftm",
dataset_root="./CelebAMask-HQ",
img_root="./CelebAMask-HQ/CelebA-HQ-img",
mask_root="./CelebAMask-HQ/CelebAMask-HQ-mask-anno",
checkpoint_dir="./weights",
validate_paths=False
),
"evaluation": Config(
swap_type="ftm",
dataset_root="/content/CelebAMask-HQ",
img_root="/content/CelebAMask-HQ/CelebA-HQ-img",
mask_root="/content/CelebAMask-HQ/CelebAMask-HQ-mask-anno",
checkpoint_dir="/content/drive/MyDrive/Datasets/weights",
validate_paths=False
)
}