-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
225 lines (187 loc) · 6.16 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# %%
from numpy import ndarray
from torch import Tensor
import yaml
import os
import torch.nn as nn
from glob import glob
import torch as t
import cv2
import numpy as np
from shutil import copyfile
from model import Discriminator
def load_config(config_file_path: str) -> dict:
"""
load config from a yaml file and returns a dictionary that contains the configuration and print them
--------
params:
config_file_path: your config file path, normally is the config.yaml, you should not change the name
"""
# check the file location
if not os.path.exists(config_file_path):
raise FileNotFoundError(
"the file does not exist, please check the path")
with open(config_file_path, "r", encoding="utf-8") as f:
args = yaml.load(f.read(), yaml.FullLoader)
print("###################YOUR SETTINGS###################")
for key in args.keys():
print(f"[{key}]".ljust(20, " "), f"--->{args[key]}")
create_folder(args)
check_config(args)
return args
def create_folder(config: dict) -> None:
"""
Create a directory to store training results
--------
params:
config, a dictionary that stores the settings
"""
result_path = os.path.join(config["result_dir"], config["dataset"])
if not os.path.exists(result_path):
os.makedirs(os.path.join(result_path, "img"))
os.makedirs(os.path.join(result_path, "model"))
os.makedirs(os.path.join(result_path, "test"))
copyfile("config.yaml", os.path.join(
config["result_dir"], config["dataset"], "config.yaml"))
def check_config(config: dict) -> bool:
"""
Check for incorrect parameters
------
"""
assert config["batch_size"] >= 1
assert config["iteration"] >= 1
assert config["ch"] >= 1
assert len(config["dataset"]) > 0
def add_spectral_norm(m: Discriminator) -> Discriminator:
"""
add Spectral Normalization to the Module
------
parameter:
m: the module that you want to add Normalization
returns:
the module with Spectral Normalization
"""
for name, layer in m.named_children():
m.add_module(name, add_spectral_norm(layer))
if isinstance(m, (nn.Linear, nn.Conv2d)):
return nn.utils.spectral_norm(m)
else:
return m
def find_latest_model(result_path: str, dataset_name: str) -> tuple[int, str]:
"""
Find the latest model that meets the criteria
-------
Parameter:
result_path: the path to store the results
dataset_name: the name of the dataset
Returns:
the iteration number of the model, 1 if not exists\n
the relative path of the latest model, None if not exists\n
"""
model_list = glob(os.path.join(result_path, dataset_name, "model", "*.pt"))
if not len(model_list) == 0:
model_list.sort()
start_iter = int(model_list[-1].split("_")[-1].split(".")[0])
return start_iter+1, os.path.join(model_list[-1])
else:
return 1, None
def save_model(genA2B: nn.Module, genB2A: nn.Module, disGA: nn.Module, disGB: nn.Module, disLA: nn.Module, disLB: nn.Module, dir: str, dataset: str, step: int) -> None:
"""
save the model to the specific directory
------
Parameter:
genA2B, genB2A, disGA, disGB, disLA, disLB: the module you want to save
dir: the directory you want to save
dataset: the dataset name
step: the training step
Returns:
None
"""
params = {}
params['genA2B'] = genA2B.state_dict()
params['genB2A'] = genB2A.state_dict()
params['disGA'] = disGA.state_dict()
params['disGB'] = disGB.state_dict()
params['disLA'] = disLA.state_dict()
params['disLB'] = disLB.state_dict()
t.save(params, os.path.join(
dir, dataset + '_params_%07d.pt' % step))
def tensor2numpy(x: Tensor) -> ndarray:
"""
transform tensor to numpy.ndarray
------
Args:
x: the tensor
Returns:
the ndarray obj
"""
return x.detach().cpu().numpy().transpose(1, 2, 0)
def denormalization(x: Tensor) -> Tensor:
"""
Regularize images
------
Args:
x: the input image
Returns:
the Regularized image
"""
return x*0.5+0.5
def RGB2BGR(x: ndarray) -> ndarray:
return cv2.cvtColor(x, cv2.COLOR_RGB2BGR)
def cam(x, size=256) -> ndarray:
"""
generate heatmap accroding to the attention module
-------
Args:
x: the attention module's output
size: the image size
Returns:
the heatmap
"""
# norm
x = x-np.min(x)
cam_img = x/np.max(x)
# to uint8
cam_img = np.uint8(255*cam_img)
# resize
cam_img = cv2.resize(cam_img, (640, 360))
cam_img = cv2.applyColorMap(cam_img, cv2.COLORMAP_JET)
return cam_img/255
def handle_generated_image(x: Tensor) -> ndarray:
"""
Combines operations such as regularization and tensor to numpy
"""
return RGB2BGR(tensor2numpy(denormalization(x)))
def handle_cam_heatmap(x: Tensor, size: int) -> ndarray:
"""
Combined with cam, tensor to numpy and other operations
"""
return cam(tensor2numpy(x), size=size)
def set_requires_grad(nets: list[nn.Module], requires_grad: bool) -> None:
"""
disable or enable the grad computing of the networks, Set requies_grad=Fasle for all the networks to avoid unnecessary computations
------
Args:
nets:list[nn.Module] the modules you want to do operation
requires_grad: bool, false to disable the grad and on to enable grad
"""
if not isinstance(nets, list):
nets = [nets]
for net in nets:
if net is not None:
for param in net.parameters():
param.requires_grad = requires_grad
def disable_grad(nets: list[nn.Module]) -> None:
"""
call set_requires_grad(nets:list[nn.Module],requires_grad=False)
"""
set_requires_grad(nets, False)
def enable_grad(nets: list[nn.Module]) -> None:
"""
call set_requires_grad(nets:list[nn.Module],requires_grad=True)
"""
set_requires_grad(nets, True)
def load_path_yamls():
with open("path.yaml", "r", encoding="utf-8") as f:
path = yaml.load(f.read(), yaml.FullLoader)
return path