-
Notifications
You must be signed in to change notification settings - Fork 5
/
tf_event2txt.py
45 lines (37 loc) · 1.54 KB
/
tf_event2txt.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
"""
Created on Fri, 15 Dec 2023
@author: Ye Kyaw Thu, Visiting Professor, LST, NECTEC, Thailand
How to Run:
$ python ./tf_event2txt.py --help
$ time python ./tf_event2txt.py --input ./events.out.tfevents.1702602561.lst-gpu-3090.18783.0 --output ./log.txt
"""
import argparse
import sys
import tensorflow as tf
from tensorflow.core.util import event_pb2
from tensorflow.python.lib.io import tf_record
def read_tf_events_file(file_path, output_file=None):
for serialized_example in tf_record.tf_record_iterator(file_path):
event = event_pb2.Event.FromString(serialized_example)
output_string = "Event: {}\n".format(event)
if event.WhichOneof('what') == 'summary':
for value in event.summary.value:
output_string += "Summary Value: {}\n".format(value)
if output_file:
with open(output_file, 'a') as f:
f.write(output_string)
else:
print(output_string)
def main():
parser = argparse.ArgumentParser(description="Read TensorFlow event files.")
parser.add_argument("-i", "--input", type=str, help="Input TensorFlow event file. If not provided, reads from stdin.")
parser.add_argument("-o", "--output", type=str, help="Output file to write the results. If not provided, prints to stdout.")
args = parser.parse_args()
if args.input:
read_tf_events_file(args.input, args.output)
else:
# Read from stdin
for line in sys.stdin:
read_tf_events_file(line.strip(), args.output)
if __name__ == "__main__":
main()