-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathplot.py
More file actions
137 lines (111 loc) · 4.74 KB
/
plot.py
File metadata and controls
137 lines (111 loc) · 4.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
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
"""
Plot training/validation curves for multiple models.
"""
from __future__ import division
from __future__ import print_function
import argparse
import matplotlib
import numpy as np
import os
matplotlib.use('Agg') # This must be called before importing pyplot
import matplotlib.pyplot as plt
COLORS_RGB = [
(228, 26, 28), (55, 126, 184), (77, 175, 74),
(152, 78, 163), (255, 127, 0), (255, 255, 51),
(166, 86, 40), (247, 129, 191), (153, 153, 153)
]
# Scale the RGB values to the [0, 1] range, which is the format
# matplotlib accepts.
colors = [(r / 255, g / 255, b / 255) for r, g, b in COLORS_RGB]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--dirs', nargs='+', required=True,
help='Directories where the model and costs are saved')
parser.add_argument('-s', '--save_file', type=str, required=True,
help='Filename of the output plot')
return parser.parse_args()
def graph(dirs, save_file, average_window=100):
""" Plot the training and validation costs over iterations
Params:
dirs (list(str)): Directories where the model and costs are saved
save_file (str): Filename of the output plot
average_window (int): Window size for smoothening the graphs
"""
fig, ax = plt.subplots()
ax.set_xlabel('Iters')
ax.set_ylabel('Loss')
average_filter = np.ones(average_window) / float(average_window)
for i, d in enumerate(dirs):
name = os.path.basename(os.path.abspath(d))
color = colors[i % len(colors)]
costs = np.load(os.path.join(d, 'costs.npz'))
train_costs = costs['train']
valid_costs = costs['validation'].tolist()
iters = train_costs.shape[0]
valid_range = [500 * (i + 1) for i in range(iters // 500)]
if len(valid_range) != len(valid_costs):
valid_range.append(iters)
if train_costs.ndim == 1:
train_costs = np.convolve(train_costs, average_filter,
mode='valid')
ax.plot(train_costs, color=color, label=name + '_train', lw=1.5)
ax.plot(valid_range, valid_costs[:len(valid_range)],
'-o', color=color, label=name + '_valid')
ax.grid(True)
ax.legend(loc='best')
plt.savefig(save_file)
def graph2(dirs, save_file, average_window=100):
""" Plot the training and not equla steps validation costs over iterations
Params:
dirs (list(str)): Directories where the model and costs are saved
save_file (str): Filename of the output plot
average_window (int): Window size for smoothening the graphs
"""
fig_size = plt.rcParams["figure.figsize"]
print(fig_size)
fig_size[0] = 15
fig_size[1] = 15
#plt.rcParams["figure.figsize"] = fig_size
p1 = plt.subplot(211)
p1.set_xlabel('Iters')
p1.set_ylabel('Loss')
p2 = plt.subplot(212)
p2.set_xlabel('Iters')
p2.set_ylabel('cer&wer')
average_filter = np.ones(average_window) / float(average_window)
for i, d in enumerate(dirs):
name = os.path.basename(os.path.abspath(d))
color = colors[i % len(colors)]
color1 = colors[i+1 % len(colors)]
costs = np.load(os.path.join(d, 'costs.npz'))
train_costs = costs['train']
if train_costs.ndim == 1:
train_costs = np.convolve(train_costs, average_filter, mode='valid')
p1.plot(train_costs, color=color, label=name + '_train', lw=1.5)
if 'validation_with_step' in costs.files:
valid_costs_with_step = costs['validation_with_step']
# validate cost is [step,cost] per sample
valid_range = valid_costs_with_step[:,0].tolist()
valid_costs = valid_costs_with_step[:,1].tolist()
p1.plot(valid_range, valid_costs, color=color1, label=name + '_valid')
if 'cer_with_step' in costs.files:
cer_costs_with_step = costs['cer_with_step']
# validate cost is [step,cost] per sample
cer_range = cer_costs_with_step[:,0].tolist()
cer_costs = cer_costs_with_step[:,1].tolist()
p2.plot(cer_range, cer_costs, color=color, label=name + '_cer')
if 'wer_with_step' in costs.files:
wer_costs_with_step = costs['wer_with_step']
# validate cost is [step,cost] per sample
wer_range = wer_costs_with_step[:,0].tolist()
wer_costs = wer_costs_with_step[:,1].tolist()
p2.plot(wer_range, wer_costs, color=color1, label=name + '_wer')
p1.grid(True)
p1.legend(loc='best')
p2.grid(True)
p2.legend(loc='best')
plt.savefig(save_file)
if __name__ == '__main__':
args = parse_args()
#graph(args.dirs, args.save_file)
graph2(args.dirs, args.save_file+'.png', 1)