-
Notifications
You must be signed in to change notification settings - Fork 109
/
trainer.py
146 lines (126 loc) · 5.59 KB
/
trainer.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
import os.path
import datetime
import cv2
import numpy as np
from skimage.metrics import structural_similarity as compare_ssim
from core.utils import preprocess, metrics
import lpips
import torch
loss_fn_alex = lpips.LPIPS(net='alex')
def train(model, ims, real_input_flag, configs, itr):
cost = model.train(ims, real_input_flag)
if configs.reverse_input:
ims_rev = np.flip(ims, axis=1).copy()
cost += model.train(ims_rev, real_input_flag)
cost = cost / 2
if itr % configs.display_interval == 0:
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'itr: ' + str(itr))
print('training loss: ' + str(cost))
def test(model, test_input_handle, configs, itr):
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'test...')
test_input_handle.begin(do_shuffle=False)
res_path = os.path.join(configs.gen_frm_dir, str(itr))
os.mkdir(res_path)
avg_mse = 0
batch_id = 0
img_mse, ssim, psnr = [], [], []
lp = []
for i in range(configs.total_length - configs.input_length):
img_mse.append(0)
ssim.append(0)
psnr.append(0)
lp.append(0)
# reverse schedule sampling
if configs.reverse_scheduled_sampling == 1:
mask_input = 1
else:
mask_input = configs.input_length
real_input_flag = np.zeros(
(configs.batch_size,
configs.total_length - mask_input - 1,
configs.img_width // configs.patch_size,
configs.img_width // configs.patch_size,
configs.patch_size ** 2 * configs.img_channel))
if configs.reverse_scheduled_sampling == 1:
real_input_flag[:, :configs.input_length - 1, :, :] = 1.0
while (test_input_handle.no_batch_left() == False):
batch_id = batch_id + 1
test_ims = test_input_handle.get_batch()
test_dat = preprocess.reshape_patch(test_ims, configs.patch_size)
test_ims = test_ims[:, :, :, :, :configs.img_channel]
img_gen = model.test(test_dat, real_input_flag)
img_gen = preprocess.reshape_patch_back(img_gen, configs.patch_size)
output_length = configs.total_length - configs.input_length
img_out = img_gen[:, -output_length:]
# MSE per frame
for i in range(output_length):
x = test_ims[:, i + configs.input_length, :, :, :]
gx = img_out[:, i, :, :, :]
gx = np.maximum(gx, 0)
gx = np.minimum(gx, 1)
mse = np.square(x - gx).sum()
img_mse[i] += mse
avg_mse += mse
# cal lpips
img_x = np.zeros([configs.batch_size, 3, configs.img_width, configs.img_width])
if configs.img_channel == 3:
img_x[:, 0, :, :] = x[:, :, :, 0]
img_x[:, 1, :, :] = x[:, :, :, 1]
img_x[:, 2, :, :] = x[:, :, :, 2]
else:
img_x[:, 0, :, :] = x[:, :, :, 0]
img_x[:, 1, :, :] = x[:, :, :, 0]
img_x[:, 2, :, :] = x[:, :, :, 0]
img_x = torch.FloatTensor(img_x)
img_gx = np.zeros([configs.batch_size, 3, configs.img_width, configs.img_width])
if configs.img_channel == 3:
img_gx[:, 0, :, :] = gx[:, :, :, 0]
img_gx[:, 1, :, :] = gx[:, :, :, 1]
img_gx[:, 2, :, :] = gx[:, :, :, 2]
else:
img_gx[:, 0, :, :] = gx[:, :, :, 0]
img_gx[:, 1, :, :] = gx[:, :, :, 0]
img_gx[:, 2, :, :] = gx[:, :, :, 0]
img_gx = torch.FloatTensor(img_gx)
lp_loss = loss_fn_alex(img_x, img_gx)
lp[i] += torch.mean(lp_loss).item()
real_frm = np.uint8(x * 255)
pred_frm = np.uint8(gx * 255)
psnr[i] += metrics.batch_psnr(pred_frm, real_frm)
for b in range(configs.batch_size):
score, _ = compare_ssim(pred_frm[b], real_frm[b], full=True, multichannel=True)
ssim[i] += score
# save prediction examples
if batch_id <= configs.num_save_samples:
path = os.path.join(res_path, str(batch_id))
os.mkdir(path)
for i in range(configs.total_length):
name = 'gt' + str(i + 1) + '.png'
file_name = os.path.join(path, name)
img_gt = np.uint8(test_ims[0, i, :, :, :] * 255)
cv2.imwrite(file_name, img_gt)
for i in range(output_length):
name = 'pd' + str(i + 1 + configs.input_length) + '.png'
file_name = os.path.join(path, name)
img_pd = img_out[0, i, :, :, :]
img_pd = np.maximum(img_pd, 0)
img_pd = np.minimum(img_pd, 1)
img_pd = np.uint8(img_pd * 255)
cv2.imwrite(file_name, img_pd)
test_input_handle.next()
avg_mse = avg_mse / (batch_id * configs.batch_size)
print('mse per seq: ' + str(avg_mse))
for i in range(configs.total_length - configs.input_length):
print(img_mse[i] / (batch_id * configs.batch_size))
ssim = np.asarray(ssim, dtype=np.float32) / (configs.batch_size * batch_id)
print('ssim per frame: ' + str(np.mean(ssim)))
for i in range(configs.total_length - configs.input_length):
print(ssim[i])
psnr = np.asarray(psnr, dtype=np.float32) / batch_id
print('psnr per frame: ' + str(np.mean(psnr)))
for i in range(configs.total_length - configs.input_length):
print(psnr[i])
lp = np.asarray(lp, dtype=np.float32) / batch_id
print('lpips per frame: ' + str(np.mean(lp)))
for i in range(configs.total_length - configs.input_length):
print(lp[i])