-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
executable file
·212 lines (178 loc) · 7.62 KB
/
eval.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
# ------------------------------------------------------------------------------
# OptVQ: Preventing Local Pitfalls in Vector Quantization via Optimal Transport
# Copyright (c) 2024 Borui Zhang. All Rights Reserved.
# Licensed under the MIT License [see LICENSE for details]
# ------------------------------------------------------------------------------
from omegaconf import OmegaConf
import os
import warnings
warnings.filterwarnings("ignore")
from tqdm import tqdm
import pyiqa
import torch
import torch.distributed as dist
import torch.nn as nn
from torch.utils.data.dataloader import DataLoader
import torch.multiprocessing as mp
from optvq.trainer.pipeline import (
get_setup_optimizers, setup_dataset, setup_config,
setup_dataloader, setup_model
)
from optvq.trainer.arguments import get_parser
import optvq.utils.logger as L
from optvq.utils.init import seed_everything
from optvq.data.preprocessor import get_recover_map
from optvq.utils.func import dist_all_gather
from optvq.utils.metrics import FIDMetric
_USE_TORCHRUN_: bool = False
def evaluate(config: OmegaConf, device: torch.device, model: nn.Module, loader: DataLoader):
# set the model to eval
model.eval()
model_ori = model.module if config.is_distributed else model
recover_map = get_recover_map(config.data.preprocess)
# codebook utility meter
num_code = model_ori.quantize.n_e
codebook_usage = torch.zeros(num_code, device=device)
# meters
lpips_list = []
psnr_list = []
ssim_list = []
psnr_computer = pyiqa.create_metric(
metric_name="psnr", test_y_channel=True, data_range=1.0, color_space="ycbcr", device=device
)
ssim_computer = pyiqa.create_metric(
metric_name="ssim", device=device
)
lpips_computer = pyiqa.create_metric(
metric_name="lpips", color_space="ycbcr", device=device
)
fid_computer = FIDMetric(device=device)
# iterate over the dataloader
iterator = tqdm(enumerate(loader), total=len(loader))
pbar = L.ProgressWithIndices(total=len(loader))
with torch.no_grad():
for i, (x, _) in iterator:
pbar.update()
L.log.update_steps()
x = x.to(device)
quant, _, indices = model_ori.encode(x)
x_rec = model_ori.decode(quant)
x = recover_map(x).float()
x_rec = recover_map(x_rec).float()
x_rec = x_rec.clamp(min=0.0, max=1.0)
# visualize the reconstruction results
if i == 0:
max_vis_num = 8
L.log.add_images("val/rec", x_rec[:max_vis_num])
L.log.add_images("val/x", x[:max_vis_num])
if config.is_distributed:
# gather the data
x = dist_all_gather(x)
x_rec = dist_all_gather(x_rec)
lpips_score = lpips_computer(x, x_rec)
lpips_list.append(lpips_score)
psnr_score = psnr_computer(x, x_rec)
psnr_list.append(psnr_score)
ssim_score = ssim_computer(x, x_rec)
ssim_list.append(ssim_score)
fid_computer.update(x, x_rec)
if indices is not None:
indices = indices.view(-1)
codebook_usage.index_add_(0, indices, torch.ones_like(indices, dtype=codebook_usage.dtype))
if config.is_distributed:
dist.barrier()
if config.local_rank == 0:
psnr_score = torch.cat(psnr_list).mean().item()
ssim_score = torch.cat(ssim_list).mean().item()
lpips_score = torch.cat(lpips_list).mean().item()
fid_score = fid_computer.result()
L.log.add_scalar("val/psnr", psnr_score)
L.log.add_scalar("val/ssim", ssim_score)
L.log.add_scalar("val/lpips", lpips_score)
L.log.add_scalar("val/fid", fid_score)
pbar.print(
prefix=f"Evaluation: ",
content=L.log.show("val")
)
def main_worker(gpu, ngpus_per_node, opt):
torch.backends.cudnn.benchmark = True
torch.cuda.set_device(gpu)
device = torch.device(f"cuda:{gpu}")
# setup the loggers and configs
L.config_loggers(log_dir=opt.log_dir, local_rank=gpu)
L.log.info(f"Start a new training logger at {opt.log_dir}")
config = setup_config(opt)
L.log.save_configs(config)
L.log.info(f"Save the configurations to {opt.log_dir}")
# overwrite certain configurations
config.is_distributed = opt.is_distributed
config.resume = opt.resume
config.mode = opt.mode
config.gpu = gpu
## distributed configurations
config.local_rank = int(gpu)
config.world_size = int(ngpus_per_node * config.train.nnodes)
config.world_rank = int(opt.device_rank * ngpus_per_node + gpu)
# setup torch.distributed
if config.is_distributed:
if _USE_TORCHRUN_:
dist.init_process_group(backend="nccl")
else:
dist.init_process_group(
backend="nccl", init_method=config.train.dist_url,
rank=config.world_rank, world_size=config.world_size
)
dist.barrier()
# setup the datasets
train_data, val_data = setup_dataset(config)
train_loader = setup_dataloader(train_data, config.data.batch_size, config.is_distributed, is_train=True)
val_loader = setup_dataloader(val_data, config.data.test_batch_size, config.is_distributed, is_train=False)
# setup the models
model = setup_model(config, device)
model_ori = model.module if config.is_distributed else model
# resume from the checkpoint
resume_path = os.path.join(opt.resume, "checkpoint.pth")
assert os.path.exists(resume_path), f"Resume {resume_path} does not exist!"
checkpoint_dict = torch.load(resume_path, map_location=device)
model_ori.load_state_dict(checkpoint_dict["model"])
# model enterpoint
if config.train.enterpoint is not None:
params = torch.load(config.train.enterpoint, map_location=device)["model"]
# delete the quantize embeddings
if params.get("quantize.embedding.weight", None) is not None:
del params["quantize.embedding.weight"]
elif params.get("_orig_mod.quantize.embedding.weight", None) is not None:
del params["_orig_mod.quantize.embedding.weight"]
missing_keys, unexpected_keys = model_ori.load_state_dict(params, strict=False)
assert len(unexpected_keys) == 0, f"Unexpected keys: {unexpected_keys}"
L.log.info(f"Loaded the model from {config.train.enterpoint}")
dist.barrier()
# start testing
L.log.info("\n\n### Start testing. ###")
evaluate(
config=config, device=device, model=model, loader=val_loader
)
def main():
# parse the arguments
parser = get_parser()
opt, unknown = parser.parse_known_args()
# setup seed
if opt.seed is not None:
seed_everything(opt.seed)
print(f"Seed is set to {opt.seed}")
# setup devices
if opt.gpu is None:
opt.gpu = list(range(torch.cuda.device_count()))
ngpus_per_node = len(opt.gpu)
if opt.is_distributed:
assert len(opt.gpu) > 1, "Expected more than 1 GPU for distributed training."
if _USE_TORCHRUN_:
main_worker(int(os.environ["LOCAL_RANK"]), ngpus_per_node, opt)
else:
mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, opt))
else:
assert isinstance(opt.gpu, int) or len(opt.gpu) == 1, "Expected a single GPU for non-distributed training."
opt.gpu = opt.gpu[0] if isinstance(opt.gpu, list) else opt.gpu
main_worker(opt.gpu, ngpus_per_node, opt)
if __name__ == "__main__":
main()