数据增强之Gridmask

网友投稿 660 2022-11-23

数据增强之Gridmask

数据增强之Gridmask

paper: ​​code: ​​augmentation)方法,指出当前方法有三类:spatial transformation, color distortion, 以及 information dropping。本文提出的方法属于 information dropping,作者指出,对于此类方法,避免过度删除或保持连续区域是核心问题:一方面,过度删除区域将造成完整目标被删除或者上下文信息缺失,因此,剩下的区域不足以表达目标信息,会成为noisy data。另一方面,保留过多区域,将会使得目标不受影响(untouched),会影响网络的鲁棒性。

作者重点介绍了 Cutout 和 HaS 方法。Cutout方法只删除图像中的一块连续区域,因此,容易出现删除掉整个目标,或者一点目标也没有删除的情况;HaS方法把图像划分为若干小块的区域,然后随机删除,但仍然会出现和 Cutout 相同的问题。下图展示了 GridMask 方法与当前方法的对比。

方法

实验结果

在ImageNet-1K图像分类任务上,Cutout对ResNet50的提升为0.6%,HaS的提升为0.7%,AutoAugement提升为1.1%,相比而言,GridMask的提升为1.4%。作者还在CIFAR10数据集上进行了实验,这里不再详述。

代码

import torchimport torch.nn as nnimport numpy as npfrom PIL import Imageimport pdbclass Grid(object): def __init__(self, use_h, use_w, rotate=1, offset=True, ratio=0.005, mode=0, prob=1.): self.use_h = use_h self.use_w = use_w self.rotate = rotate self.offset = offset self.ratio = ratio self.mode = mode self.st_prob = prob self.prob = prob def set_prob(self, epoch, max_epoch): self.prob = self.st_prob * epoch / max_epoch def __call__(self, img): if np.random.rand() > self.prob: return img h = img.size(1) w = img.size(2) self.d1 = 2 self.d2 = min(h, w) hh = int(1.5 * h) ww = int(1.5 * w) d = np.random.randint(self.d1, self.d2) # d = self.d # self.l = int(d*self.ratio+0.5) if self.ratio == 1: self.l = np.random.randint(1, d) else: self.l = min(max(int(d * self.ratio + 0.5), 1), d - 1) mask = np.ones((hh, ww), np.float32) st_h = np.random.randint(d) st_w = np.random.randint(d) if self.use_h: for i in range(hh // d): s = d * i + st_h t = min(s + self.l, hh) mask[s:t, :] *= 0 if self.use_w: for i in range(ww // d): s = d * i + st_w t = min(s + self.l, ww) mask[:, s:t] *= 0 r = np.random.randint(self.rotate) mask = Image.fromarray(np.uint8(mask)) mask = mask.rotate(r) mask = np.asarray(mask) # mask = 1*(np.random.randint(0,3,[hh,ww])>0) mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) // 2 + w] mask = torch.from_numpy(mask).float() if self.mode == 1: mask = 1 - mask mask = mask.expand_as(img) if self.offset: offset = torch.from_numpy(2 * (np.random.rand(h, w) - 0.5)).float() offset = (1 - mask) * offset img = img * mask + offset else: img = img * mask return imgclass GridMask(nn.Module): def __init__(self, use_h, use_w, rotate=1, offset=False, ratio=0.5, mode=0, prob=1.): super(GridMask, self).__init__() self.use_h = use_h self.use_w = use_w self.rotate = rotate self.offset = offset self.ratio = ratio self.mode = mode self.st_prob = prob def set_prob(self, epoch, max_epoch): self.prob = self.st_prob * epoch / max_epoch # + 1.#0.5 def forward(self, x): if np.random.rand() > self.prob or not self.training: return x n, c, h, w = x.size() x = x.view(-1, h, w) hh = int(1.5 * h) ww = int(1.5 * w) d = np.random.randint(2, h) # d = self.d # self.l = int(d*self.ratio+0.5) self.l = min(max(int(d * self.ratio + 0.5), 1), d - 1) mask = np.ones((hh, ww), np.float32) st_h = np.random.randint(d) st_w = np.random.randint(d) if self.use_h: for i in range(hh // d): s = d * i + st_h t = min(s + self.l, hh) mask[s:t, :] *= 0 if self.use_w: for i in range(ww // d): s = d * i + st_w t = min(s + self.l, ww) mask[:, s:t] *= 0 r = np.random.randint(self.rotate) mask = Image.fromarray(np.uint8(mask)) mask = mask.rotate(r) mask = np.asarray(mask) # mask = 1*(np.random.randint(0,3,[hh,ww])>0) mask = mask[(hh - h) // 2:(hh - h) // 2 + h, (ww - w) // 2:(ww - w) // 2 + w] mask = torch.from_numpy(mask).float().cuda() if self.mode == 1: mask = 1 - mask mask = mask.expand_as(x) if self.offset: offset = torch.from_numpy(2 * (np.random.rand(h, w) - 0.5)).float().cuda() x = x * mask + offset * (1 - mask) else: x = x * mask return x.view(n, c, h, w)

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:域适应论文简读
下一篇:数据增强之RandAugment
相关文章

 发表评论

暂时没有评论,来抢沙发吧~