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

2022-11-08 18:18:31 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript复制
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

代码语言:javascript复制
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中的图片将指定区域填充噪声

代码语言:javascript复制
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)

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

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

代码语言:javascript复制
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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

0 人点赞