-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplots.py
157 lines (121 loc) · 4.5 KB
/
plots.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
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn.functional as F
from torchvision.utils import make_grid
import models
def plot_metrics(args, metrics):
# Plot training metrics
f, ax = plt.subplots(1, 1)
f.set_size_inches(15, 4)
nepochs = metrics.nepochs()
# Loss
ax.set_title('recon loss')
ax.set_xlabel('epochs')
for subset in ['train', 'test']:
loss = metrics.recon_loss(subset)
ax.plot(np.linspace(0, nepochs, len(loss)), loss, label=subset)
ax.legend()
f.savefig(os.path.join(args.outdir, 'metrics.jpg'))
def plot_plca_recon(args, sample_img, model):
torch.set_grad_enabled(False)
model.eval(), model.cuda()
# Sample reconstruction
recon, priors, impulses, feats = model(sample_img.cuda())
# Assert reconstructions are probability distributions
recon_sum = recon.sum(dim=(1, 2, 3))
assert torch.allclose(recon_sum, torch.ones_like(recon_sum)), recon_sum
# Plot
f, ax = plt.subplots(2, 3)
f.set_size_inches(30, 20)
# Priors
nrow = max(int(model.nkern ** 0.5), 1)
ax[0, 0].set_title('priors')
grid_priors = make_grid(priors[0].unsqueeze(1).cpu(), nrow=nrow)
pcm = ax[0, 0].imshow(grid_priors[0])
f.colorbar(pcm, ax=ax[0, 0])
# Impulses
grid_impulses = make_grid(impulses[0].unsqueeze(1).cpu(), nrow=nrow, pad_value=1)
ax[0, 1].set_title('impulses')
pcm = ax[0, 1].imshow(grid_impulses[0])
f.colorbar(pcm, ax=ax[0, 1])
# Features
# Normalize each feature individually
max_feats, _ = feats.flatten(1).max(dim=1, keepdim=True)
feats = feats / max_feats.unflatten(1, (1, 1, 1))
grid_feats = make_grid(feats.cpu(), nrow=nrow, normalize=True)
ax[0, 2].set_title('normalized features')
ax[0, 2].imshow(grid_feats.permute(1, 2, 0))
# Original
ax[1, 0].set_title('normalized image')
img = make_grid(sample_img.cpu(), normalize=True)
ax[1, 0].imshow(img.permute(1, 2, 0))
# Reconstruction
ax[1, 1].set_title('normalized reconstruction')
img = make_grid(recon.cpu(), normalize=True)
ax[1, 1].imshow(img.permute(1, 2, 0))
ax[1, 2].remove()
f.savefig(os.path.join(args.outdir, 'recon.jpg'))
# Plot top components
f, ax = plt.subplots(1, 6)
f.set_size_inches(24, 4)
f.suptitle('top 6 components')
# Sort by priors
top_kerns = torch.argsort(priors, dim=1, descending=True)
for i in range(6):
idx = top_kerns[0, i]
component = F.conv_transpose2d(impulses[:, idx:idx + 1], feats[idx:idx + 1])
component.clamp_(min=0)
img = make_grid(component.cpu(), normalize=True)
ax[i].imshow(img.permute(1, 2, 0))
f.savefig(os.path.join(args.outdir, 'comp.jpg'))
def plot_ae_recon(args, sample_img, model):
torch.set_grad_enabled(False)
model.eval()
# Sample reconstruction
recon = model(sample_img.cuda())
# Plot
f, ax = plt.subplots(1, 2)
f.set_size_inches(12, 5)
# Original
ax[0].set_title('normalized image')
img = make_grid(sample_img.cpu(), normalize=True)
ax[0].imshow(img.permute(1, 2, 0))
# Reconstruction
ax[1].set_title('normalized reconstruction')
img = make_grid(recon.cpu(), normalize=True)
ax[1].imshow(img.permute(1, 2, 0))
def plot_al_recon(args, sample_img, model):
torch.set_grad_enabled(False)
model.cuda(), model.eval()
# Sample reconstruction
recon, layer_logits = model(sample_img.cuda())
n = len(layer_logits)
# Plot
f, ax = plt.subplots(2, 1 + n)
ax[1, 0].remove()
f.set_size_inches(4 * n, 8)
# Original
ax[0, 0].set_title('image')
img = make_grid(sample_img.cpu())
ax[0, 0].imshow(img.permute(1, 2, 0))
# Layers
recon_layer_logits = torch.zeros_like(recon)
for i in range(n):
ax[0, i + 1].set_title(f'layer {i + 1}')
img = make_grid(torch.sigmoid(layer_logits[i]).cpu())
ax[0, i + 1].imshow(img.permute(1, 2, 0))
ax[1, i + 1].set_title(f'layer {i + 1} reconstruction')
recon_layer_logits += layer_logits[i]
img = make_grid(torch.sigmoid(recon_layer_logits).cpu())
ax[1, i + 1].imshow(img.permute(1, 2, 0))
def plot_recon(args, imgs, model):
if isinstance(model, models.plca.PLCA):
plot_plca_recon(args, imgs, model)
elif isinstance(model, models.auto.AutoEncoder):
plot_ae_recon(args, imgs, model)
elif isinstance(model, models.auto.AutoLayer):
plot_al_recon(args, imgs, model)
else:
raise Exception(f'unknown model {model}')