-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
executable file
·184 lines (162 loc) · 5.79 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
import torch
import socket
import argparse
import json
import glob
import os
import shutil
import pdb
import numpy as np
import scipy.misc
import matplotlib.pyplot as plt
import functools
from torchvision import datasets, transforms
from torch.autograd import Variable
from data.moving_mnist import MovingMNIST
from data.kth import KTH
from data import suncg
######## Refer to the implementation given at 'https://github.com/edenton/drnet-py' for indepth understanding ###########
hostname = socket.gethostname()
def load_dataset(opt):
if opt.dataset == 'mnist':
train_data = MovingMNIST(
train=True,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width,
num_digits=2)
test_data = MovingMNIST(
train=False,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width,
num_digits=2)
elif opt.dataset == 'suncg':
train_data = suncg.SUNCG(
train=True,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width)
test_data = suncg.SUNCG(
train=False,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width)
elif opt.dataset == 'kth':
train_data = KTH(
train=True,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width,
data_type=opt.data_type)
test_data = KTH(
train=False,
data_root=opt.data_root,
seq_len=opt.max_step,
image_size=opt.image_width,
data_type=opt.data_type)
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):
if opt.dataset == 'mnist':
sequence.transpose_(0, 1)
if opt.channels > 1:
sequence.transpose_(3, 4).transpose_(2, 3)
else:
sequence.unsqueeze_(2)
elif opt.dataset == 'suncg' or opt.dataset == 'suncg_dual' or opt.dataset == 'kth':
sequence.transpose_(0, 1)
sequence.transpose_(3, 4).transpose_(2, 3)
else:
sequence.transpose_(0, 1)
return sequence_input(sequence, dtype)
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 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.max(),
channel_axis=0)
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 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)
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)