-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
154 lines (117 loc) · 7.46 KB
/
test.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
import datetime
import torch
import pytorch_lightning as pl
from nets.model import *
from utils.data import *
from args import parse_args
def main():
args = parse_args()
print('Called with args:')
print(args)
curr_time = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
print('curr_time:', curr_time)
device = args.device
print("Device:", device)
# Ensure that all operations are deterministic on GPU (if used) for reproducibility
pl.seed_everything(args.seed)
torch.backends.cudnn.determinstic = True
torch.backends.cudnn.benchmark = False
data = {
'cifar10': CIFAR10Data(args),
'CelebA': CELEBAData(args),
'CelebA_128': CELEBA128Data(args),
'ImageNet': ImageNetData(args)
}[args.dataset]
train_loader = data.train_dataloader()
val_loader = data.val_dataloader()
test_loader = data.test_dataloader()
if args.raq_type == 'mb':
if args.model_type == 'vqvae':
logger = pl.loggers.TensorBoardLogger(save_dir=args.save_dir + "vqvae/model_based/" + str(args.dataset)
+ "/result"
+ "/voca_size:" + str(args.num_embeddings),
name='test',
version=args.seed)
trainer = pl.Trainer.from_argparse_args(args,
logger=logger,
devices=[args.cuda_ind],
max_epochs=args.n_epochs,
accelerator='gpu')
model = VQVAE_ONE.load_from_checkpoint(args.save_dir + "vqvae/model_based/" + str(args.dataset)
+"/voca_size:" + str(args.num_embeddings)
+ '/vqvae/version_0'
+ '/checkpoints/epoch=299-step=381300.ckpt',
args=args).to(device)
model.eval()
if args.cluster_target > args.num_embeddings:
args.i_clustered_weight = inverse_dkm(model, args)
trainer.test(model, test_loader)
result = eval_fid(model, args, model_type="mb_vq")
print(result)
if args.model_type == 'vqvae2':
logger = pl.loggers.TensorBoardLogger(save_dir=args.save_dir + "vqvae2/model_based/" + str(args.dataset)
+ "/result"
+ "/voca_size:" + str(args.num_embeddings),
name='test',
version=args.seed)
trainer = pl.Trainer.from_argparse_args(args,
logger=logger,
devices=[args.cuda_ind],
max_epochs=args.n_epochs,
accelerator='gpu')
model = VQVAE_ONE.load_from_checkpoint(args.save_dir + "vqvae2/model_based/" + str(args.dataset)
+"/voca_size:" + str(args.num_embeddings)
+ '/vqvae2/version_0'
+ '/checkpoints/epoch=299-step=381300.ckpt',
args=args).to(device)
model.eval()
if args.cluster_target > args.num_embeddings:
args.i_clustered_weight = inverse_dkm(model, args)
trainer.test(model, test_loader)
result = eval_fid(model, args, model_type="mb_vq2")
print(result)
if args.raq_type == 'dd':
if args.model_type == 'vqvae':
logger = pl.loggers.TensorBoardLogger(save_dir=args.save_dir + "vqvae/data_driven/" + str(args.dataset)
+ "/result"
+ "/voca_size:" + str(args.num_embeddings)
+ "/" + str(args.num_embeddings_min) + "_to_" + str(args.num_embeddings_max),
name='test',
version=args.seed)
trainer = pl.Trainer.from_argparse_args(args,
logger=logger,
devices=[args.cuda_ind],
max_epochs=args.n_epochs,
accelerator='gpu')
model = RAQVAE_ONE.load_from_checkpoint(args.save_dir + "vqvae/data_driven/" + str(args.dataset)
+"/voca_size:" + str(args.num_embeddings)
+ '/vqvae/version_0'
+ '/checkpoints/epoch=299-step=381300.ckpt',
args=args).to(device)
model.eval()
trainer.test(model, test_loader)
result = eval_fid(model, args, model_type="dd_vq")
print(result)
if args.model_type == 'vqvae2':
logger = pl.loggers.TensorBoardLogger(save_dir=args.save_dir + "vqvae2/data_driven/" + str(args.dataset)
+ "/result"
+ "/voca_size:" + str(args.num_embeddings)
+ "/" + str(args.num_embeddings_min) + "_to_" + str(args.num_embeddings_max),
name='test',
version=args.seed)
trainer = pl.Trainer.from_argparse_args(args,
logger=logger,
devices=[args.cuda_ind],
max_epochs=args.n_epochs,
accelerator='gpu')
model = VQVAE_ONE.load_from_checkpoint(args.save_dir + "vqvae2/data_driven/" + str(args.dataset)
+"/voca_size:" + str(args.num_embeddings)
+ '/vqvae2/version_0'
+ '/checkpoints/epoch=299-step=381300.ckpt',
args=args).to(device)
model.eval()
trainer.test(model, test_loader)
result = eval_fid(model, args, model_type="dd_vq2")
print(result)
if __name__ == '__main__':
main()