detectron2的数据在线增强

文章目录


前言

关于一些detectron2的学习记录和分享,有问题可留言交流。

一、训练数据的在线增强

使用visualize_data.py观察dataloader里增强后用于训练的图片和标注。

train_data_loader = build_detection_train_loader(cfg) #构建训练数据loader

到data/build.py里找到build_detection_train_loader()

def build_detection_train_loader(cfg, mapper=None):
    if mapper is None:
        mapper = DatasetMapper(cfg, True) 

到dataset_mapper.py里

    def from_config(cls, cfg, is_train: bool = True):
    	#这个函数里可以增加数据增强方法
        augs = utils.build_augmentation(cfg, is_train)
        #对训练的数据做裁剪
        if cfg.INPUT.CROP.ENABLED and is_train:
            augs.insert(0, T.RandomCrop(cfg.INPUT.CROP.TYPE, cfg.INPUT.CROP.SIZE))

到detection_utils.py里,做了resize和flip。

def build_augmentation(cfg, is_train):
    """
    Create a list of default :class:`Augmentation` from config.
    Now it includes resizing and flipping.

    Returns:
        list[Augmentation]
    """
    if is_train:
        min_size = cfg.INPUT.MIN_SIZE_TRAIN
        max_size = cfg.INPUT.MAX_SIZE_TRAIN
        sample_style = cfg.INPUT.MIN_SIZE_TRAIN_SAMPLING
    else:
        min_size = cfg.INPUT.MIN_SIZE_TEST
        max_size = cfg.INPUT.MAX_SIZE_TEST
        sample_style = "choice"
    augmentation = [T.ResizeShortestEdge(min_size, max_size, sample_style)]
    if is_train and cfg.INPUT.RANDOM_FLIP != "none":
        augmentation.append(
            T.RandomFlip(
                horizontal=cfg.INPUT.RANDOM_FLIP == "horizontal",
                vertical=cfg.INPUT.RANDOM_FLIP == "vertical",
            )
        )
    return augmentation

一些自带的增强方法函数在augmentation_impl.py里

from .augmentation import Augmentation, _transform_to_aug
from .transform import ExtentTransform, ResizeTransform, RotationTransform
__all__ = [
    "RandomApply",
    "RandomBrightness",
    "RandomContrast",
    "RandomCrop",
    "RandomExtent",
    "RandomFlip",
    "RandomSaturation",
    "RandomLighting",
    "RandomRotation",
    "Resize",
    "ResizeShortestEdge",
    "RandomCrop_CategoryAreaConstraint",
]

不够用的话,可以在transform.py里,自己外面包一层就是上面的那些。
all = [
“ExtentTransform”,
“ResizeTransform”,
“RotationTransform”,
“ColorTransform”,
“PILColorTransform”,
]
暂时就这些

上一篇:Detectron2 API 之 config | 十五


下一篇:detectron2 特征金字塔代码讲解,detectron2train的过程