10
10
import re
11
11
import extract_seconds
12
12
import argparse
13
+ import csv
13
14
14
15
15
16
def get_line_type (line ):
@@ -26,6 +27,13 @@ def get_line_type(line):
26
27
27
28
def parse_log (path_to_log ):
28
29
"""Parse log file
30
+ Returns (train_dict_list, train_dict_names, test_dict_list, test_dict_names)
31
+
32
+ train_dict_list and test_dict_list are lists of dicts that define the table
33
+ rows
34
+
35
+ train_dict_names and test_dict_names are ordered tuples of the column names
36
+ for the two dict_lists
29
37
"""
30
38
31
39
re_iteration = re .compile ('Iteration (\d+)' )
@@ -38,8 +46,10 @@ def parse_log(path_to_log):
38
46
iteration = - 1
39
47
test_accuracy = - 1
40
48
learning_rate = float ('NaN' )
41
- train_list = []
42
- test_list = []
49
+ train_dict_list = []
50
+ test_dict_list = []
51
+ train_dict_names = ('NumIters' , 'Seconds' , 'TrainingLoss' , 'LearningRate' )
52
+ test_dict_names = ('NumIters' , 'Seconds' , 'TestAccuracy' , 'TestLoss' )
43
53
44
54
logfile_year = extract_seconds .get_log_created_year (path_to_log )
45
55
with open (path_to_log ) as f :
@@ -68,8 +78,10 @@ def parse_log(path_to_log):
68
78
train_loss_match = re_train_loss .search (line )
69
79
if train_loss_match :
70
80
train_loss = float (train_loss_match .group (1 ))
71
- train_list .append ((iteration , seconds , train_loss ,
72
- learning_rate ))
81
+ train_dict_list .append ({'NumIters' : iteration ,
82
+ 'Seconds' : seconds ,
83
+ 'TrainingLoss' : train_loss ,
84
+ 'LearningRate' : learning_rate })
73
85
74
86
output_loss_match = re_output_loss .search (line )
75
87
if output_loss_match and get_line_type (line ) == 'test' :
@@ -78,13 +90,16 @@ def parse_log(path_to_log):
78
90
# loss for test data so the test_accuracy variable is already
79
91
# correctly populated and (2) there's one and only one output
80
92
# named "accuracy" for the test net
81
- test_list .append ((iteration , seconds , test_accuracy , test_loss ))
93
+ test_dict_list .append ({'NumIters' : iteration ,
94
+ 'Seconds' : seconds ,
95
+ 'TestAccuracy' : test_accuracy ,
96
+ 'TestLoss' : test_loss })
82
97
83
- return train_list , test_list
98
+ return train_dict_list , train_dict_names , test_dict_list , test_dict_names
84
99
85
100
86
- def save_csv_files (logfile_path , output_dir , train_list , test_list ,
87
- verbose = False ):
101
+ def save_csv_files (logfile_path , output_dir , train_dict_list , train_dict_names ,
102
+ test_dict_list , test_dict_names , verbose = False ):
88
103
"""Save CSV files to output_dir
89
104
90
105
If the input log file is, e.g., caffe.INFO, the names will be
@@ -93,23 +108,20 @@ def save_csv_files(logfile_path, output_dir, train_list, test_list,
93
108
94
109
log_basename = os .path .basename (logfile_path )
95
110
train_filename = os .path .join (output_dir , log_basename + '.train' )
96
- write_csv (train_filename , train_list , '%d,%f,%f,%f' ,
97
- 'NumIters,Seconds,TrainingLoss,LearningRate' , verbose )
111
+ write_csv (train_filename , train_dict_list , train_dict_names , verbose )
98
112
99
113
test_filename = os .path .join (output_dir , log_basename + '.test' )
100
- write_csv (test_filename , test_list , '%d,%f,%f,%f' ,
101
- 'NumIters,Seconds,TestAccuracy,TestLoss' , verbose )
114
+ write_csv (test_filename , test_dict_list , test_dict_names , verbose )
102
115
103
116
104
- def write_csv (output_filename , list_of_tuples , format_string , header ,
105
- verbose = False ):
117
+ def write_csv (output_filename , dict_list , header_names , verbose = False ):
106
118
"""Write a CSV file
107
119
"""
120
+
108
121
with open (output_filename , 'w' ) as f :
109
- f .write (header + '\n ' )
110
- for row in list_of_tuples :
111
- line = format_string % row
112
- f .write (line + '\n ' )
122
+ dict_writer = csv .DictWriter (f , header_names )
123
+ dict_writer .writeheader ()
124
+ dict_writer .writerows (dict_list )
113
125
if verbose :
114
126
print 'Wrote %s' % output_filename
115
127
@@ -135,8 +147,10 @@ def parse_args():
135
147
136
148
def main ():
137
149
args = parse_args ()
138
- train_list , test_list = parse_log (args .logfile_path )
139
- save_csv_files (args .logfile_path , args .output_dir , train_list , test_list )
150
+ train_dict_list , train_dict_names , test_dict_list , test_dict_names = \
151
+ parse_log (args .logfile_path )
152
+ save_csv_files (args .logfile_path , args .output_dir , train_dict_list ,
153
+ train_dict_names , test_dict_list , test_dict_names )
140
154
141
155
142
156
if __name__ == '__main__' :
0 commit comments