forked from eezkni/UEGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.py
183 lines (144 loc) · 8.2 KB
/
tester.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#-*- coding:utf-8 -*-
import os
import time
import torch
import datetime
import torch.nn as nn
from torchvision.utils import save_image
from losses import PerceptualLoss, TVLoss
from utils import Logger, denorm, ImagePool, GaussianNoise
from models import Generator, Discriminator
from metrics.NIMA.CalcNIMA import calc_nima
from metrics.CalcPSNR import calc_psnr
from metrics.CalcSSIM import calc_ssim
from tqdm import *
from data_loader import InputFetcher
from utils import tensor_to_img
from torch.nn.parallel import DistributedDataParallel
class Tester(object):
def __init__(self, loaders, args):
# data loader
self.loaders = loaders
# Model configuration.
self.args = args
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Directories.
self.model_save_path = os.path.join(args.save_root_dir, args.version, args.model_save_path)
self.sample_path = os.path.join(args.save_root_dir, args.version, args.sample_path)
self.log_path = args.log_path
self.test_result_path = os.path.join(args.save_root_dir, args.version, args.test_result_path)
# Build the model and tensorboard.
self.build_model()
if self.args.use_tensorboard:
self.build_tensorboard()
def test(self):
""" Test UEGAN ."""
self.load_pretrained_model(self.args.pretrained_model_path)
start_time = time.time()
test_start = 0
test_total_steps = len(self.loaders.tes)
self.fetcher_test = InputFetcher(self.loaders.tes)
test = {}
test_save_path = self.test_result_path + '/' + 'test_results'
test_compare_save_path = self.test_result_path + '/' + 'test_compare'
if not os.path.exists(test_save_path):
os.makedirs(test_save_path)
if not os.path.exists(test_compare_save_path):
os.makedirs(test_compare_save_path)
self.G.eval()
test_gen_imgs = []
test_label_imgs = []
pbar = tqdm(total=(test_total_steps - test_start), desc='Test epoches', position=test_start)
pbar.write("============================== Start tesing ==============================")
with torch.no_grad():
for test_step in range(test_start, test_total_steps):
input = next(self.fetcher_test)
test_real_raw, test_real_label, test_name = input.img_raw, input.img_exp, input.img_name
test_fake_exp = self.G(test_real_raw)
for i in range(0, denorm(test_real_raw.data).size(0)):
save_imgs = denorm(test_fake_exp.data)[i:i + 1,:,:,:]
img_filename = os.path.basename(test_name[i]).split('.')[0]
save_path = os.path.join(test_save_path, '{:s}.png'.format(img_filename))
save_image(save_imgs, save_path)
if self.args.save_input:
label_save_img = denorm(test_real_label.data)[i:i + 1,:,:,:]
label_save_path = os.path.join(test_save_path, '{:s}_{:0>3.2f}_testOrig.png'.format(img_filename, self.args.pretrained_model_epoch))
save_image(label_save_img, label_save_path)
input_save_imgs = denorm(test_real_raw.data)[i:i + 1,:,:,:]
input_save_path = os.path.join(test_save_path, '{:s}_{:0>3.2f}_testInput.png'.format(img_filename, self.args.pretrained_model_epoch))
save_image(input_save_imgs, input_save_path)
fake_img_rgb = tensor_to_img(save_imgs.detach())
test_gen_imgs.append(fake_img_rgb)
test_real_label = denorm(test_real_label.data)[i:i + 1,:,:,:]
real_label_rgb = tensor_to_img(test_real_label.detach())
test_label_imgs.append(real_label_rgb)
#save_imgs_compare = torch.cat([denorm(test_real_raw.data)[i:i + 1,:,:,:], denorm(test_fake_exp.data)[i:i + 1,:,:,:]], 3)
#save_image(save_imgs_compare, os.path.join(test_compare_save_path, '{:s}_{:0>3.2f}_testRealRaw_testFakeExp.png'.format(img_filename, self.args.pretrained_model_epoch)))
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
if test_step % self.args.info_step == 0:
pbar.write("=== Elapse:{}, Save {:>3d}-th test_fake_exp images into {} ===".format(elapsed, test_step, test_save_path))
test['test/testFakeExp'] = denorm(test_fake_exp.detach().cpu())
test['test_compare/testRealRaw_testFakeExp'] = torch.cat([denorm(test_real_raw.cpu()), denorm(test_fake_exp.detach().cpu())], 3)
pbar.update(1)
if self.args.use_tensorboard:
for tag, images in test.items():
self.logger.images_summary(tag, images, test_step + 1)
if self.args.is_test_nima:
self.nima_result_save_path = './results/nima_test_results/'
curr_nima = calc_nima(test_save_path, self.nima_result_save_path, self.args.pretrained_model_epoch)
print("====== Avg. NIMA: {:>.4f} ======".format(curr_nima))
if self.args.is_test_psnr_ssim:
self.psnr_save_path = './results/psnr_test_results/'
curr_psnr = calc_psnr(test_gen_imgs, test_label_imgs, self.psnr_save_path, self.args.pretrained_model_epoch)
print("====== Avg. PSNR: {:>.4f} dB ======".format(curr_psnr))
self.ssim_save_path = './results/ssim_test_results/'
curr_ssim = calc_ssim(test_gen_imgs, test_label_imgs, self.ssim_save_path, self.args.pretrained_model_epoch)
print("====== Avg. SSIM: {:>.4f} ======".format(curr_ssim))
"""define some functions"""
def build_model(self):
"""Create a generator and a discriminator."""
self.G = Generator(self.args.g_conv_dim, self.args.g_norm_fun, self.args.g_act_fun, self.args.g_use_sn).to(self.device)
#self.D = Discriminator(self.args.d_conv_dim, self.args.d_norm_fun, self.args.d_act_fun, self.args.d_use_sn, self.args.adv_loss_type).to(self.device)
if self.args.parallel_mode == "dataparallel":
self.G.to(self.args.gpu_ids[0])
#self.D.to(self.args.gpu_ids[0])
self.G = nn.DataParallel(self.G, self.args.gpu_ids)
#self.D = nn.DataParallel(self.D, self.args.gpu_ids)
#elif self.args.parallel_mode == "ddp":
# self.G = DistributedDataParallel(self.G, device_ids=[torch.cuda.current_device()])
print("=== Models have been created ===")
# print network
if self.args.is_print_network:
self.print_network(self.G, 'Generator')
#self.print_network(self.D, 'Discriminator')
def print_network(self, model, name):
"""Print out the network information."""
num_params = 0
for p in model.parameters():
num_params += p.numel()
# print(model)
print("=== The number of parameters of the above model [{}] is [{}] or [{:>.4f}M] ===".format(name, num_params, num_params / 1e6))
def load_pretrained_model(self, checkpoint_path):
if torch.cuda.is_available():
# save on GPU, load on GPU
checkpoint = torch.load(checkpoint_path)
try:
self.G.load_state_dict(checkpoint['G_net'])
except RuntimeError:
G_state_dict = {}
for key, value in checkpoint['G_net'].items():
# remove "module."
key = key[7:]
G_state_dict[key] = value
self.G.load_state_dict(G_state_dict)
#self.D.load_state_dict(checkpoint['D_net'])
else:
# save on GPU, load on CPU
checkpoint = torch.load(checkpoint_path, map_location=lambda storage, loc: storage)
self.G.load_state_dict(checkpoint['G_net'])
#self.D.load_state_dict(checkpoint['D_net'])
print("=========== loaded trained models (epochs: {})! ===========".format(checkpoint_path))
def build_tensorboard(self):
"""Build a tensorboard logger."""
self.logger = Logger(self.log_path)