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


相关推荐

  • goland2020激活码【在线破解激活】

    goland2020激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日
    39
  • thinkCMF—-列表页跳转

    thinkCMF—-列表页跳转

    2021年6月11日
    88
  • 2019年5月25更新——“运动世界校园”软件破解

    一、软件工作原理软件和后台使用API进行交互,在早期版本中API没有鉴权导致可以通过伪造请求来完成跑步,但是后期API迭代了好几个版本,目前认为现在的请求需要间隔一个合理的时间差(跑步时间),而且需要计算一个token用来鉴权,避免了伪造请求。 跑步开始和跑步完成时,会记录时间、设备序列号、IMEI等敏感信息,上传服务器。此操作用来记录手机设备唯一性,用来检测频繁换手机,一台设备上登陆不同账…

    2022年4月7日
    196
  • 数据库恢复的三种方式

    数据库恢复的三种方式数据库恢复的三种方式

    2022年4月25日
    75
  • 华为交换机vlan配置教程

    华为交换机vlan配置教程Sys  \\进入系统视图Dhcpenable \\全局开启dhcp功能Vlanbacth234 \\批量创建vlan 234Intvlanif2 \\进入vlan 2Ipadd192.168.xxx.1 255.255.255.0 \\为vlan2分配IP地址及子网掩码Dhcpselectglobal\\dhcp选择全局开启Q \…

    2022年9月15日
    0
  • java 日志时间错误

    java时区错误解决方法问题参考链接电脑上所有java应用、项目时间都不对。核心业务系统启动后日志时间和当前系统时间差11个小时30分钟,电脑用的是云桌面系统有严格的权限控制,找相关人和同事弄了几次没好;都知道是时区问题,但没注意到系统桌面右下角的提示。最后解决方法很简单,先说解决方法。(出现问题的主机是无法连接公网的,文件也无法外传,图片都是照片;)解决方法在windows…

    2022年4月9日
    125

发表回复

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

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