使用对象检测api的默认配置时,不同尺寸的图像缩放器的影响是什么

yuh*_*566 6 object-detection tensorflow

我试图使用Tensorflow的对象检测API来训练模型.我正在使用更快的rcnn resnet101的示例配置(https://github.com/tensorflow/models/blob/master/object_detection/samples/configs/faster_rcnn_resnet101_voc07.config).
以下代码是我不太了解的配置文件的一部分:

image_resizer {
  keep_aspect_ratio_resizer {
    min_dimension: 600
    max_dimension: 1024
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:

  1. min_dimensionand 的确切含义是max_dimension什么?是否意味着输入图像的大小将调整为600x1024或1024x600?
  2. 如果我有图像的大小不同,也许他们中的一些比600x1024(或1024×600)相对较大,可以/我应该增加的价值min_dimensionmax_dimension

我有这样的问题的原因来自这篇文章: TensorFlow对象检测API怪异行为

在这篇文章中,作者自己回答了这个问题:

然后我决定裁剪输入图像并将其作为输入提供.只是为了看看结果是否有所改善而且确实如此!
事实证明,输入图像的尺寸远大于模型所接受的600 x 1024.因此,它将这些图像缩小到600 x 1024,这意味着香烟盒正在丢失它们的细节:)

它使用了我使用的相同配置.我不确定如果这些参数是默认设置或建议设置为此特殊模型,fast_rcnn_resnet101,我是否可以更改这些参数.

yuh*_*566 6

经过一些测试,我想我找到了答案.如果有任何错误,请纠正我.

在.config文件中:

image_resizer {
  keep_aspect_ratio_resizer {
    min_dimension: 600
    max_dimension: 1024
  }
}
Run Code Online (Sandbox Code Playgroud)

根据'object_detection/builders/image_resizer_builder.py'的图像缩放器设置

if image_resizer_config.WhichOneof(
    'image_resizer_oneof') == 'keep_aspect_ratio_resizer':
  keep_aspect_ratio_config = image_resizer_config.keep_aspect_ratio_resizer
  if not (keep_aspect_ratio_config.min_dimension
          <= keep_aspect_ratio_config.max_dimension):
    raise ValueError('min_dimension > max_dimension')
  return functools.partial(
      preprocessor.resize_to_range,
      min_dimension=keep_aspect_ratio_config.min_dimension,
      max_dimension=keep_aspect_ratio_config.max_dimension)
Run Code Online (Sandbox Code Playgroud)

然后它尝试使用'object_detection/core/preprocessor.py'的'resize_to_range'函数

  with tf.name_scope('ResizeToRange', values=[image, min_dimension]):
    image_shape = tf.shape(image)
    orig_height = tf.to_float(image_shape[0])
    orig_width = tf.to_float(image_shape[1])
    orig_min_dim = tf.minimum(orig_height, orig_width)

    # Calculates the larger of the possible sizes
    min_dimension = tf.constant(min_dimension, dtype=tf.float32)
    large_scale_factor = min_dimension / orig_min_dim
    # Scaling orig_(height|width) by large_scale_factor will make the smaller
    # dimension equal to min_dimension, save for floating point rounding errors.
    # For reasonably-sized images, taking the nearest integer will reliably
    # eliminate this error.
    large_height = tf.to_int32(tf.round(orig_height * large_scale_factor))
    large_width = tf.to_int32(tf.round(orig_width * large_scale_factor))
    large_size = tf.stack([large_height, large_width])

    if max_dimension:
      # Calculates the smaller of the possible sizes, use that if the larger
      # is too big.
      orig_max_dim = tf.maximum(orig_height, orig_width)
      max_dimension = tf.constant(max_dimension, dtype=tf.float32)
      small_scale_factor = max_dimension / orig_max_dim
      # Scaling orig_(height|width) by small_scale_factor will make the larger
      # dimension equal to max_dimension, save for floating point rounding
      # errors. For reasonably-sized images, taking the nearest integer will
      # reliably eliminate this error.
      small_height = tf.to_int32(tf.round(orig_height * small_scale_factor))
      small_width = tf.to_int32(tf.round(orig_width * small_scale_factor))
      small_size = tf.stack([small_height, small_width])

      new_size = tf.cond(
          tf.to_float(tf.reduce_max(large_size)) > max_dimension,
          lambda: small_size, lambda: large_size)
    else:
      new_size = large_size

    new_image = tf.image.resize_images(image, new_size,
                                       align_corners=align_corners)
Run Code Online (Sandbox Code Playgroud)

从上面的代码中,我们可以知道我们是否有一个大小为800*1000的图像.最终输出图像的大小为600*750.

也就是说,此图像缩放器将始终根据"min_dimension"和"max_dimension"的设置调整输入图像的大小.

  • 嗯……这真是一个很难回答的问题。正如我之前提到的,您的图像将始终根据最小和最大尺寸调整大小。如果你真的想让你的物体检测器学习输入图像中物体的正确大小。您也许可以尝试使用多种最小和最大维度来训练您的模型。也就是说,它可以识别不同尺寸的物体。但!如果所有输入图像的大小相同,则只需设置 min=2976 和 max=4464。它应该工作。 (2认同)