-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
57 lines (44 loc) · 1.61 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
import os
import argparse
import random
import shutil
import torch
from torchvision import transforms
from tqdm import tqdm
import numpy as np
from trainer import Trainer
from utils import get_config, get_model_list, get_loaders, write_image
parser = argparse.ArgumentParser()
parser.add_argument('--name', type=str,default="results/vggface_lofganours")
parser.add_argument('--gpu', type=str, default='0')
args = parser.parse_args()
conf_file = os.path.join(args.name, 'configs.yaml')
config = get_config(conf_file)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
transform_list = [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
transform = transforms.Compose(transform_list)
_, test_dataloader = get_loaders(config)
if __name__ == '__main__':
SEED = 0
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
out_dir = os.path.join(args.name, 'test')
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.makedirs(out_dir, exist_ok=True)
trainer = Trainer(config)
last_model_name = get_model_list(os.path.join(args.name, 'checkpoints'), "gen")
trainer.load_ckpt(last_model_name)
trainer.cuda()
trainer.eval()
with torch.no_grad():
for it, (imgs, _) in tqdm(enumerate(test_dataloader)):
imgs = imgs.cuda()
fake_xs = []
for i in range(config['num_generate']):
fake_xs.append(trainer.generate(imgs).unsqueeze(1))
fake_xs = torch.cat(fake_xs, dim=1)
write_image(it, out_dir, imgs, fake_xs, format='png')