-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
86 lines (66 loc) · 2.74 KB
/
utils.py
File metadata and controls
86 lines (66 loc) · 2.74 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
import numpy as np
from matplotlib import pyplot as plt
from keras.callbacks import Callback
class LossHistory(Callback):
def __init__(self, metrics, *args, **kwargs):
super().__init__(*args, **kwargs)
self.metrics = {}
for metric in metrics:
self.metrics[metric] = ([], [])
def on_epoch_end(self, epoch, logs=None):
for key, metric in self.metrics.items():
self.metrics[key][0].append( logs.get(key) )
self.metrics[key][1].append( logs.get('val_' + key) )
def plotHistory(history):
metrics = [metric for metric in history.keys() if not metric[:3] == "val"]
num_metrics = len(metrics)
plt.figure(figsize=(20, 4))
for idx, metric in enumerate(metrics):
plt.subplot(1, num_metrics, idx+1)
plt.plot(history[metric], '#EF6C00', label='train')
plt.plot(history[f"val_{metric}"], '#0077BB', label='eval')
plt.title(metric)
plt.xlabel('epoch')
plt.legend()
def visualization_train(dataset, netout):
images, labels = dataset
reconstructions, predictions = netout
plt.figure(figsize=(20, 10))
n_images = len(images)
for i in range(n_images):
ax = plt.subplot(2, n_images, i + 1)
plt.imshow(images[i], cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_title(labels[i])
ax = plt.subplot(2, n_images, n_images + i + 1)
plt.imshow(reconstructions[i], cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_title(predictions[i])
def visualization_data(images, labels, predictions):
plt.figure(figsize=(20, 10))
n_images = len(images)
for i in range(n_images):
ax = plt.subplot(1, n_images, i + 1)
plt.imshow(images[i], cmap="gray")
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
ax.set_title(f'{predictions[i]} ({labels[i]})')
def rotation_accuracy(model, dataGenerator, m_test, batch_size=32, n_points=10):
rotations = np.linspace(0, 180, n_points)
data_augmentation = {'rotation_range': 0}
test_accuracy = []
for deg in rotations:
# Select data
data_augmentation['rotation_range'] = deg
testGenerator = dataGenerator('test', batch_size=batch_size, reshape=False, **data_augmentation)
# Test accuracy
test_acc = model.evaluate_generator(testGenerator, steps=m_test//batch_size)[1]
print(f'Test acc [{deg}°]:\t{round(test_acc, 3)}')
test_accuracy.append(test_acc)
# Plot
plt.plot(rotations, test_accuracy)
plt.title('rotation accuracy')
plt.xlabel('rotation (deg)')
plt.ylabel('test accuracy')