Last active
April 11, 2023 09:26
-
-
Save SubhadityaMukherjee/f0afd86f69d10ebc1e07b1023eae4577 to your computer and use it in GitHub Desktop.
tbprocessevents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def process_event_acc(event_acc, save_ims=False) -> dict: | |
"""Process the EventAccumulator and return a dictionary of tag values""" | |
all_tags = event_acc.Tags() # Get all tags | |
temp_dict = {} # Store all values here | |
for tag in all_tags.keys(): # Loop over all tags | |
if tag == "scalars": | |
# Process scalars | |
for subtag in all_tags[tag]: | |
try: | |
# Try to get the last value | |
temp_dict[subtag] = [ | |
tag[-1] for tag in event_acc.Scalars(tag=subtag) | |
][-1].value | |
except: | |
# If there is only one value, get that | |
temp_dict[subtag] = [tag for tag in event_acc.Scalars(tag=subtag)][ | |
-1 | |
].value | |
if tag == "tensors": | |
# Process tensors | |
for subtag in all_tags["tensors"]: | |
tensor_proto = event_acc.Tensors(subtag)[0].tensor_proto | |
if "/text_summary" in subtag: | |
# Decode text summaries to ascii and remove the subtag suffix | |
subtag = subtag.replace("/text_summary", "") | |
value = tensor_proto.string_val[0].decode("ascii") | |
else: | |
# Decode other tensors to float | |
value = tensor_proto | |
temp_dict[subtag] = value | |
if save_ims and tag == "images": | |
# Process images | |
for subtag in all_tags["images"]: | |
try: | |
# Try to get the last value | |
encoded_image = event_acc.Images(subtag)[1].encoded_image_string | |
except IndexError: | |
# If there is only one value, get that | |
encoded_image = event_acc.Images(subtag).encoded_image_string | |
# Decode the image and save it to the dictionary | |
image = Image.open(BytesIO(encoded_image)) | |
temp_dict[subtag] = image | |
return temp_dict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment