保存 tensorflow 对象检测增强图像

Save tensorflow object detection augmented images

有没有办法查看 tensorflow 对象检测 api 训练后的图像 preprocessing/augmentation。

我想验证一切看起来是否正确。我能够通过查看图表 post 在推理中调整大小来验证调整大小,但显然我不能为增强选项这样做。

在过去使用 Keras 时,我能够做到这一点,但我发现我过于激进了。

API 提供增强选项的测试代码。在 input_test.py 文件中,函数 test_apply_image_and_box_augmentation 就是为此目的。您可以通过将自己的图像传递给 tensor_dict 并保存 augmented_tensor_dict_out 进行验证来重写此函数,也可以直接将其可视化。

编辑: 由于此答案很久以前就已回答但仍未被接受,因此我决定提供更具体的示例答案。我写了一个名为 augmentation_test.py.

的小测试脚本
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import functools
import os
from absl.testing import parameterized

import numpy as np
import tensorflow as tf
from scipy.misc import imsave, imread

from object_detection import inputs
from object_detection.core import preprocessor
from object_detection.core import standard_fields as fields
from object_detection.utils import config_util
from object_detection.utils import test_case

FLAGS = tf.flags.FLAGS

class DataAugmentationFnTest(test_case.TestCase):

  def test_apply_image_and_box_augmentation(self):
    data_augmentation_options = [
        (preprocessor.random_horizontal_flip, {
        })
    ]
    data_augmentation_fn = functools.partial(
        inputs.augment_input_data,
        data_augmentation_options=data_augmentation_options)
    tensor_dict = {
        fields.InputDataFields.image:
            tf.constant(imread('lena.jpeg').astype(np.float32)),
        fields.InputDataFields.groundtruth_boxes:
            tf.constant(np.array([[.5, .5, 1., 1.]], np.float32))
    }
    augmented_tensor_dict = 
        data_augmentation_fn(tensor_dict=tensor_dict)
    with self.test_session() as sess:
      augmented_tensor_dict_out = sess.run(augmented_tensor_dict)
    imsave('lena_out.jpeg',augmented_tensor_dict_out[fields.InputDataFields.image])


if __name__ == '__main__':
  tf.test.main()

您可以将此脚本放在 models/research/object_detection/ 下,然后用 python augmentation_test.py 简单地 运行。要成功 运行 您应该提供任何图像名称 'lena.jpeg' 并且增强后的输出图像将保存为 'lena_out.jpeg'.

我 运行 它与 'lena' 图像,这是增强前和增强后的结果。

.

请注意,我在脚本中使用了 preprocessor.random_horizontal_flip。结果准确显示了输入图像在 random_horizontal_flip 之后的样子。要使用其他增强选项对其进行测试,您可以将 random_horizontal_flip 替换为其他方法(这些方法都在 preprocessor.py 以及配置原型文件中定义),您可以将其他选项附加到 data_augmentation_options列表,例如:

data_augmentation_options = [(preprocessor.resize_image, {
        'new_height': 20,
        'new_width': 20,
        'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR
    }),(preprocessor.random_horizontal_flip, {
    })]

这是实现问题 https://github.com/majrie/visualize_augmentation/blob/master/visualize_augmentation.ipynb 中所问内容的代码。

根据@danyfang 在以下问题.

中的回答