数据增强之cutout变体,添加噪声和mixcut

数据增强之cutout变体,添加噪声
生成框

def rand_bbox(size, lam):
    W = size[2]
    H = size[3]
    # ratio = np.sqrt(1. - lam)
    cut_w = np.int(W * lam)
    cut_h = np.int(H * lam)

    # uniform
    cx = np.random.randint(W)
    cy = np.random.randint(H)

    bbx1 = np.clip(cx - cut_w // 2, 0, W)
    bby1 = np.clip(cy - cut_h // 2, 0, H)
    bbx2 = np.clip(cx + cut_w // 2, 0, W)
    bby2 = np.clip(cy + cut_h // 2, 0, H)

    return bbx1, bby1, bbx2, bby2

CutMix

def mix_make_data(img, label):
    b, _, h, w = img.shape
    bflag = random.randint(0, b // 2 - 1)
    fflag = random.randint(0, 100)
    rrate = 1.0
    if fflag < 50:
        hflag = random.randint(0, 100)
        rflag = random.randint(2, 6)
        pflag = random.randint(0, rflag)
        wsp = 0
        hsp = 0
        sw = w
        sh = h
       
### Cutout 数据增强技术概述 Cutout 是一种简单而有效的数据增强方法,通过遮挡输入图像的一部分来提升模型的鲁棒性泛化能力。具体来说,该方法会在训练过程中随机选取若干矩形区域,并将这些区域内像素值设为零[^2]。 这种方法能够促使模型不过分依赖于特定位置的信息,从而增强了其对不同情况下的适应性。此外,Cutout 还有助于缓解过拟合现象的发生,尤其是在小规模数据集上的表现尤为明显。 #### Python 实现示例 下面是一个简单的 PyTorch 版本的 Cutout 实现: ```python import numpy as np import torch from torchvision import transforms 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, length): self.n_holes = n_holes self.length = length def __call__(self, img): h = img.size(1) w = img.size(2) mask = np.ones((h, w), np.float32) for _ in range(self.n_holes): y = np.random.randint(h) x = np.random.randint(w) y1 = int(np.clip(y - self.length / 2, 0, h)) y2 = int(np.clip(y + self.length / 2, 0, h)) x1 = int(np.clip(x - self.length / 2, 0, w)) x2 = int(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 *= mask return img ``` 此代码定义了一个 `Cutout` 类,用于创建指定数量大小的黑色方块覆盖原图的部分区域。在实际应用中,可以通过调整参数 `n_holes` `length` 来控制遮挡的数量与面积。 #### 应用场景 Cutout 主要应用于卷积神经网络(CNNs)的训练阶段,特别是在计算机视觉任务如图像分类、目标检测等领域表现出色。通过对原始图片施加这种变换,可以让模型更加关注全局特征而非局部细节,进而改善最终预测效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值