Augmentation

原图:

Flipping 翻转



Grayscale

Equalize 均衡直方图

Posterize 减少颜色通道位数

Cropping

Rotation

Translation

Noise injection



Hue 色调

Brightness

Saturation

Contrast

Erasing 抹去

Augmix

代码

config.json:


{
"Crop": {
"size": [100, 200]
},
"Rotate": {
"degrees": 20
},
"Translate": {
"translate": [0.3, 0.3]
},
"Scale": {
"scale": [0.7, 1.3]
},
"Shear": {
"shear": [10, 100, 10, 100]
},
"Hflip": {},
"Vflip": {},
"Equalize": {
"mask": null
},
"Posterize": {
"bits" : 2
},
"Grayscale": {
"num_output_channels": 1
},
"Normalnoise": {
"loc": 0.0,
"scale": 0.1
},
"Uniformnoise": {
"alpha": 0.2
},
"Contrast": {
"contrast": 0.8
},
"Brightness": {
"brightness": 0.7
},
"Saturation": {
"saturation": 0.7
},
"Hue": {
"hue": 0.5
},
"Erase": {
"value": 100
} }

"""
ops.py
""" import torch
import torchvision.transforms.functional as F
import PIL.ImageOps
import random
from torchvision import transforms
import scipy.stats
from collections.abc import Iterable def special(cls, name, *args, **kwargs):
class Newclass(cls):
def __init__(self, *newargs, **newkwargs):
newargs += args
newkwargs.update(kwargs)
super(Newclass, self).__init__(*newargs, **newkwargs)
Newclass.__name__ = name
return Newclass class Hflip:
"""
水平翻转
"""
def __call__(self, img):
return F.hflip(img) class Vflip:
"""
上下翻转
"""
def __call__(self, img):
return F.vflip(img) class Equalize:
"""
均衡图像直方图
"""
def __init__(self, mask):
self.mask = mask def __call__(self, img):
return PIL.ImageOps.equalize(img, self.mask) class Posterize:
"""
减少每个颜色通道的位数
"""
def __init__(self, bits):
self.bits = bits def __call__(self, img):
return PIL.ImageOps.posterize(img, self.bits) class Grayscale:
def __init__(self, num_output_channels=1):
self.num_output_channels = num_output_channels def __call__(self, img):
return F.to_grayscale(img, self.num_output_channels) class Normalnoise:
def __init__(self, loc, scale):
self.loc = loc
self.scale = scale def __call__(self, img):
img = F.to_tensor(img)
img += (torch.randn_like(img) + self.loc) * self.scale
img = torch.clamp(img, 0., 1.)
return F.to_pil_image(img) class Uniformnoise:
def __init__(self, alpha):
if alpha < 0 or alpha > 1:
raise ValueError("alpha should be in [0, 1]...")
self.alpha = alpha def __call__(self, img):
img = F.to_tensor(img)
img += torch.rand_like(img) * self.alpha
img = torch.clamp(img, 0., 1.)
return F.to_pil_image(img) class Erase: def __init__(self, value):
self.value = value def __call__(self, img):
img = F.to_tensor(img)
H, W = img.size()[-2:]
lui = torch.randint(0, H, (1,)).item()
luj = torch.randint(0, W, (1,)).item()
rbi = torch.randint(lui, H, (1,)).item()
rbj = torch.randint(luj, W, (1,)).item()
h = rbj - luj
w = rbi - lui return F.to_pil_image(F.erase(img, lui, luj, h, w, 0)) class Perspective: def __init__(self, startpoints, endpoints, interpolation=3):
self.startpoints = startpoints
self.endpoints = endpoints
self.interpolation = interpolation def __call__(self, img):
return F.perspective(img, self.startpoints,
self.endpoints, self.interpolation) Translate = special(transforms.RandomAffine, "Translate", degrees = 0)
Scale = special(transforms.RandomAffine, "Scale", degrees = 0)
Shear = special(transforms.RandomAffine, "Shear", degrees=0)
Rotate = special(transforms.RandomAffine, "Rotate")
Brightness = special(transforms.ColorJitter, "Brightness")
Contrast = special(transforms.ColorJitter, "Contrast")
Saturation = special(transforms.ColorJitter, "Saturation")
Hue = special(transforms.ColorJitter, "Hue")
Crop = transforms.RandomCrop class Augmix: def __init__(self, ops, k=3, alpha=1, beta=1):
self.ops = ops
self.k = k
if isinstance(alpha, Iterable):
self.alpha = alpha
else:
self.alpha = [alpha] * k
self.beta = beta def get_params(self):
op1, op2, op3 = random.sample(self.ops, 3)
op12 = transforms.Compose([op1, op2])
op123 = transforms.Compose([op1, op2, op3])
return random.sample([op1, op12, op123], 1)[0] def __call__(self, img):
weights = scipy.stats.dirichlet.rvs(self.alpha)[0]
img_tensor = F.to_tensor(img)
xaug = torch.zeros_like(img_tensor)
for i in range(self.k):
opschain = self.get_params()
temp = weights[i] * F.to_tensor(opschain(img))
xaug += temp
m = scipy.stats.beta.rvs(self.beta, self.beta, size=1)[0]
new_img = m * img_tensor + (1 - m) * xaug
return F.to_pil_image(new_img)

最新文章

  1. iOS- -安装cocopods
  2. 尽量多的以 const/enum/inline 替代 #define
  3. [kuangbin带你飞]专题二十 斜率DP
  4. 【C++ in Qt5】一个简单的通讯录程序,支持文件存取
  5. 鸟哥Linux学习笔记03
  6. SQL-59 按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推。
  7. 《剑指offer》 树的子结构
  8. 【转】escape,encodeURI,encodeURIComponent有什么区别?
  9. 【转】ssh服务器启动和客户端常用操作
  10. Struts2中 radio标签的详细使用方法
  11. 深入了解mitmproxy(二)
  12. C# int转string 每三位加一个逗号
  13. 读/写锁的实现和应用(高并发状态下的map实现)
  14. 【Unity笔记】给UGUI元素添加交互性
  15. bzoj 2818 gcd 线性欧拉函数
  16. SQL Server 2008内存及I/O性能监控
  17. 判断序列是否为二叉排序树的后序遍历 python
  18. java/eclipse/myeclipse建立.properties文件的方法
  19. [新手教程]windows 2003 php环境搭建详细教程(转)
  20. Java访问控制权限

热门文章

  1. 论 Erda 的安全之道
  2. acre, across
  3. 关于redis HSCAN count参数不生效的问题
  4. HTTP初识
  5. vim编码设置(转)
  6. ORACEL 创建DIRECTORY
  7. 【面试】【Linux】【Web】基础概念
  8. 【Python】【Module】re
  9. 【Linux】【Services】【Disks】zfs
  10. SSO(单点登录)示例