数据增强之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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java WebService 客户端写法[通俗易懂]

    Java WebService 客户端写法[通俗易懂]这篇文章的前提是已经服务器端已经存在。变成工具:1.MyEclipse8.62.Tomcat7.03.JDK1.64.实现WebService服务的插件有很多,我选择的XFire我服务器端的WebServiceURL为:http://localhost/Homepage/services/DataInfoService?wsdl使用MyEclip

    2022年8月31日
    3
  • Jlink或者stlink用于SWD接口下载程序「建议收藏」

    Jlink或者stlink用于SWD接口下载程序「建议收藏」最近要使用stm32f103c8t6最小系统板,直接ISP串口下载程序太麻烦,就想着使用swd接口来调试。结果:通过SWD接口下载程序成功,但调试失败,还不知原因,会的的人麻烦交流一下。SWD接口:3.3VDIO(数据)CLK(时钟)GND1.首先声明jlink和stlink都有jtag和swd调试功能。jlink接口如下:如图,我使用的就是VCC…

    2022年4月25日
    96
  • QT QList介绍及应用

    QT QList介绍及应用QList是目前最常用的容器类,它存储了给定类型的值的一个列表,而这些值可以通过索引访问。QList使用数组来实现,以确保进行快速的基于索引的访问。使用QList::append和QList::prepend在列表的两端进行添加项目。使用QList::insert()在列表的中间插入项目。实例:#include<QCoreApplication>#include<Q…

    2025年6月14日
    1
  • Laravel渴求式加载(比较容易理解理解load与with关系)

    Laravel渴求式加载(比较容易理解理解load与with关系)

    2021年11月8日
    43
  • C++二维vector初始化

    C++二维vector初始化初始化一个二维vector,行M,列N(行列数确定且含有初始值)://初始化一个二维的matrix,行M,列N,且值为0vector<vector<int>>matrix(M,vector<int>(N));//等价于下面的vector<vector<int>>matrix(M);for(inti=0;i<M;i++){matrix[i].resize(N);}//等价于下面的vector&l

    2022年9月17日
    3
  • C++ int与string的相互转换(含源码实现)

    C++ int与string的相互转换(含源码实现)一、int转换成stringⅠ、to_string函数c++11标准增加了全局函数std::to_string:stringto_string(intval);stringto_str

    2022年7月3日
    21

发表回复

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

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