代码之家  ›  专栏  ›  技术社区  ›  eqzx

如何从张力板事件摘要中提取和保存图像?

  •  18
  • eqzx  · 技术社区  · 8 年前

    .png ?

    3 回复  |  直到 8 年前
        1
  •  17
  •   eqzx    8 年前

    你可以这样提取图像。输出格式可能取决于图像在摘要中的编码方式,因此生成的磁盘写入可能需要使用其他格式 .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  
    

    然后,调用示例可能如下所示:

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

        2
  •  9
  •   Agile Bean    5 年前

    对于那些也可以不用代码的人来说,Tensorboard UI中有一种优雅的方式。

    1. 在左上角,选中复选框 Show data download links
    2. 在左下角,选择下载图标,可以下载svg文件。
    3. 在右下角,为原始数据选择数据下载链接,这对于那些想要进行更复杂的数据分析或数据可视化的人来说很方便

    enter image description here

        3
  •  4
  •   Fabian Reister    5 年前

    如果您使用的是TensorFlow 2,那么它可以很好地工作

    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
    

    虽然“v”有一个图像字段,但它是空的。

    我用过

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

    将图像添加到事件文件。

    推荐文章