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

数据增强之cutout变体,添加噪声和mixcut数据增强之cutout变体,添加噪声生成框defrand_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)#uniformcx=np.rand…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

数据增强之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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189755.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 5G核心网UPF是什么_3gpp 5g协议

    5G核心网UPF是什么_3gpp 5g协议目录文章目录目录UPF参考文档UPFUPF(…

    2022年8月3日
    7
  • Docker创建MySQL集装箱

    Docker创建MySQL集装箱

    2022年1月5日
    49
  • expect用法介绍

    expect用法介绍一、概念Expect是一个用来实现自动交互功能的软件套件。执行shell脚本,需要从终端得到输入时(如sshroot@192.168.1.2),Expect可以根据提示,模拟标准输入来实现交互脚本执行,使其以非交互的方式执行可以把shell和expect理解为两种不同的脚本语言,expect有独自的语法、变量二、ssh远程主机的方式2.1.简单方式,直接使用expect命令#!/bin/bash#登陆远程主机并查看主机名IP=”192.168.1.2″USERNAME=”root”P

    2025年8月23日
    0
  • 新视野大学英语(第三版)读写教程4答案

    新视野大学英语(第三版)读写教程4答案Unit1TextATextA.Languagefocus1.crumbled2.discern3.surpass4.shrewd5.conversion6.distort7.radiant8.Ingenious9.propositionTextA:LanguagefocusWordbuildingPractice1delicacybankruptcyac…

    2022年6月14日
    38
  • mysql字段默认值使用null还空字符串_mysql分割字符串split

    mysql字段默认值使用null还空字符串_mysql分割字符串split#字符串拼接concat(s1,s2);将表中last_name和first_name中的字符串拼接selectconcat(last_name,first_name)as姓名fromemployees;#只会修改last_name不会修改first_nameSELECTfirst_name,last_nameASfFROMemployees;#将两个列用逗号隔开并命名为o…

    2022年9月1日
    3
  • c++酒店管理系统源代码_c语言酒店管理系统实验报告

    c++酒店管理系统源代码_c语言酒店管理系统实验报告现今大多数宾馆所提供的服务样式都各式各样,规模大小也是各有不同,但是归总下来,不可或缺的两类模块还是顾客和工作人员。由于对宾馆行业内部没有很深刻的理解,此次系统设计包括数据库和功能模块都是根据网上收集到的材料和个人认知上,简单模仿和具体实现的。为满宾馆管理的实际需求,本系统主要实现以下功能:入住登记:登记所入住房间号码,登记顾客入住时间,退房时间,个人信息(身份证号,手机号,姓名)退房办理:输入已经入住的房间号,确认完毕即可退房。房间查询:管理员输入正确的密码后即可对房间状态查询,和具体入住信息查

    2022年9月24日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号