-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
195 lines (166 loc) · 6.75 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
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
import argparse
import numpy as np
import torch
from tqdm import tqdm
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from parse_config import ConfigParser
from torch.nn import functional as F
import torchvision.utils as vutils
from torchvision import transforms
from torch.autograd import Variable
import os
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
#Fixes PosixPath Error
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
def latent_traversal(model, samples, n_changes=5, val_range=(-1, 1)):
""" This function perform latent traversal on a VAE latent space
model_path: str
The absolute path of the model to load
fname: str
The filename to use for saving the latent traversal
samples:
The list of data examples to provide as input of the model
n_changes: int
The number of changes to perform on one latent dimension
val_range: tuple
The range of values that can be set for one latent dimension
"""
# TODO: change the next two lines to retrieve the output of your encoder with pytorch
# m = tf.keras.models.load_model(model_path)
z_base = model.encode(samples)[-1]
z_base = z_base.cpu()
# END TODO
r, c = n_changes, z_base.shape[1]
vals = np.linspace(*val_range, r)
shape = samples[0].shape
for j, z in enumerate(z_base):
imgs = np.empty([r * c, *shape])
for i in range(c):
z_iter = np.tile(z, [r, 1])
z_iter[:, i] = vals
z_iter = torch.from_numpy(z_iter)
z_iter = z_iter.to(device)
imgs[r * i:(r * i) + r] = F.sigmoid(model.decode(z_iter)[-1])
plot_traversal(imgs, r, c, shape[-1] == 1, show=True)
# save_figure(fname, tight=False)
def plot_traversal(imgs, r, c, greyscale, show=False):
fig = plt.figure(figsize=(20., 20.))
grid = ImageGrid(fig, 111, nrows_ncols=(r, c), axes_pad=0, direction="column")
for i, (ax, im) in enumerate(zip(grid, imgs)):
ax.set_axis_off()
if i % r == 0:
ax.set_title("z{}".format(i // r), fontdict={'fontsize': 25})
if greyscale is True:
ax.imshow(im, cmap="gray")
else:
ax.imshow(im)
fig.subplots_adjust(wspace=0, hspace=0)
if show is True:
plt.show()
plt.savefig('traversal.png')
def interpolate(autoencoder, x_1, x_2, n=12):
z_1 = autoencoder.encode(x_1)[2]
z_2 = autoencoder.encode(x_2)[2]
z = torch.stack([z_1 + (z_2 - z_1)*t for t in np.linspace(0, 1, n)])
interpolate_list = autoencoder.decode(z)
interpolate_list = interpolate_list.to('cpu').detach()
print(len(interpolate_list))
plt.figure(figsize=(64, 64))
for i in range(len(interpolate_list)):
ax = plt.subplot(1, len(interpolate_list), i+1)
plt.imshow(interpolate_list[i].permute(1, 2, 0).numpy())
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.savefig('linear_interpolation.png')
def main(config):
logger = config.get_logger('test')
# setup data_loader instances
data_loader = getattr(module_data, config['data_loader']['type'])(
config['data_loader']['args']['data_dir'],
batch_size=36,
shuffle=False,
validation_split=0.0,
# training=False,
num_workers=2
)
# build model architecture
model = config.init_obj('arch', module_arch)
logger.info(model)
# get function handles of loss and metrics
loss_fn = getattr(module_loss, config['loss'])
# metric_fns = [getattr(module_metric, met) for met in config['metrics']]
logger.info('Loading checkpoint: {} ...'.format(config.resume))
# checkpoint = torch.load(config.resume)
# loading on CPU-only machine
checkpoint = torch.load(config.resume, map_location=torch.device('cpu'))
state_dict = checkpoint['state_dict']
if config['n_gpu'] > 1:
model = torch.nn.DataParallel(model)
model.load_state_dict(state_dict)
# prepare model for testing
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
model.eval()
total_loss = 0.0
# total_metrics = torch.zeros(len(metric_fns))
with torch.no_grad():
for i, (data, target) in enumerate(tqdm(data_loader)):
data, target = data.to(device), target.to(device)
output, mu, logvar = model(data)
# computing loss, metrics on test set
loss = loss_fn(output, data, mu, logvar)
batch_size = data.shape[0]
total_loss += loss.item() * batch_size
# for i, metric in enumerate(metric_fns):
# total_metrics[i] += metric(output, target) * batch_size
# Reconstructing and generating images for a mini-batch
test_input, test_label = next(iter(data_loader))
test_input = test_input.to(device)
test_label = test_label.to(device)
recons = model.generate(test_input, labels=test_label)
vutils.save_image(recons.data,
os.path.join(
"Reconstructions",
f"recons_{logger.name}_epoch_{config['trainer']['epochs']}.png"),
normalize=True,
nrow=6)
try:
samples = model.sample(36,
device,
labels=test_label)
vutils.save_image(samples.cpu().data,
os.path.join(
"Samples",
f"{logger.name}.png"),
normalize=True,
nrow=6)
except Warning:
pass
# linear interpolation two chosen images
x_1 = test_input[1].to(device)
x_1 = torch.unsqueeze(x_1, dim=0)
x_2 = test_input[2].to(device)
x_2 = torch.unsqueeze(x_2, dim=0)
interpolate(model, x_1, x_2, n=5)
n_samples = len(data_loader.sampler)
log = {'loss': total_loss / n_samples}
# log.update({
# met.__name__: total_metrics[i].item() / n_samples for i, met in enumerate(metric_fns)
# })
logger.info(log)
if __name__ == '__main__':
args = argparse.ArgumentParser(description='PyTorch Template')
args.add_argument('-c', '--config', default=None, type=str,
help='config file path (default: None)')
args.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
args.add_argument('-d', '--device', default=None, type=str,
help='indices of GPUs to enable (default: all)')
config = ConfigParser.from_args(args)
main(config)