API 提供了增强选项的测试代码。在输入测试.py https://github.com/tensorflow/models/blob/master/research/object_detection/inputs_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”。
I ran it with the 'lena' image and here is the result before augmentation and after augmentation.
.
请注意,我使用了preprocessor.random_horizontal_flip
在脚本中。结果准确地显示了输入图像之后的样子random_horizontal_flip
。要使用其他增强选项对其进行测试,您可以替换random_horizontal_flip
与其他方法(全部定义在预处理器.py https://github.com/tensorflow/models/blob/master/research/object_detection/core/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, {
})]