-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
312 lines (271 loc) · 10.1 KB
/
utils.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import math
import torch
import socket
import argparse
import os
import numpy as np
from sklearn.manifold import TSNE
import scipy.misc
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
import functools
from skimage.measure import compare_psnr as psnr_metric
from skimage.measure import compare_ssim as ssim_metric
from scipy import signal
from scipy import ndimage
from PIL import Image, ImageDraw
from torchvision import datasets, transforms
from torch.autograd import Variable
import imageio
hostname = socket.gethostname()
def load_dataset(opt):
if opt.dataset == 'smmnist':
from data.moving_mnist import MovingMNIST
train_data = MovingMNIST(
train=True,
data_root=opt.data_root,
seq_len=opt.n_past+opt.n_future,
image_size=opt.image_width,
deterministic=False,
num_digits=opt.num_digits)
test_data = MovingMNIST(
train=False,
data_root=opt.data_root,
seq_len=opt.n_eval,
image_size=opt.image_width,
deterministic=False,
num_digits=opt.num_digits)
elif opt.dataset == 'bair':
from data.bair import RobotPush
train_data = RobotPush(
data_root=opt.data_root,
train=True,
seq_len=opt.n_past+opt.n_future,
image_size=opt.image_width)
test_data = RobotPush(
data_root=opt.data_root,
train=False,
seq_len=opt.n_eval,
image_size=opt.image_width)
elif opt.dataset == 'kth':
from data.kth import KTH
train_data = KTH(
train=True,
data_root=opt.data_root,
seq_len=opt.n_past+opt.n_future,
image_size=opt.image_width)
test_data = KTH(
train=False,
data_root=opt.data_root,
seq_len=opt.n_eval,
image_size=opt.image_width)
elif opt.dataset == 'ucf':
from data.ucf import UCF
train_data = UCF(
train=True,
data_root=opt.data_root,
seq_len=opt.n_past+opt.n_future,
image_size=opt.image_width)
test_data = UCF(
train=False,
data_root=opt.data_root,
seq_len=opt.n_eval,
image_size=opt.image_width)
return train_data, test_data
def sequence_input(seq, dtype):
return [Variable(x.type(dtype)) for x in seq]
def normalize_data(opt, dtype, sequence):
sequence, targets = sequence # , targets
# sequence = sequence
if opt.dataset == 'smmnist' or opt.dataset == 'ucf' or opt.dataset == 'kth' or opt.dataset == 'bair' :
sequence.transpose_(0, 1)
sequence.transpose_(3, 4).transpose_(2, 3)
else:
sequence.transpose_(0, 1)
return sequence_input(sequence, dtype), targets.cuda()
def is_sequence(arg):
return (not hasattr(arg, "strip") and
not type(arg) is np.ndarray and
not hasattr(arg, "dot") and
(hasattr(arg, "__getitem__") or
hasattr(arg, "__iter__")))
def image_tensor(inputs, padding=1):
# assert is_sequence(inputs)
assert len(inputs) > 0
# print(inputs)
# if this is a list of lists, unpack them all and grid them up
if is_sequence(inputs[0]) or (hasattr(inputs, "dim") and inputs.dim() > 4):
images = [image_tensor(x) for x in inputs]
if images[0].dim() == 3:
c_dim = images[0].size(0)
x_dim = images[0].size(1)
y_dim = images[0].size(2)
else:
c_dim = 1
x_dim = images[0].size(0)
y_dim = images[0].size(1)
result = torch.ones(c_dim,
x_dim * len(images) + padding * (len(images)-1),
y_dim)
for i, image in enumerate(images):
result[:, i * x_dim + i * padding :
(i+1) * x_dim + i * padding, :].copy_(image)
return result
# if this is just a list, make a stacked image
else:
images = [x.data if isinstance(x, torch.autograd.Variable) else x
for x in inputs]
# print(images)
if images[0].dim() == 3:
c_dim = images[0].size(0)
x_dim = images[0].size(1)
y_dim = images[0].size(2)
else:
c_dim = 1
x_dim = images[0].size(0)
y_dim = images[0].size(1)
result = torch.ones(c_dim,
x_dim,
y_dim * len(images) + padding * (len(images)-1))
for i, image in enumerate(images):
result[:, :, i * y_dim + i * padding :
(i+1) * y_dim + i * padding].copy_(image)
return result
def save_np_img(fname, x):
if x.shape[0] == 1:
x = np.tile(x, (3, 1, 1))
img = scipy.misc.toimage(x,
high=255*x.max(),
channel_axis=0)
img.save(fname)
def make_image(tensor):
tensor = tensor.cpu().clamp(0, 1)
if tensor.size(0) == 1:
tensor = tensor.expand(3, tensor.size(1), tensor.size(2))
# pdb.set_trace()
return scipy.misc.toimage(tensor.numpy(),high=255*tensor.numpy().max(),channel_axis=0)
def draw_text_tensor(tensor, text):
np_x = tensor.transpose(0, 1).transpose(1, 2).data.cpu().numpy()
pil = Image.fromarray(np.uint8(np_x*255))
draw = ImageDraw.Draw(pil)
draw.text((4, 64), text, (0,0,0))
img = np.asarray(pil)
return Variable(torch.Tensor(img / 255.)).transpose(1, 2).transpose(0, 1)
def save_gif(filename, inputs, duration=0.25):
images = []
for tensor in inputs:
img = image_tensor(tensor, padding=0)
img = img.cpu()
img = img.transpose(0,1).transpose(1,2).clamp(0,1)
images.append(img.numpy())
imageio.mimsave(filename, images, duration=duration)
def save_gif_with_text(filename, inputs, text, duration=0.25):
images = []
for tensor, text in zip(inputs, text):
img = image_tensor([draw_text_tensor(ti, texti) for ti, texti in zip(tensor, text)], padding=0)
img = img.cpu()
img = img.transpose(0,1).transpose(1,2).clamp(0,1).numpy()
images.append(img)
imageio.mimsave(filename, images, duration=duration)
def save_image(filename, tensor):
img = make_image(tensor)
img.save(filename)
def save_tensors_image(filename, inputs, padding=1):
images = image_tensor(inputs, padding)
return save_image(filename, images)
def prod(l):
return functools.reduce(lambda x, y: x * y, l)
def batch_flatten(x):
return x.resize(x.size(0), prod(x.size()[1:]))
def clear_progressbar():
# moves up 3 lines
print("\033[2A")
# deletes the whole line, regardless of character position
print("\033[2K")
# moves up two lines again
print("\033[2A")
def mse_metric(x1, x2):
err = np.sum((x1 - x2) ** 2)
err /= float(x1.shape[0] * x1.shape[1] * x1.shape[2])
return err
def eval_seq(gt, pred):
T = len(gt)
bs = gt[0].shape[0]
ssim = np.zeros((bs, T))
psnr = np.zeros((bs, T))
mse = np.zeros((bs, T))
for i in range(bs):
for t in range(T):
for c in range(gt[t][i].shape[0]):
ssim[i, t] += ssim_metric(gt[t][i][c], pred[t][i][c])
psnr[i, t] += psnr_metric(gt[t][i][c], pred[t][i][c])
ssim[i, t] /= gt[t][i].shape[0]
psnr[i, t] /= gt[t][i].shape[0]
return mse, ssim, psnr
# ssim function used in Babaeizadeh et al. (2017), Fin et al. (2016), etc.
def finn_eval_seq(gt, pred):
T = len(gt)
bs = gt[0].shape[0]
ssim = np.zeros((bs, T))
psnr = np.zeros((bs, T))
mse = np.zeros((bs, T))
for i in range(bs):
for t in range(T):
for c in range(gt[t][i].shape[0]):
res = finn_ssim(gt[t][i][c], pred[t][i][c]).mean()
if math.isnan(res):
ssim[i, t] += -1
else:
ssim[i, t] += res
psnr[i, t] += finn_psnr(gt[t][i][c], pred[t][i][c])
ssim[i, t] /= gt[t][i].shape[0]
psnr[i, t] /= gt[t][i].shape[0]
mse[i, t] = mse_metric(gt[t][i], pred[t][i])
return mse, ssim, psnr
def finn_psnr(x, y):
mse = ((x - y)**2).mean()
return 10*np.log(1/mse)/np.log(10)
def gaussian2(size, sigma):
A = 1/(2.0*np.pi*sigma**2)
x, y = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
g = A*np.exp(-((x**2/(2.0*sigma**2))+(y**2/(2.0*sigma**2))))
return g
def fspecial_gauss(size, sigma):
x, y = np.mgrid[-size//2 + 1:size//2 + 1, -size//2 + 1:size//2 + 1]
g = np.exp(-((x**2 + y**2)/(2.0*sigma**2)))
return g/g.sum()
def finn_ssim(img1, img2, cs_map=False):
img1 = img1.cpu().numpy().astype(np.float64)
img2 = img2.cpu().numpy().astype(np.float64)
size = 11
sigma = 1.5
window = fspecial_gauss(size, sigma)
K1 = 0.01
K2 = 0.03
L = 1 #bitdepth of image
C1 = (K1*L)**2
C2 = (K2*L)**2
mu1 = signal.fftconvolve(img1, window, mode='valid')
mu2 = signal.fftconvolve(img2, window, mode='valid')
mu1_sq = mu1*mu1
mu2_sq = mu2*mu2
mu1_mu2 = mu1*mu2
sigma1_sq = signal.fftconvolve(img1*img1, window, mode='valid') - mu1_sq
sigma2_sq = signal.fftconvolve(img2*img2, window, mode='valid') - mu2_sq
sigma12 = signal.fftconvolve(img1*img2, window, mode='valid') - mu1_mu2
if cs_map:
return (((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2)),
(2.0*sigma12 + C2)/(sigma1_sq + sigma2_sq + C2))
else:
return ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*
(sigma1_sq + sigma2_sq + C2))
def init_weights(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1 or classname.find('Linear') != -1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)