大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/189755.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...