stMind

about Tech, Computer vision and Machine learning

TD OD APIでデータ拡張のオプションを追加する

データ拡張のオプションは、TrainConfigの中でdata_augmentation_optionsとして定義されている。

data_augmentation_optionsは、repeatedフィールドで、PreprocessingStepに指定されているデータ拡張を任意の数だけ指定することができる。

// Message for configuring DetectionModel training jobs (train.py).
// Next id: 31
message TrainConfig {
  // Effective batch size to use for training.
  // For TPU (or sync SGD jobs), the batch size per core (or GPU) is going to be
  // `batch_size` / number of cores (or `batch_size` / number of GPUs).
  optional uint32 batch_size = 1 [default=32];

  // Data augmentation options.
  repeated PreprocessingStep data_augmentation_options = 2;

例えば、random_horizontal_flipは、水平方向の反転を行う処理で、デフォルトでは50%の確率で行われる。

pipeline.config内では、フォーマットに従って使いたい拡張を指定すれば良い。

train_config: {
  ...
  data_augmentation_options {
    random_horizontal_flip {
    }
   random_image_scale {
       min_scale_ratio: 0.9
       max_scale_ratio: 1.1
    }
  }
}

一方、ファイル編集ではなくコードの中で直接データ拡張を追加したい場合には、以下の様に行う。

例えば、ssd_resnet50_v1_fpn_640x640_coco17_tpu-8のrandom_horizontal_flipとrandom_crop_imageに対して、random_image_scaleを追加する場合。

from google.protobuf import text_format

from object_detection.builders import preprocessor_builder
from object_detection.core import preprocessor
from object_detection.protos import preprocessor_pb2
from object_detection.utils import config_util

configs = config_util.get_configs_from_pipeline_file(/path/to/pipeline_config)
train_config = configs['train_config']

# Random image scale
preprocessor_text_proto = """
random_image_scale {
  min_scale_ratio: 0.8
  max_scale_ratio: 2.2
}
"""
preprocessor_proto = preprocessor_pb2.PreprocessingStep()
text_format.Merge(preprocessor_text_proto, preprocessor_proto)
train_config.data_augmentation_options.append(preprocessor_proto)

データ拡張オプションが追加されて、3つの処理が指定されている状態。

print(train_config.data_augmentation_options)
"""
[random_horizontal_flip {
}
, random_crop_image {
  min_object_covered: 0.0
  min_aspect_ratio: 0.75
  max_aspect_ratio: 3.0
  min_area: 0.75
  max_area: 1.0
  overlap_thresh: 0.0
}
, random_image_scale {
  min_scale_ratio: 0.800000011920929
  max_scale_ratio: 2.200000047683716
}
]
"""

参考

discuss.tensorflow.org