数据增强之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
        rrate = 1.0 / rflag
        if hflag < 50:
            sw = w // rflag
            wsp = sw * pflag;
        else:
            sh = h // rflag
            hsp = sh * pflag
    else:
        hflag = random.randint(1, 100)
        wflag = random.randint(1, 100)
        sw = int(max((w / 2 * wflag / 100), 5))
        sh = int(max((h / 2 * hflag / 100), 5))
        wsp = random.randint(0, w - sw - 1)
        hsp = random.randint(0, h - sh - 1)
        rrate = sw * sh * 1.0 / (h * w)
    bsp = bflag
    bep = (b >> 2) << 1
    bmp = bsp + (bep >> 1)
    bep = bsp + bep
    idxs1 = np.arange(bmp - bsp) + bsp
    idxs2 = np.arange(bep - bmp) + bmp
    nidx1 = np.concatenate([idxs1, idxs2])
    nidx2 = np.concatenate([idxs2, idxs1])
    img_np = img.cpu().data.numpy()
    img_np[nidx1, :, hsp:hsp + sh, wsp: wsp + sw] = img_np[nidx2, :, hsp:hsp + sh, wsp: wsp + sw]
    img = torch.from_numpy(img_np)
    img = Variable(img)
    nlabel = np.tile(label.cpu().data.numpy().reshape([-1, 1]), [1, 2])
    # nlabel[bsp:bmp, 1], nlabel[bmp:bep, 1] = nlabel[bmp:bep, 1], nlabel[bsp:bmp, 1]
    nlabel[nidx1, 1] = nlabel[nidx2, 1]
    nlabel = torch.from_numpy(nlabel)
    return img, nlabel, rrate
# loss 变化
def label_mix_loss(prediction, nlabel, rrate=0.0):
    oloss = F.log_softmax(prediction, dim=1)
    kloss = torch.gather(oloss, 1, nlabel)
    loss = kloss[:, 0] * (1.0 - rrate) + kloss[:, 1] * rrate
    loss = -loss
    return loss
# 运用
img, nlabel, rrate = mix_make_data(img, label)
prediction = model(img.cuda(), y=nlabel.cuda())
loss = label_mix_loss(prediction, nlabel.cuda(), rrate)

随机选择一个batch中的图片将指定区域填充噪声

img.cuda()
batch_size = img.size()[0]
rand_index = torch.randperm(batch_size).cuda()
lam = random.uniform(0.1,0.25)
bbx1, bby1, bbx2, bby2 = rand_bbox(img.size(), lam)
 rand_index = rand_index[:int(batch_size*args.cutout_ratio)]
img[rand_index, :,  bbx1:bbx2, bby1:bby2] = img[rand_index, :, bbx1:bbx2, 			bby1:bby2].fill_(lam)

同样也可以将此方法应用在特征中,对特征进行添加噪声块

其他增强方法,图像重压缩,模糊度,

class JpegCompression(object):
    """Randomly apply gamma correction
    """

    def __init__(self, probability=0.3):
        self.probability = probability

    def __call__(self, img):
        if np.random.random() > self.probability:
            return img

        quality = np.random.randint(80, 99)

        out = BytesIO()
        img.save(out, format='jpeg', quality=quality)
        return Image.open(out)
class Blur(object):
    def __init__(self, probability=0.3):
        self.probability = probability
        
    def __call__(self,img):
        if np.random.random() > self.probability:
            return img
        img = img.filter(ImageFilter.BLUR)

        return img
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值