-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
117 lines (94 loc) · 3.43 KB
/
Copy pathutils.py
File metadata and controls
117 lines (94 loc) · 3.43 KB
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
"""
Utility functions
"""
import os
import numpy as np
import matplotlib.pyplot as plt
def config(attr):
"""
Retrieves the queried attribute value from the config file. Loads the
config file on first call.
"""
if not hasattr(config, "config"):
with open("config.json") as f:
config.config = eval(f.read())
node = config.config
for part in attr.split("."):
node = node[part]
return node
def denormalize_image(image):
"""Rescale the image's color space from (min, max) to (0, 1)"""
ptp = np.max(image, axis=(0, 1)) - np.min(image, axis=(0, 1))
return (image - np.min(image, axis=(0, 1))) / ptp
def hold_training_plot():
"""
Keep the program alive to display the training plot
"""
plt.ioff()
plt.show()
def log_training(epoch, stats):
"""Print the train, validation, test accuracy/loss/auroc.
Each epoch in `stats` should have order
[val_acc, val_loss, val_auc, train_acc, ...]
Test accuracy is optional and will only be logged if stats is length 9.
"""
splits = ["Validation", "Train", "Test"]
metrics = ["Accuracy", "Loss", "AUROC"]
print("Epoch {}".format(epoch))
for j, split in enumerate(splits):
for i, metric in enumerate(metrics):
idx = len(metrics) * j + i
if idx >= len(stats[-1]):
continue
print(f"\t{split} {metric}:{round(stats[-1][idx],4)}")
def make_training_plot(name="CNN Training"):
"""Set up an interactive matplotlib graph to log metrics during training."""
plt.ion()
fig, axes = plt.subplots(1, 3, figsize=(20, 5))
plt.suptitle(name)
axes[0].set_xlabel("Epoch")
axes[0].set_ylabel("Accuracy")
axes[1].set_xlabel("Epoch")
axes[1].set_ylabel("Loss")
axes[2].set_xlabel("Epoch")
axes[2].set_ylabel("AUROC")
return axes
def update_training_plot(axes, epoch, stats):
"""Update the training plot with a new data point for loss and accuracy."""
splits = ["Validation", "Train", "Test"]
metrics = ["Accuracy", "Loss", "AUROC"]
colors = ["r", "b", "g"]
for i, metric in enumerate(metrics):
for j, split in enumerate(splits):
idx = len(metrics) * j + i
if idx >= len(stats[-1]):
continue
# __import__('pdb').set_trace()
axes[i].plot(
range(epoch - len(stats) + 1, epoch + 1),
[stat[idx] for stat in stats],
linestyle="--",
marker="o",
color=colors[j],
)
axes[i].legend(splits[: int(len(stats[-1]) / len(metrics))])
plt.pause(0.00001)
def save_cnn_training_plot():
"""Save the training plot to a file."""
plt.savefig("cnn_training_plot.png", dpi=200)
def save_tl_training_plot(num_layers):
"""Save the transfer learning training plot to a file."""
if num_layers == 0:
plt.savefig("TL_0_layers.png", dpi=200)
elif num_layers == 1:
plt.savefig("TL_1_layers.png", dpi=200)
elif num_layers == 2:
plt.savefig("TL_2_layers.png", dpi=200)
elif num_layers == 3:
plt.savefig("TL_3_layers.png", dpi=200)
def save_source_training_plot():
"""Save the source learning training plot to a file."""
plt.savefig("source_training_plot.png", dpi=200)
def save_challenge_training_plot():
"""Save the challenge learning training plot to a file."""
plt.savefig("challenge_training_plot.png", dpi=200)