-
Notifications
You must be signed in to change notification settings - Fork 8
/
exportTensorFlowLog.py
181 lines (155 loc) · 4.96 KB
/
exportTensorFlowLog.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tensorflow as tf
import time
import csv
import sys
import os
import collections
# Import the event accumulator from Tensorboard. Location varies between Tensorflow versions. Try each known location until one works.
eventAccumulatorImported = False;
# TF version < 1.1.0
if (not eventAccumulatorImported):
try:
from tensorflow.python.summary import event_accumulator
eventAccumulatorImported = True;
except ImportError:
eventAccumulatorImported = False;
# TF version = 1.1.0
if (not eventAccumulatorImported):
try:
from tensorflow.tensorboard.backend.event_processing import event_accumulator
eventAccumulatorImported = True;
except ImportError:
eventAccumulatorImported = False;
# TF version >= 1.3.0
if (not eventAccumulatorImported):
try:
from tensorboard.backend.event_processing import event_accumulator
eventAccumulatorImported = True;
except ImportError:
eventAccumulatorImported = False;
# TF version = Unknown
if (not eventAccumulatorImported):
raise ImportError('Could not locate and import Tensorflow event accumulator.')
summariesDefault = ['scalars','histograms','images','audio','compressedHistograms'];
class Timer(object):
# Source: https://stackoverflow.com/a/5849861
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print('[%s]' % self.name)
print('Elapsed: %s' % (time.time() - self.tstart))
def exitWithUsage():
print(' ');
print('Usage:');
print(' python readLogs.py <output-folder> <output-path-to-csv> <summaries>');
print('Inputs:');
print(' <input-path-to-logfile> - Path to TensorFlow logfile.');
print(' <output-folder> - Path to output folder.');
print(' <summaries> - (Optional) Comma separated list of summaries to save in output-folder. Default: ' + ', '.join(summariesDefault));
print(' ');
sys.exit();
if (len(sys.argv) < 3):
exitWithUsage();
inputLogFile = sys.argv[1];
outputFolder = sys.argv[2];
if (len(sys.argv) < 4):
summaries = summariesDefault;
else:
if (sys.argv[3] == 'all'):
summaries = summariesDefault;
else:
summaries = sys.argv[3].split(',');
print(' ');
print('> Log file: ' + inputLogFile);
print('> Output folder: ' + outputFolder);
print('> Summaries: ' + ', '.join(summaries));
if any(x not in summariesDefault for x in summaries):
print('Unknown summary! See usage for acceptable summaries.');
exitWithUsage();
print(' ');
print('Setting up event accumulator...');
with Timer():
ea = event_accumulator.EventAccumulator(inputLogFile,
size_guidance={
event_accumulator.COMPRESSED_HISTOGRAMS: 0, # 0 = grab all
event_accumulator.IMAGES: 0,
event_accumulator.AUDIO: 0,
event_accumulator.SCALARS: 0,
event_accumulator.HISTOGRAMS: 0,
})
print(' ');
print('Loading events from file*...');
print('* This might take a while. Sit back, relax and enjoy a cup of coffee :-)');
with Timer():
ea.Reload() # loads events from file
print(' ');
print('Log summary:');
tags = ea.Tags();
for t in tags:
tagSum = []
if (isinstance(tags[t],collections.Sequence)):
tagSum = str(len(tags[t])) + ' summaries';
else:
tagSum = str(tags[t]);
print(' ' + t + ': ' + tagSum);
if not os.path.isdir(outputFolder):
os.makedirs(outputFolder);
if ('audio' in summaries):
print(' ');
print('Exporting audio...');
with Timer():
print(' Audio is not yet supported!');
if ('compressedHistograms' in summaries):
print(' ');
print('Exporting compressedHistograms...');
with Timer():
print(' Compressed histograms are not yet supported!');
if ('histograms' in summaries):
print(' ');
print('Exporting histograms...');
with Timer():
print(' Histograms are not yet supported!');
if ('images' in summaries):
print(' ');
print('Exporting images...');
imageDir = outputFolder + 'images'
print('Image dir: ' + imageDir);
with Timer():
imageTags = tags['images'];
for imageTag in imageTags:
images = ea.Images(imageTag);
imageTagDir = imageDir + '/' + imageTag;
if not os.path.isdir(imageTagDir):
os.makedirs(imageTagDir);
for image in images:
imageFilename = imageTagDir + '/' + str(image.step) + '.png';
with open(imageFilename,'wb') as f:
f.write(image.encoded_image_string);
if ('scalars' in summaries):
print(' ');
csvFileName = os.path.join(outputFolder,'scalars.csv');
print('Exporting scalars to csv-file...');
print(' CSV-path: ' + csvFileName);
scalarTags = tags['scalars'];
with Timer():
with open(csvFileName,'w') as csvfile:
logWriter = csv.writer(csvfile, delimiter=',');
# Write headers to columns
headers = ['wall_time','step'];
for s in scalarTags:
headers.append(s);
logWriter.writerow(headers);
vals = ea.Scalars(scalarTags[0]);
for i in range(len(vals)):
v = vals[i];
data = [v.wall_time, v.step];
for s in scalarTags:
scalarTag = ea.Scalars(s);
S = scalarTag[i];
data.append(S.value);
logWriter.writerow(data);
print(' ');
print('Bye bye...');