forked from twak/bikegan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·61 lines (51 loc) · 1.84 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
import os
from options.test_options import TestOptions
from data import CreateDataLoader
from models import create_model
from util.visualizer import save_images
from itertools import islice
from util import html
import numpy as np
# helper function
def get_random_z(opt):
z_samples = np.random.normal(0, 1, (opt.n_samples + 1, opt.nz))
return z_samples
# options
opt = TestOptions().parse()
opt.nThreads = 1 # test code only supports nThreads=1
opt.batchSize = 1 # test code only supports batchSize=1
opt.serial_batches = True # no shuffle
# create dataset
data_loader = CreateDataLoader(opt)
dataset = data_loader.load_data()
model = create_model(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, G = %s, E = %s' % (
opt.name, opt.phase, opt.G_path, opt.E_path))
# sample random z
if opt.sync:
z_samples = get_random_z(opt)
# test stage
for i, data in enumerate(islice(dataset, opt.how_many)):
model.set_input(data)
print('process input image %3.3d/%3.3d' % (i, opt.how_many))
if not opt.sync:
z_samples = get_random_z(opt)
for nn in range(opt.n_samples + 1):
encode_B = nn == 0 and not opt.no_encode
_, real_A, fake_B, real_B, _ = model.test_simple(
z_samples[nn], encode_real_B=encode_B)
if nn == 0:
all_images = [real_A, real_B, fake_B]
all_names = ['input', 'ground truth', 'encoded']
else:
all_images.append(fake_B)
all_names.append('random sample%2.2d' % nn)
img_path = 'input image%3.3i' % i
save_images(webpage, all_images, all_names, img_path, None,
width=opt.fineSize, aspect_ratio=opt.aspect_ratio)
webpage.save()