analyze_logs.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import json
  4. from collections import defaultdict
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. import seaborn as sns
  8. def cal_train_time(log_dicts, args):
  9. for i, log_dict in enumerate(log_dicts):
  10. print(f'{"-" * 5}Analyze train time of {args.json_logs[i]}{"-" * 5}')
  11. all_times = []
  12. for epoch in log_dict.keys():
  13. if args.include_outliers:
  14. all_times.append(log_dict[epoch]['time'])
  15. else:
  16. all_times.append(log_dict[epoch]['time'][1:])
  17. all_times = np.array(all_times)
  18. epoch_ave_time = all_times.mean(-1)
  19. slowest_epoch = epoch_ave_time.argmax()
  20. fastest_epoch = epoch_ave_time.argmin()
  21. std_over_epoch = epoch_ave_time.std()
  22. print(f'slowest epoch {slowest_epoch + 1}, '
  23. f'average time is {epoch_ave_time[slowest_epoch]:.4f}')
  24. print(f'fastest epoch {fastest_epoch + 1}, '
  25. f'average time is {epoch_ave_time[fastest_epoch]:.4f}')
  26. print(f'time std over epochs is {std_over_epoch:.4f}')
  27. print(f'average iter time: {np.mean(all_times):.4f} s/iter')
  28. print()
  29. def plot_curve(log_dicts, args):
  30. if args.backend is not None:
  31. plt.switch_backend(args.backend)
  32. sns.set_style(args.style)
  33. # if legend is None, use {filename}_{key} as legend
  34. legend = args.legend
  35. if legend is None:
  36. legend = []
  37. for json_log in args.json_logs:
  38. for metric in args.keys:
  39. legend.append(f'{json_log}_{metric}')
  40. assert len(legend) == (len(args.json_logs) * len(args.keys))
  41. metrics = args.keys
  42. num_metrics = len(metrics)
  43. for i, log_dict in enumerate(log_dicts):
  44. epochs = list(log_dict.keys())
  45. for j, metric in enumerate(metrics):
  46. print(f'plot curve of {args.json_logs[i]}, metric is {metric}')
  47. if metric not in log_dict[epochs[0]]:
  48. raise KeyError(
  49. f'{args.json_logs[i]} does not contain metric {metric}')
  50. xs = []
  51. ys = []
  52. for epoch in epochs:
  53. xs.append(np.array(log_dict[epoch]['step']))
  54. ys.append(np.array(log_dict[epoch][metric]))
  55. xs = np.concatenate(xs)
  56. ys = np.concatenate(ys)
  57. plt.xlabel('step')
  58. plt.plot(xs, ys, label=legend[i * num_metrics + j], linewidth=0.5)
  59. plt.legend()
  60. if args.title is not None:
  61. plt.title(args.title)
  62. if args.out is None:
  63. plt.show()
  64. else:
  65. print(f'save curve to: {args.out}')
  66. plt.savefig(args.out)
  67. plt.cla()
  68. def add_plot_parser(subparsers):
  69. parser_plt = subparsers.add_parser(
  70. 'plot_curve', help='parser for plotting curves')
  71. parser_plt.add_argument(
  72. 'json_logs',
  73. type=str,
  74. nargs='+',
  75. help='path of train log in json format')
  76. parser_plt.add_argument(
  77. '--keys',
  78. type=str,
  79. nargs='+',
  80. default=['loss_kpt'],
  81. help='the metric that you want to plot')
  82. parser_plt.add_argument('--title', type=str, help='title of figure')
  83. parser_plt.add_argument(
  84. '--legend',
  85. type=str,
  86. nargs='+',
  87. default=None,
  88. help='legend of each plot')
  89. parser_plt.add_argument(
  90. '--backend', type=str, default=None, help='backend of plt')
  91. parser_plt.add_argument(
  92. '--style', type=str, default='dark', help='style of plt')
  93. parser_plt.add_argument('--out', type=str, default=None)
  94. def add_time_parser(subparsers):
  95. parser_time = subparsers.add_parser(
  96. 'cal_train_time',
  97. help='parser for computing the average time per training iteration')
  98. parser_time.add_argument(
  99. 'json_logs',
  100. type=str,
  101. nargs='+',
  102. help='path of train log in json format')
  103. parser_time.add_argument(
  104. '--include-outliers',
  105. action='store_true',
  106. help='include the first value of every epoch when computing '
  107. 'the average time')
  108. def parse_args():
  109. parser = argparse.ArgumentParser(description='Analyze Json Log')
  110. # currently only support plot curve and calculate average train time
  111. subparsers = parser.add_subparsers(dest='task', help='task parser')
  112. add_plot_parser(subparsers)
  113. add_time_parser(subparsers)
  114. args = parser.parse_args()
  115. return args
  116. def load_json_logs(json_logs):
  117. # load and convert json_logs to log_dict, key is epoch, value is a sub dict
  118. # keys of sub dict is different metrics, e.g. memory, top1_acc
  119. # value of sub dict is a list of corresponding values of all iterations
  120. log_dicts = [dict() for _ in json_logs]
  121. for json_log, log_dict in zip(json_logs, log_dicts):
  122. with open(json_log, 'r') as log_file:
  123. for line in log_file:
  124. log = json.loads(line.strip())
  125. # skip lines without `epoch` field
  126. if 'epoch' not in log:
  127. continue
  128. epoch = log.pop('epoch')
  129. if epoch not in log_dict:
  130. log_dict[epoch] = defaultdict(list)
  131. for k, v in log.items():
  132. log_dict[epoch][k].append(v)
  133. return log_dicts
  134. def main():
  135. args = parse_args()
  136. json_logs = args.json_logs
  137. for json_log in json_logs:
  138. assert json_log.endswith('.json')
  139. log_dicts = load_json_logs(json_logs)
  140. eval(args.task)(log_dicts, args)
  141. if __name__ == '__main__':
  142. main()