forked from xuekt98/BBDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_vqgan.py
61 lines (52 loc) · 2.29 KB
/
test_vqgan.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
from model.VQGAN.vqgan import VQModel
import yaml
import argparse
import omegaconf.dictconfig
import os
import torch
import numpy as np
if __name__ == '__main__':
f = open('/mnt/disk1/mbbank/tien/BBDM_folk/configs/test_vqgan.yaml', 'r')
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict) or isinstance(value, omegaconf.dictconfig.DictConfig):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
i = 0
file_names = []
for npy_file in os.listdir('/mnt/disk3/tiennh/data4vq/test'):
i += 1
if i == 1000:
break
file_names.append(npy_file)
from tqdm import tqdm
def cal_mae(weight_path, test_dir_path, file_names):
f = open('/mnt/disk1/mbbank/tien/BBDM_folk/configs/test_vqgan.yaml', 'r')
dict_config = yaml.load(f, Loader=yaml.FullLoader)
nconfig = dict2namespace(dict_config)
nconfig.model.VQGAN.params.ckpt_path = weight_path
vq = VQModel(**vars(nconfig.model.VQGAN.params))
# load npy file in test_dir_path
maes = []
for npy_file in tqdm(file_names):
x_np = np.load(os.path.join(test_dir_path, npy_file), allow_pickle=True)
x = torch.from_numpy(x_np)
x = x.unsqueeze(0)
input = x
quant, diff, _ = vq.encode(input)
dec = vq.decode(quant)
# caculate MAE between input and dec
mae = torch.abs(dec - x ).mean()
maes.append(mae.item())
# print(f'File: {npy_file}, MAE: {torch.mean(diff).item()}')
print(f'MAE: {np.mean(maes)}')
print("len :", len(maes))
cal_mae('/mnt/disk3/tiennh/taming-transformers/last_82.ckpt', '/mnt/disk3/tiennh/data4vq/test', file_names)
print("=====================================================")
cal_mae('/mnt/disk3/tiennh/taming-transformers/last_90.ckpt', '/mnt/disk3/tiennh/data4vq/test', file_names)
print("======================================================")
cal_mae('/mnt/disk3/tiennh/taming-transformers/last_55.ckpt', '/mnt/disk3/tiennh/data4vq/test', file_names)