How to extract and save images from tensorboard event summary?

If you are using TensorFlow 2, this works nicely

from collections import defaultdict, namedtuple
from typing import List
import tensorflow as tf


TensorBoardImage = namedtuple("TensorBoardImage", ["topic", "image", "cnt"])


def extract_images_from_event(event_filename: str, image_tags: List[str]):
    topic_counter = defaultdict(lambda: 0)

    serialized_examples = tf.data.TFRecordDataset(event_filename)
    for serialized_example in serialized_examples:
        event = event_pb2.Event.FromString(serialized_example.numpy())
        for v in event.summary.value:
            if v.tag in image_tags:

                if v.HasField('tensor'):  # event for images using tensor field
                    s = v.tensor.string_val[2]  # first elements are W and H

                    tf_img = tf.image.decode_image(s)  # [H, W, C]
                    np_img = tf_img.numpy()

                    topic_counter[v.tag] += 1

                    cnt = topic_counter[v.tag]
                    tbi = TensorBoardImage(topic=v.tag, image=np_img, cnt=cnt)

                    yield tbi

Although, 'v' has an image field, it is empty.

I used

    tf.summary.image("topic", img)

to add the images to the event file.


You could extract the images like so. The output format may depend on how the image is encoded in the summary, so the resulting write to disk may need to use another format besides .png

import os
import scipy.misc
import tensorflow as tf

def save_images_from_event(fn, tag, output_dir='./'):
    assert(os.path.isdir(output_dir))

    image_str = tf.placeholder(tf.string)
    im_tf = tf.image.decode_image(image_str)

    sess = tf.InteractiveSession()
    with sess.as_default():
        count = 0
        for e in tf.train.summary_iterator(fn):
            for v in e.summary.value:
                if v.tag == tag:
                    im = im_tf.eval({image_str: v.image.encoded_image_string})
                    output_fn = os.path.realpath('{}/image_{:05d}.png'.format(output_dir, count))
                    print("Saving '{}'".format(output_fn))
                    scipy.misc.imsave(output_fn, im)
                    count += 1  

And then an example invocation may look like:

save_images_from_event('path/to/event/file', 'tag0')

Note that this assumes the event file is fully written -- in the case that it's not, some error handling is probably necessary.


For those who can also do without code, there is an elegant way in the Tensorboard UI.

  1. In the upper left corner, select the checkbox Show data download links
  2. In the lower left corner, select the download icon which lets you download a svg file.
  3. In the lower right corner, select data download links for the raw data, handy for those who want to do more sophisticated data analysis or data visualization

enter image description here