Detectron2-資料增強方法

 找到 detectron2\engine\defaults.py

客製化mapper 其中transform_list 為數據增強方法包含resize、翻轉、改變亮度等
更多強化方法可參考:https://detectron2.readthedocs.io/en/latest/modules/data_transforms.html

import copy
from detectron2.data import detection_utils as utils

def mapper(dataset_dict):
     # Implement a mapper, similar to the default DatasetMapper, but with your own customizations
    dataset_dict = copy.deepcopy(dataset_dict)  # it will be modified by code below
    image = utils.read_image(dataset_dict["file_name"], format="BGR")
    transform_list = [
                      #T.Resize(300,300),
                      #T.RandomCrop("absolute", (640, 640)),
                      T.RandomSaturation(0.5,1.5),
                      T.RandomRotation(angle=[-90.0, 90.0]),
                      T.RandomLighting(scale=0.1),
                      T.RandomSaturation(0.75, 1.25),
                      T.RandomFlip(prob=0.5, horizontal=False, vertical=True),
                      T.RandomFlip(prob=0.5, horizontal=True, vertical=False),
                      T.RandomContrast(0.8, 3),
                      T.RandomBrightness(0.8, 1.6),
                      ]

    image, transforms = T.apply_transform_gens(transform_list, image)
    dataset_dict["image"] = torch.as_tensor(image.transpose(2, 0, 1).astype("float32"))

    annos = [
        utils.transform_instance_annotations(obj, transforms, image.shape[:2])
        for obj in dataset_dict.pop("annotations")
    ]
    instances = utils.annotations_to_instances(annos, image.shape[:2])
    dataset_dict["instances"] = utils.filter_empty_instances(instances)
    return dataset_dict

修改build_train_loader方法把mapper套用

@classmethod
    def build_train_loader(cls, cfg):
        """
        Returns:
            iterable

        It now calls :func:`detectron2.data.build_detection_train_loader`.
        Overwrite it if you'd like a different data loader.
        """
        return build_detection_train_loader(cfg, mapper=mapper)
        #return build_detection_train_loader(cfg)

留言

這個網誌中的熱門文章

Python-相關係數矩陣實作(python-correlation matrix )

ASP.NET-後端將值傳給javascript

ASP.NET-FileUpload上傳後自動觸發button click(FileUpload upload auto trigger button click)