-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
83 lines (62 loc) · 2.95 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
import os
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from util.visualizer_test import save_images
from util import makedir
import time
if __name__ == '__main__':
opt = TestOptions().parse() # get test options
# hard-code some parameters for test
opt.phase = 'val'
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 # disable data shuffling; comment this line if results on randomly chosen images are needed.
opt.no_flip = True # no flip; comment this line if results on flipped images are needed.
model = create_model(opt) # create a model given opt.model and other options
model.setup(opt) # regular setup: load and print networks; create schedulers
dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
# create a website
result_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch)) # define the result directory
if opt.load_iter > 0: # load_iter is 0 by default
result_dir = '{:s}_iter{:d}'.format(result_dir, opt.load_iter)
print('creating result directory', result_dir)
result_dir = makedir.MakeDir(result_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))
test_losses = None
last_time = time.time()
time_meter = 0.
data_count = 0.
model.eval()
for i, data in enumerate(dataset):
data_time = time.time() - last_time
last_time = time.time()
if i >= opt.num_test: # only apply our model to opt.num_test images.
break
model.set_input(data) # unpack data from data loader
interval = model.test() # run inference
time_meter += interval
data_count += 1
model.cal_test_loss()
losses = model.get_current_losses(in_test=True)
result_str = f"idx: {i}"
for k, v in losses.items():
result_str += f', loss {k} = {v}'
print(result_str)
if test_losses is None:
test_losses = losses
else:
for k, v in losses.items():
test_losses[k] += v
if i % 5 == 0:
print('processing (%04d)-th image' % i)
if not opt.only_metrics:
visuals = model.get_current_visuals() # get image results
img_path = model.get_image_paths() # get image paths
save_images(result_dir, visuals, img_path)
calculating_time = time.time() - last_time
last_time = time.time()
print(f"Data time: {data_time}, Calculation time: {calculating_time}")
for k in test_losses.keys():
test_losses[k] /= len(dataset)
print(f'Test loss {k} from all samples: {test_losses[k]}')
print('The execution time of per image:', time_meter, data_count, time_meter / data_count)