-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplot_log.py
47 lines (36 loc) · 1.27 KB
/
plot_log.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
import argparse
import collections
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import json
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--log', type=str, default='result/log')
parser.add_argument('--out', type=str, default='result/log.png')
parser.add_argument('--keys', nargs='+', type=str, default=['dis/loss', 'gen/loss', 'denoiser/loss'])
return parser.parse_args()
def load_log(filename, keys):
"""Parse a JSON file and return a dictionary with the given keys. Each
key maps to a list of corresponding data measurements in the file."""
log = collections.defaultdict(list)
with open(filename) as f:
for data in json.load(f): # For each type of data
for key in keys:
log[key].append(data[key])
return log
def plot_log(filename, log):
"""Create a plot from the given log and write it to disk as an image."""
for key, data in log.items():
plt.plot(range(len(data)), data, label=key)
ax = plt.gca()
ax.legend(loc='best')
plt.savefig(filename)
plt.clf()
plt.close()
def main(args):
log = load_log(args.log, args.keys)
plot_log(args.out, log)
if __name__ == '__main__':
args = parse_args()
main(args)