-
Notifications
You must be signed in to change notification settings - Fork 165
/
helper_plotting.py
138 lines (105 loc) · 3.92 KB
/
helper_plotting.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
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
def plot_training_loss(
minibatch_loss_list,
num_epochs,
iter_per_epoch,
results_dir=None,
averaging_iterations=100,
):
plt.figure()
ax1 = plt.subplot(1, 1, 1)
ax1.plot(
range(len(minibatch_loss_list)), (minibatch_loss_list), label="Minibatch Loss"
)
if len(minibatch_loss_list) > 1000:
ax1.set_ylim([0, np.max(minibatch_loss_list[1000:]) * 1.5])
ax1.set_xlabel("Iterations")
ax1.set_ylabel("Loss")
ax1.plot(
np.convolve(
minibatch_loss_list,
np.ones(
averaging_iterations,
)
/ averaging_iterations,
mode="valid",
),
label="Running Average",
)
ax1.legend()
###################
# Set scond x-axis
ax2 = ax1.twiny()
newlabel = list(range(num_epochs + 1))
newpos = [e * iter_per_epoch for e in newlabel]
ax2.set_xticks(newpos[::10])
ax2.set_xticklabels(newlabel[::10])
ax2.xaxis.set_ticks_position("bottom")
ax2.xaxis.set_label_position("bottom")
ax2.spines["bottom"].set_position(("outward", 45))
ax2.set_xlabel("Epochs")
ax2.set_xlim(ax1.get_xlim())
###################
plt.tight_layout()
if results_dir is not None:
image_path = os.path.join(results_dir, "plot_training_loss.pdf")
plt.savefig(image_path)
def plot_accuracy(train_acc_list, valid_acc_list, results_dir=None):
num_epochs = len(train_acc_list)
plt.plot(np.arange(1, num_epochs + 1), train_acc_list, label="Training")
plt.plot(np.arange(1, num_epochs + 1), valid_acc_list, label="Validation")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()
plt.tight_layout()
if results_dir is not None:
image_path = os.path.join(results_dir, "plot_acc_training_validation.pdf")
plt.savefig(image_path)
def show_examples(model, data_loader, unnormalizer=None, class_dict=None):
fail_features, fail_targets, fail_predicted = [], [], []
for batch_idx, (features, targets) in enumerate(data_loader):
with torch.no_grad():
logits = model(features)
predictions = torch.argmax(logits, dim=1)
mask = targets != predictions
fail_features.extend(features[mask])
fail_targets.extend(targets[mask])
fail_predicted.extend(predictions[mask])
if len(fail_targets) > 15:
break
fail_features = torch.cat(fail_features)
fail_targets = torch.tensor(fail_targets)
fail_predicted = torch.tensor(fail_predicted)
fig, axes = plt.subplots(nrows=3, ncols=5, sharex=True, sharey=True)
if unnormalizer is not None:
for idx in range(fail_features.shape[0]):
features[idx] = unnormalizer(fail_features[idx])
if fail_features.ndim == 4:
nhwc_img = np.transpose(fail_features, axes=(0, 2, 3, 1))
nhw_img = np.squeeze(nhwc_img.numpy(), axis=3)
for idx, ax in enumerate(axes.ravel()):
ax.imshow(nhw_img[idx], cmap="binary")
if class_dict is not None:
ax.title.set_text(
f"P: {class_dict[fail_predicted[idx].item()]}"
f"\nT: {class_dict[fail_targets[idx].item()]}"
)
else:
ax.title.set_text(f"P: {fail_predicted[idx]} | T: {fail_targets[idx]}")
ax.axison = False
else:
for idx, ax in enumerate(axes.ravel()):
ax.imshow(fail_features[idx], cmap="binary")
if class_dict is not None:
ax.title.set_text(
f"P: {class_dict[fail_predicted[idx].item()]}"
f"\nT: {class_dict[fail_targets[idx].item()]}"
)
else:
ax.title.set_text(f"P: {fail_predicted[idx]} | T: {targets[idx]}")
ax.axison = False
plt.tight_layout()
plt.show()