【数据增强】Learning Data Augmentation Strategies for Object Detection 代码

参考官方TF版,在numpy版基础上实现的:

GitHub - zzl421/Data_Augmentation_Zoo_for_Object_Detection: Includes: Learning data augmentation strategies for object detection | GridMask data augmentation | Augmentation for small object detection in Numpy. Use RetinaNet with ResNet-18 to test these methods on VOC and KITTI.

一、多函数如何统一调用:

1. 用字典建立函数名和函数的对应关系。相似函数如Translate_X和Translate_Y可以借助lambda函数,送入不同的布尔值来xu

'TranslateX_BBox': lambda image, bboxes, pixels, replace: 
    translate_bbox(image, bboxes, pixels, replace, shift_horizontal=True),
'TranslateY_BBox': lambda image, bboxes, pixels, replace: 
    translate_bbox(image, bboxes, pixels, replace, shift_horizontal=False),

2. 借助inspect.getfullargspec(func)[0]获得函数的参数列表,通过参数列表,给函数送入不同的args:

def _parse_policy_info(name, prob, level, replace_value, augmentation_hparams):
    """Return the function that corresponds to `name` and update `level` param."""
    func = NAME_TO_FUNC[name]
    args = level_to_arg(augmentation_hparams)[name](level)

    if 'prob' in inspect.getfullargspec(func)[0]:
        # if 'prob' in inspect.signature(func)[0]:
        args = tuple([prob] + list(args))
  
    if 'replace' in inspect.getfullargspec(func)[0]:
        # Make sure replace is the final argument
        assert 'replace' == inspect.getfullargspec(func)[0][-1]
        args = tuple(list(args) + [replace_value])

    if 'bboxes' not in inspect.getfullargspec(func)[0]:
        func = bbox_wrapper(func)
    """
        args经过对应函数进行了修改,如:prob, replace
        func对参数存在没有bboxes进行了修改
        prob无修改
    """
    return (func, prob, args)

有些函数不需要bboxes参数,除了送入函数后不使用外,还有一种包装方法:

def bbox_wrapper(func):
    """Adds a bboxes function argument to func and returns unchanged bboxes."""

    def wrapper(images, bboxes, *args, **kwargs):
        return (func(images, *args, **kwargs), bboxes)

    return wrapper

3. 定义函数列表:定义函数内函数,将执行函数再次包装。

        def make_final_policy(tf_policy_):
            def final_policy(image_, bboxes_):
                for func, prob, args in tf_policy_:
                    image_, bboxes_ = _apply_func_with_prob(func, image_, args, prob, bboxes_)
                return image_, bboxes_
            return final_policy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值