基于pytorch的目标检测数据增强(tensor数据流版本)

我的示例代码的dataloader中打包传入的是一个target字典,里面包括boxes和label,如果你们传入的是boxes和label,直接修改参数就行了,然后因为我传入的image和target都是经过torch的转换,数据格式是tensor,所以有一些转换格式的代码,然后图片shape是(c,h,w),随机概率设的是0.3,都按需要修改就行。

随机缩放

class randomScale(object):
    def __call__(self,image,target):
        #固定住高度,以0.8-1.2伸缩宽度,做图像形变
        if random.random() < 0.3:
            image = np.array(image)
            image = np.transpose(image, (1, 2, 0))
            boxes = target["boxes"]
            scale = random.uniform(0.8,1.2)
            height,width,c = image.shape
            image = cv2.resize(image,(int(width*scale),height))
            scale_tensor = torch.FloatTensor([[scale,1,scale,1]]).expand_as(boxes)
            boxes = boxes * scale_tensor
            image = np.transpose(image, (2, 0, 1))
            image = torch.from_numpy(image)
            target["boxes"] = boxes
        return image,target

随机模糊

class randomBlur(object):
    def __call__(self, image, target):
        if random.random() < 0.3:
            image = np.array(image)
            image = np.transpose(image, (1, 2, 0))
            image = cv2.blur(image, (5, 5))
            image = np.transpose(image, (2, 0, 1))
            image = torch.from_numpy(image)
        return image, target

随机擦除(遮挡)
可以增加鲁棒性,提供两个经典算法,cutout和randomerase

class Cutout(object):
    """Randomly mask out one or more patches from an image.
    Args:
        n_holes (int): Number of patches to cut out of each image.
        length (int): The length (in pixels) of each square patch.
    """
    def __init__(self, n_holes=6, length=50):
        self.n_holes = n_holes
        self.length = length

    def __call__(self, image, target):
        """
        Args:
            img (Tensor): Tensor image of size (C, H, W).
        Returns:
            Tensor: Image with n_holes of dimension length x length cut out of it.
        """
        if random.random() < 0.3:
            img = image
            h = img.shape[1]
            w = img.shape[2]

            mask = np.ones((h, w), np.float32)

            for n in range(self.n_holes):
                y = np.random.randint(h)
                x = np.random.randint(w)

                y1 = np.clip(y - self.length // 2, 0, h)
                y2 = np.clip(y + self.length // 2, 0, h)
                x1 = np.clip(x - self.length // 2, 0, w)
                x2 = np.clip(x + self.length // 2, 0, w)

                mask[y1: y2, x1: x2] = 0.

            mask = torch.from_numpy(mask)
            mask = mask.expand_as(img)
            img = img * mask
            image = img
        return image, target
class RandomErasing(object):
    '''
    Class that performs Random Erasing in Random Erasing Data Augmentation by Zhong et al.
    -------------------------------------------------------------------------------------
    probability: The probability that the operation will be performed.
    sl: min erasing area
    sh: max erasing area
    r1: min aspect ratio
    mean: erasing value
    -------------------------------------------------------------------------------------
    '''
    def __init__(self, sl=0.01, sh=0.25, r1=0.3, mean=
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值