如何使用Keras在TensorBoard中显示自定义图像? [英] How to display custom images in TensorBoard using Keras?

查看:50
本文介绍了如何使用Keras在TensorBoard中显示自定义图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Keras中的细分问题,我想在每个训练时期结束时显示细分结果.

I'm working on a segmentation problem in Keras and I want to display segmentation results at the end of every training epoch.

我想要类似于 Tensorflow:如何在Tensorboard中显示自定义图像(例如Matplotlib图) ,但使用Keras.我知道Keras具有 TensorBoard 回调,但似乎仅限于此目的.

I want something similar to Tensorflow: How to Display Custom Images in Tensorboard (e.g. Matplotlib Plots), but using Keras. I know that Keras has the TensorBoard callback but it seems limited for this purpose.

我知道这会破坏Keras后端抽象,但是无论如何,我对使用TensorFlow后端很感兴趣.

I know this would break the Keras backend abstraction, but I'm interested in using TensorFlow backend anyway.

使用Keras + TensorFlow是否可以实现这一目标?

Is it possible to achieve that with Keras + TensorFlow?

推荐答案

因此,以下解决方案对我来说效果很好:

So, the following solution works well for me:

import tensorflow as tf

def make_image(tensor):
    """
    Convert an numpy representation image to Image protobuf.
    Copied from https://github.com/lanpa/tensorboard-pytorch/
    """
    from PIL import Image
    height, width, channel = tensor.shape
    image = Image.fromarray(tensor)
    import io
    output = io.BytesIO()
    image.save(output, format='PNG')
    image_string = output.getvalue()
    output.close()
    return tf.Summary.Image(height=height,
                         width=width,
                         colorspace=channel,
                         encoded_image_string=image_string)

class TensorBoardImage(keras.callbacks.Callback):
    def __init__(self, tag):
        super().__init__() 
        self.tag = tag

    def on_epoch_end(self, epoch, logs={}):
        # Load image
        img = data.astronaut()
        # Do something to the image
        img = (255 * skimage.util.random_noise(img)).astype('uint8')

        image = make_image(img)
        summary = tf.Summary(value=[tf.Summary.Value(tag=self.tag, image=image)])
        writer = tf.summary.FileWriter('./logs')
        writer.add_summary(summary, epoch)
        writer.close()

        return

tbi_callback = TensorBoardImage('Image Example')

只需将回调传递给fitfit_generator.

请注意,您还可以使用回调内的model运行某些操作.例如,您可以在某些图像上运行模型以检查其性能.

Note that you can also run some operations using the model inside the callback. For example, you may run the model on some images to check its performance.

这篇关于如何使用Keras在TensorBoard中显示自定义图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆