1#!/usr/bin/env python 2 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19 20""" 21parse mxnet output log into a markdown table 22""" 23import argparse 24import sys 25import re 26 27parser = argparse.ArgumentParser(description='Parse mxnet output log') 28parser.add_argument('logfile', nargs=1, type=str, 29 help = 'the log file for parsing') 30parser.add_argument('--format', type=str, default='markdown', 31 choices = ['markdown', 'none'], 32 help = 'the format of the parsed outout') 33parser.add_argument('--metric-names', type=str, nargs="+", default = ['accuracy'], 34 help='names of metrics in log which should be parsed') 35args = parser.parse_args() 36 37with open(args.logfile[0]) as f: 38 lines = f.readlines() 39 40res = [re.compile('.*Epoch\[(\d+)\] Train-'+s+'.*=([.\d]+)') for s in args.metric_names]\ 41 + [re.compile('.*Epoch\[(\d+)\] Validation-'+s+'.*=([.\d]+)') for s in args.metric_names]\ 42 + [re.compile('.*Epoch\[(\d+)\] Time.*=([.\d]+)')] 43 44data = {} 45for l in lines: 46 i = 0 47 for r in res: 48 m = r.match(l) 49 if m is not None: 50 break 51 i += 1 52 if m is None: 53 continue 54 55 assert len(m.groups()) == 2 56 epoch = int(m.groups()[0]) 57 val = float(m.groups()[1]) 58 59 if epoch not in data: 60 data[epoch] = [0] * len(res) * 2 61 62 data[epoch][i*2] += val 63 data[epoch][i*2+1] += 1 64 65if args.format == 'markdown': 66 print("| epoch | " + " | ".join(['train-'+s for s in args.metric_names]) + " | " + " | ".join(['val-'+s for s in args.metric_names]) + " | time |") 67 print("| --- "*(len(res)+1) + "|") 68 for k, v in data.items(): 69 print("| %2d | " % (k+1)\ 70 + " | ".join(["%f" % (v[2*j]/v[2*j+1]) for j in range(2*len(args.metric_names))])\ 71 + " | %.1f |" % (v[-2]/v[-1])) 72elif args.format == 'none': 73 print("\t".join(['epoch'] + ['train-' + s for s in args.metric_names] + ['val-' + s for s in args.metric_names] + ['time'])) 74 for k, v in data.items(): 75 print("\t".join(["%2d" % (k+1)] + ["%f" % (v[2*j]/v[2*j+1]) for j in range(2*len(args.metric_names))] + ["%.1f" % (v[-2]/v[-1])])) 76