保存 tensorflow 对象检测增强图像 [英] Save tensorflow object detection augmented images

查看:76
本文介绍了保存 tensorflow 对象检测增强图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在所有预处理/增强之后查看 tensorflow 对象检测 api 训练的图像.

我想验证一下内容是否正确.我能够在推理中查看调整大小后的图表来验证调整大小,但对于增强选项,我显然无法做到这一点.

过去使用 Keras 时,我已经能够做到这一点,而且我发现我太激进了.

解决方案

API 为增强选项提供测试代码.在

.

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

data_augmentation_options = [(preprocessor.resize_image, {'新高度':20,'新宽度':20,方法":tf.image.ResizeMethod.NEAREST_NEIGHBOR}),(preprocessor.random_horizo​​ntal_flip, {})]

Is there a way to view the images that tensorflow object detection api trains on after all preprocessing/augmentation.

I'd like to verify that things look correctly. I was able to verify the resizing my looking at the graph post resize in inference but I obviously can't do that for augmentation options.

In the past with Keras I've been able to do that and I've found that I was to aggressive.

解决方案

The API provides test code for augmentation options. In input_test.py file, the function test_apply_image_and_box_augmentation is for that. You can rewrite this function by passing your own images to the tensor_dict and then save the augmented_tensor_dict_out for verification or you can directly visualize it.

EDIT: Since this answer was long ago answered and still not accepted, I decided to provide a more specific answer with examples. I wrote a little test script called 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()

You can put this script under models/research/object_detection/ and simply run it with python augmentation_test.py. To successfully run it you should provide any image name 'lena.jpeg' and the output image after augmentation would be saved as 'lena_out.jpeg'.

I ran it with the 'lena' image and here is the result before augmentation and after augmentation.

.

Note that I used preprocessor.random_horizontal_flip in the script. And the result showed exactly what the input image looks like after random_horizontal_flip. To test it with other augmentation options, you can replace the random_horizontal_flip with other methods (which are all defined in preprocessor.py and also in the config proto file), all you can append other options to the data_augmentation_options list, for example:

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

这篇关于保存 tensorflow 对象检测增强图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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