forked from junyanz/BicycleGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·50 lines (42 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
import os
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from util.visualizer import save_images
from itertools import islice
from util import html
# options
opt = TestOptions().parse()
opt.num_threads = 1 # test code only supports num_threads=1
opt.batch_size = 1 # test code only supports batch_size=1
opt.serial_batches = True # no shuffle
# create dataset
dataset = create_dataset(opt)
model = create_model(opt)
model.setup(opt)
model.eval()
print('Loading model %s' % opt.model)
# create website
web_dir = os.path.join(opt.results_dir, opt.phase + '_sync' if opt.sync else opt.phase)
webpage = html.HTML(web_dir, 'Training = %s, Phase = %s, Class =%s' % (opt.name, opt.phase, opt.name))
# sample random z
if opt.sync:
z_samples = model.get_z_random(opt.n_samples + 1, opt.nz)
# test stage
for i, data in enumerate(islice(dataset, opt.num_test)):
model.set_input(data)
print('process input image %3.3d/%3.3d' % (i, opt.num_test))
if not opt.sync:
z_samples = model.get_z_random(opt.n_samples + 1, opt.nz)
for nn in range(opt.n_samples + 1):
encode = nn == 0 and not opt.no_encode
real_A, fake_B, real_B = model.test(z_samples[[nn]], encode=encode)
if nn == 0:
images = [real_A, real_B, fake_B]
names = ['input', 'ground truth', 'encoded']
else:
images.append(fake_B)
names.append('random_sample%2.2d' % nn)
img_path = 'input_%3.3d' % i
save_images(webpage, images, names, img_path, aspect_ratio=opt.aspect_ratio, width=opt.crop_size)
webpage.save()