-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
234 lines (169 loc) · 6.96 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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
import torchvision
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
from torchvision import datasets, transforms
def get_events(path=None, modelname=None, key=None):
"""
Return tensorboard events for a key as a pandas dataframe.
Args:
path: Path with tensorboard log directory.
modelname: String to identify the model.
key: Scalar value to retrieve from the logs.
"""
event_acc = EventAccumulator(path)
event_acc.Reload()
tempdf = pd.DataFrame(event_acc.Scalars(key))
tempdf['type'] = modelname
return tempdf
def plot_metric(paths, names, key=None, fontsize=14, dot=False, logscale=False):
"""
Plot a metric from tensorboard logs.
Args:
paths: List of tensorboard log directories.
names: List with model names. Must be in the same order as `path`.
fontsize: Font size for plotting.
dot: If True, will dot discrete values on epoch-based plots.
logscale: If True, the y-axis will be plotted on a log-scale.
"""
with plt.style.context({'font.size': fontsize}):
# Get the step events for the metrics as a data frame
dfs = []
for p, n in zip(paths, names):
tmpdf = get_events(p, n, key)
dfs.append(tmpdf)
# Plot
fig, ax = plt.subplots(1, 1, figsize=(7,5))
for m, n in zip(dfs, names):
ax.plot(m['step'], m['value'], label=n)
if dot:
ax.scatter(m['step'], m['value'])
if logscale:
plt.yscale('log')
plt.xlabel('Step')
plt.ylabel(key)
plt.legend()
return fig, ax
def convert_image_np(inp):
"""
Convert a Tensor to numpy image.
This function is from Pytorch's spatial transformer tutorial.
https://pytorch.org/tutorials/intermediate/spatial_transformer_tutorial.html
Args:
inp: Torch tensor.
"""
inp = inp.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
return inp
def compare_stns(model1=None, model2=None, model1name=None, model2name=None,
dataloader=None, device=None, figsize=(15,12)):
"""
Evaluate one batch of MNIST data with two different models,
and plot an image comparison grid of the original data vs. the STN
transformation of two models. Models must have a `stn` forward function.
Args:
model1: First model (nn.Module).
model2: Second model (nn.Module).
model1name: String to identify the first model.
model2name: String to identify the second model.
dataloader: Dataloader from which the first batch of data will
be evaluated.
device: Device on which to run the computations.
figsize: Tuple (w, h) for the plot size.
"""
with torch.no_grad():
data = next(iter(dataloader))[0].to(device)
# Original data
original_grid = convert_image_np(torchvision.utils.make_grid(
data.cpu()))
# Transformed input for the first model
trans_baseline_stn_tensor = model1.stn(data).cpu()
baseline_stn_grid = convert_image_np(
torchvision.utils.make_grid(trans_baseline_stn_tensor))
# Transformed input for the second model
coordconv_stn_tensor = model2.stn(data).cpu()
coordconv_stn_grid = convert_image_np(
torchvision.utils.make_grid(coordconv_stn_tensor))
# Plot the results side-by-side
fig, ax = plt.subplots(1, 3, figsize=figsize)
ax[0].imshow(original_grid)
ax[0].set_title('Original input')
ax[1].imshow(baseline_stn_grid)
ax[1].set_title(model1name)
ax[2].imshow(coordconv_stn_grid)
ax[2].set_title(model2name)
def plot_wrong_preds(model1=None, model2=None, model1name=None, model2name=None,
dataloader=None, device=None, k=30, figsize=(15,12)):
"""
Examine the k most incorrectly predicted samples according to model2,
and plot the original data and STN transformations obtained by
model1 and model2. Models must have a `stn` forward function.
Args:
model1: First model (nn.Module).
model2: Second model (nn.Module).
model1name: String to identify the first model.
model2name: String to identify the second model.
dataloader: Dataloader with samples to evaluate.
device: Device on which to run the computations.
k: Number of top-k wrong predictions to plot.
figsize: Tuple (w, h) for the plot size.
"""
model1_logits = []
model2_logits = []
targets = []
# Evaluate all the samples from the dataloader
# and store logits and predictions
with torch.no_grad():
for i, batch in enumerate(dataloader):
data, target = batch
data = data.to(device)
# Forward pass
m1 = model1(data).cpu()
m2 = model2(data).cpu()
# Store logits
model1_logits.append(m1)
model2_logits.append(m2)
# Store predictions
targets.append(target.cpu())
model1_logits = np.vstack(model1_logits)
model2_logits = np.vstack(model2_logits)
targets = np.concatenate(targets)
# Find the top-k most incorrect predictions according to model2
x_logits = model2_logits
preds = x_logits.argmax(axis=1)
pred_df = pd.DataFrame.from_records(zip(preds, targets, preds==targets,
np.exp(x_logits[np.arange(
x_logits.shape[0]), preds])),
columns=['predictions', 'target',
'correct', 'pred_prob'])
incorrects = pred_df[~pred_df.correct]
incorrects = incorrects.sort_values('pred_prob', ascending=False)
worst_predictions = incorrects.index[0:k].values
# Get the tensors for the incorrect predictions
imgs = []
val_tmp = datasets.MNIST(root='.', train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))]))
for incorrect_idx in worst_predictions:
x, y = val_tmp[incorrect_idx]
imgs.append(x)
imgs = torch.cat(imgs).unsqueeze(1)
# Plot
original_grid = convert_image_np(torchvision.utils.make_grid(imgs))
model1_grid = convert_image_np(torchvision.utils.make_grid(
model1.stn(imgs.to(device)).cpu()))
model2_grid = convert_image_np(torchvision.utils.make_grid(
model2.stn(imgs.to(device)).cpu()))
fig, ax = plt.subplots(1, 3, figsize=figsize)
ax[0].imshow(original_grid)
ax[0].set_title('Original input')
ax[1].imshow(model1_grid)
ax[1].set_title(model1name)
ax[2].imshow(model2_grid)
ax[2].set_title(model2name)