analyze_logs.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if not all_times:
  18. raise KeyError(
  19. 'Please reduce the log interval in the config so that'
  20. 'interval is less than iterations of one epoch.')
  21. epoch_ave_time = np.array(list(map(lambda x: np.mean(x), all_times)))
  22. slowest_epoch = epoch_ave_time.argmax()
  23. fastest_epoch = epoch_ave_time.argmin()
  24. std_over_epoch = epoch_ave_time.std()
  25. print(f'slowest epoch {slowest_epoch + 1}, '
  26. f'average time is {epoch_ave_time[slowest_epoch]:.4f} s/iter')
  27. print(f'fastest epoch {fastest_epoch + 1}, '
  28. f'average time is {epoch_ave_time[fastest_epoch]:.4f} s/iter')
  29. print(f'time std over epochs is {std_over_epoch:.4f}')
  30. print(f'average iter time: {np.mean(epoch_ave_time):.4f} s/iter\n')
  31. def plot_curve(log_dicts, args):
  32. if args.backend is not None:
  33. plt.switch_backend(args.backend)
  34. sns.set_style(args.style)
  35. # if legend is None, use {filename}_{key} as legend
  36. legend = args.legend
  37. if legend is None:
  38. legend = []
  39. for json_log in args.json_logs:
  40. for metric in args.keys:
  41. legend.append(f'{json_log}_{metric}')
  42. assert len(legend) == (len(args.json_logs) * len(args.keys))
  43. metrics = args.keys
  44. # TODO: support dynamic eval interval(e.g. RTMDet) when plotting mAP.
  45. num_metrics = len(metrics)
  46. for i, log_dict in enumerate(log_dicts):
  47. epochs = list(log_dict.keys())
  48. for j, metric in enumerate(metrics):
  49. print(f'plot curve of {args.json_logs[i]}, metric is {metric}')
  50. if metric not in log_dict[epochs[int(args.eval_interval) - 1]]:
  51. if 'mAP' in metric:
  52. raise KeyError(
  53. f'{args.json_logs[i]} does not contain metric '
  54. f'{metric}. Please check if "--no-validate" is '
  55. 'specified when you trained the model. Or check '
  56. f'if the eval_interval {args.eval_interval} in args '
  57. 'is equal to the eval_interval during training.')
  58. raise KeyError(
  59. f'{args.json_logs[i]} does not contain metric {metric}. '
  60. 'Please reduce the log interval in the config so that '
  61. 'interval is less than iterations of one epoch.')
  62. if 'mAP' in metric:
  63. xs = []
  64. ys = []
  65. for epoch in epochs:
  66. ys += log_dict[epoch][metric]
  67. if log_dict[epoch][metric]:
  68. xs += [epoch]
  69. plt.xlabel('epoch')
  70. plt.plot(xs, ys, label=legend[i * num_metrics + j], marker='o')
  71. else:
  72. xs = []
  73. ys = []
  74. for epoch in epochs:
  75. iters = log_dict[epoch]['step']
  76. xs.append(np.array(iters))
  77. ys.append(np.array(log_dict[epoch][metric][:len(iters)]))
  78. xs = np.concatenate(xs)
  79. ys = np.concatenate(ys)
  80. plt.xlabel('iter')
  81. plt.plot(
  82. xs, ys, label=legend[i * num_metrics + j], linewidth=0.5)
  83. plt.legend()
  84. if args.title is not None:
  85. plt.title(args.title)
  86. if args.out is None:
  87. plt.show()
  88. else:
  89. print(f'save curve to: {args.out}')
  90. plt.savefig(args.out)
  91. plt.cla()
  92. def add_plot_parser(subparsers):
  93. parser_plt = subparsers.add_parser(
  94. 'plot_curve', help='parser for plotting curves')
  95. parser_plt.add_argument(
  96. 'json_logs',
  97. type=str,
  98. nargs='+',
  99. help='path of train log in json format')
  100. parser_plt.add_argument(
  101. '--keys',
  102. type=str,
  103. nargs='+',
  104. default=['bbox_mAP'],
  105. help='the metric that you want to plot')
  106. parser_plt.add_argument(
  107. '--start-epoch',
  108. type=str,
  109. default='1',
  110. help='the epoch that you want to start')
  111. parser_plt.add_argument(
  112. '--eval-interval',
  113. type=str,
  114. default='1',
  115. help='the eval interval when training')
  116. parser_plt.add_argument('--title', type=str, help='title of figure')
  117. parser_plt.add_argument(
  118. '--legend',
  119. type=str,
  120. nargs='+',
  121. default=None,
  122. help='legend of each plot')
  123. parser_plt.add_argument(
  124. '--backend', type=str, default=None, help='backend of plt')
  125. parser_plt.add_argument(
  126. '--style', type=str, default='dark', help='style of plt')
  127. parser_plt.add_argument('--out', type=str, default=None)
  128. def add_time_parser(subparsers):
  129. parser_time = subparsers.add_parser(
  130. 'cal_train_time',
  131. help='parser for computing the average time per training iteration')
  132. parser_time.add_argument(
  133. 'json_logs',
  134. type=str,
  135. nargs='+',
  136. help='path of train log in json format')
  137. parser_time.add_argument(
  138. '--include-outliers',
  139. action='store_true',
  140. help='include the first value of every epoch when computing '
  141. 'the average time')
  142. def parse_args():
  143. parser = argparse.ArgumentParser(description='Analyze Json Log')
  144. # currently only support plot curve and calculate average train time
  145. subparsers = parser.add_subparsers(dest='task', help='task parser')
  146. add_plot_parser(subparsers)
  147. add_time_parser(subparsers)
  148. args = parser.parse_args()
  149. return args
  150. def load_json_logs(json_logs):
  151. # load and convert json_logs to log_dict, key is epoch, value is a sub dict
  152. # keys of sub dict is different metrics, e.g. memory, bbox_mAP
  153. # value of sub dict is a list of corresponding values of all iterations
  154. log_dicts = [dict() for _ in json_logs]
  155. for json_log, log_dict in zip(json_logs, log_dicts):
  156. with open(json_log, 'r') as log_file:
  157. epoch = 1
  158. for i, line in enumerate(log_file):
  159. log = json.loads(line.strip())
  160. val_flag = False
  161. # skip lines only contains one key
  162. if not len(log) > 1:
  163. continue
  164. if epoch not in log_dict:
  165. log_dict[epoch] = defaultdict(list)
  166. for k, v in log.items():
  167. if '/' in k:
  168. log_dict[epoch][k.split('/')[-1]].append(v)
  169. val_flag = True
  170. elif val_flag:
  171. continue
  172. else:
  173. log_dict[epoch][k].append(v)
  174. if 'epoch' in log.keys():
  175. epoch = log['epoch']
  176. return log_dicts
  177. def main():
  178. args = parse_args()
  179. json_logs = args.json_logs
  180. for json_log in json_logs:
  181. assert json_log.endswith('.json')
  182. log_dicts = load_json_logs(json_logs)
  183. eval(args.task)(log_dicts, args)
  184. if __name__ == '__main__':
  185. main()