15

TensorBoard 是一个很棒的工具,但它可以更强大吗?下图显示了 TensorBoard 中的可视化。

它由以下代码调用:

tf.image_summary('images', images, max_images=100)

正如API所建议的那样,最后一位数字是“图像编号”,在这种情况下从0到99,因为我指定了max_images = 100。我想问一下,是否可以将此图像的标签附加到文本中?这将是一个很棒的功能,因为它允许用户在训练期间实时查看图像及其各自的标签。如果某些图像完全被错误标记,则可以实施修复。换句话说,我希望下图中的相应文本是:

images/image/9/5
images/image/39/6
images/image/31/0
images/image/30/2
where last digit is the label.

谢谢!

在此处输入图像描述

4

2 回答 2

7

我无法找到仅使用 tensorflow 的方法,因此我执行以下操作:

  1. 为摘要图像创建一个占位符(例如,十个摘要图像的 (10, 224, 224, 3))。
  2. 基于该占位符创建图像摘要。
  3. 在验证(或训练,如果你愿意)期间,使用类似session.run([sample_images, sample_labels]).
  4. 遍历批次并使用 OpenCV 将标签写入图像,使用cv2.putText.
  5. 运行摘要操作,为占位符提供标记的图像。
于 2016-03-15T17:34:46.197 回答
3

这是 Vince Gatto 提出的方法的一个小改进。我们可以tf.py_func用来避免创建额外的占位符和做额外的session.run.

首先,我们定义这些函数(你需要opencv-python安装):

import cv2
import tensorflow as tf

def put_text(imgs, texts):
    result = np.empty_like(imgs)
    for i in range(imgs.shape[0]):
        text = texts[i]
        if isinstance(text, bytes):
            text = text.decode()
        # You may need to adjust text size and position and size.
        # If your images are in [0, 255] range replace (0, 0, 1) with (0, 0, 255)
        result[i, :, :, :] = cv2.putText(imgs[i, :, :, :], str(text), (0, 30), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 1), 2)
    return result

def tf_put_text(imgs, texts):
    return tf.py_func(put_text, [imgs, texts], Tout=imgs.dtype)

现在我们可以tf_put_text在顶部图像上打印标签,然后将它们提供给图像摘要:

annotated_images = tf_put_text(images, labels)
tf.summary.image('annotated_images', annotated_images, 4)
于 2018-10-17T15:39:17.847 回答