-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
progressPlot.py
269 lines (247 loc) · 8.13 KB
/
progressPlot.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Plot a training log file
"""
import argparse
try:
import regex as re
except ImportError as e:
print("Please, pip install regex")
try:
import matplotlib.pyplot as plot
except ImportError as e:
print("Please, pip install matplotlib to plot train's progress")
raise e
def process(log):
values = {"Train": {}, "Test": {}, "Accuracy": {}, "LearningRate": {}}
epoch = 0
for line in log:
if line.startswith("Epoch:"):
words = line.split("\t")
match = re.match(
r"Epoch: \[ *(?P<epoch>\d+)\]\[ *(?P<step>\d+)/(?P<max_step>\d+)\]",
words[0],
)
epoch = int(match.group("epoch"))
step = int(match.group("step"))
train_max_step = int(match.group("max_step"))
x = float(epoch) + float(step) / float(train_max_step)
for i in range(1, len(words)):
try:
match = re.match(
r"(?P<name>\S+) +(?P<val>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?) +"
r"\( *(?P<avg>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\)",
words[i],
)
name = match.group("name")
val = float(match.group("val"))
avg = float(match.group("avg"))
values["Train"].setdefault(name, {}).setdefault("x", []).append(x)
values["Train"].setdefault(name, {}).setdefault("y", []).append(avg)
except:
pass
elif line.startswith("Test:"):
try:
words = line.split("\t")
match = re.match(
r"Test: \[ *(?P<step>\d+)/(?P<max_step>\d+)\]", words[0]
)
step = int(match.group("step"))
test_max_step = int(match.group("max_step"))
x = float(epoch) + float(step) / float(test_max_step)
for i in range(1, len(words)):
match = re.match(
r"(?P<name>\S+) +(?P<val>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?) +"
r"\( *(?P<avg>[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?)\)",
words[i],
)
name = match.group("name")
val = float(match.group("val"))
avg = float(match.group("avg"))
values["Test"].setdefault(name, {}).setdefault("x", []).append(x)
values["Test"].setdefault(name, {}).setdefault("y", []).append(avg)
except:
pass
elif line.startswith("LearningRate:"):
try:
words = line.split(" ")
epoch = int(words[1])
val = float(words[2])
values["LearningRate"].setdefault("x", []).append(epoch + 1)
values["LearningRate"].setdefault("y", []).append(val)
except:
pass
elif line.startswith(" * "):
try:
words = line.split(" ")
for i in range(2, len(words), 2):
name = words[i]
val = float(words[i + 1])
values["Accuracy"].setdefault(name, {}).setdefault("x", []).append(
epoch + 1
)
values["Accuracy"].setdefault(name, {}).setdefault("y", []).append(
val
)
except:
pass
# else:
# print(line)
return values
def draw(val, args):
fig, ax1 = plot.subplots()
ax1.set_xlabel("epochs")
plot.title("C4C: " + args.log.name)
ax2 = ax1.twinx()
colors = [
"#000000",
"#ff0000",
"#00ff00",
"#0000ff",
"#a00000",
"#00a000",
"#400000",
"#ffcc00",
"#00d9ca",
"#6600bf",
"#b22d2d",
"#997a00",
"#263332",
"#b073e6",
"#cc9999",
"#a3cc00",
"#1d6d73",
"#c299cc",
"#d93a00",
"#738040",
"#00c2f2",
"#e200f2",
"#e59173",
"#e6f2b6",
"#002233",
"#4b394d",
"#594943",
"#57664d",
"#408cff",
"#d936a3",
"#4c1f00",
"#44ff00",
"#234d8c",
"#731d4b",
"#a65800",
"#144d00",
"#bfd0ff",
"#ff4073",
"#ffc480",
"#74cc66",
"#000733",
"#331a1d",
"#4c3913",
"#208053",
"#696e8c",
"#d9c7a3",
"#003322",
"#3f1d73",
]
colorIdx = 0
# Losses
ax1.set_ylabel("loss", color=colors[colorIdx])
ax1.tick_params(
axis="y", labelcolor=colors[colorIdx], color=colors[colorIdx], direction="in"
)
if args.max_loss > 0:
ax1.set_ylim(top=args.max_loss)
k1 = "Accuracy"
k2 = "ValLoss"
if len(val[k1]) and k2 in val[k1].keys() and len(val[k1][k2]):
ax1.plot(
val[k1][k2]["x"],
val[k1][k2]["y"],
label=k2,
color=colors[colorIdx],
linestyle="dashed",
)
colorIdx = (colorIdx + 1) % len(colors)
# Accuracy
ax2.set_ylabel("accuracy", color=colors[colorIdx], loc="center")
ax2.tick_params(axis="y", labelcolor=colors[colorIdx], color=colors[colorIdx])
k1 = "Accuracy"
if len(val[k1]):
for k2 in ["Acc@1", "Acc@5"]:
if k2 in val[k1].keys() and len(val[k1][k2]):
ax2.plot(
val[k1][k2]["x"],
val[k1][k2]["y"],
label=k1 + " " + k2,
color=colors[colorIdx],
)
colorIdx = (colorIdx + 1) % len(colors)
# Learning rate
ax3 = ax1.twinx()
ax3.set_ylabel("learning rate", color=colors[colorIdx], loc="center", labelpad=-50)
ax3.tick_params(
axis="y",
labelcolor=colors[colorIdx],
color=colors[colorIdx],
direction="in",
pad=-30,
)
# ax3.tick_params(axis='y', labelcolor=colors[colorIdx], color=colors[colorIdx], direction='out')
plot.yscale("log")
if len(val["LearningRate"]):
ax3.plot(
val["LearningRate"]["x"],
val["LearningRate"]["y"],
label="Learning rate",
color=colors[colorIdx],
linestyle="dotted",
)
colorIdx = (colorIdx + 1) % len(colors)
# train accuracy, if requested
if args.train:
k1 = "Train"
if len(val[k1]):
for k2 in ["Acc@1", "Acc@5"]:
if k2 in val[k1].keys() and len(val[k1][k2]):
ax2.plot(
val[k1][k2]["x"],
val[k1][k2]["y"],
label=k1 + " " + k2,
color=colors[colorIdx],
)
colorIdx = (colorIdx + 1) % len(colors)
ax3.set_yscale("log")
fig.legend(loc=2)
plot.draw()
plot.show()
def main():
parser = argparse.ArgumentParser(description="Pytorch train progress plot")
parser.add_argument("log", type=argparse.FileType("r"), help="train.py output")
parser.add_argument(
"--max-loss", type=float, default=0, help="clamp losses", required=False
)
parser.add_argument(
"--train", action="store_true", help="Print train stats", required=False
)
args = parser.parse_args()
# process output
val = process(log=args.log)
# best epoch
keys = ["Accuracy"]
if args.train:
keys.append("Train")
for k1 in keys:
if k1 in val.keys():
for k2 in ["Acc@1", "Acc@5"]:
epoch = 0
acc = 0
if k2 in val[k1].keys() and len(val[k1][k2]):
for i in range(0, len(val[k1][k2]["x"])):
if val[k1][k2]["y"][i] > acc:
epoch = val[k1][k2]["x"][i]
acc = val[k1][k2]["y"][i]
print(k1, "best", k2, acc, "at epoch", int(epoch))
# plot
draw(val, args)
print("bye")
if __name__ == "__main__":
main()