2017-11-10 69 views

回答

4

你可以像这样提取图像。输出格式可能取决于图像是如何在总结编码,所以造成写入到磁盘可能需要使用其它格式除了.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')

请注意,这里假定事件文件是完全写入的 - 如果不是,则可能需要一些错误处理。

+0

不错的代码,尽管如果你用“if v.tag.find(tag)!= - 1:”替换最后一个条件,它的工作方式会更好。这一个帮助我;) – Eisa

+0

@Eisa将匹配标签字符串与子字符串,用户可能不想(取决于)。如果标签的小子串唯一标识标签,则您的方法将起作用。你能否详细说明你的意思是“工作方式更好”?很高兴它帮助你为我的情况 – eqzx

+0

,我有一些'输出/图像/#','输入/图像/#','剪切/图像/#'...,(#= 0,1,...) 标签名称的输出,输入,剪切,..但你的代码无法提取图像,直到我修改它,如我所述。 – Eisa

相关问题