当前位置:网站首页>深度学习图像数据增强
深度学习图像数据增强
2022-06-21 18:28:00 【是暮涯啊】
作为cv的基础,数据增扩是很重要的一环。一般来说主要有以下几种:1、通过openCV操作 2、使用torchvision.transform 3、使用torchvision.transform.function 4、使用nvidia.dali 5、albumentations库。对应的数据也一般可以分为1、单图处理,如分类。2、同尺寸多个数据处理,如分割,去噪。3、不同尺寸多个数据处理,如超分。下面将按照方法简单总结一下。
1、数据增扩类型
几何变换
旋转,缩放,翻转,裁剪,平移,仿射变换
色彩空间
亮度,对比度,饱和度,颜色空间转换,色彩调整,gamma变换
其他
高斯噪声,椒盐噪声
随机擦除,mixup,图像混合,Mosaic,copy-paste等
2、 openCV操作
该方法灵活性最高,可以对单图或者配对数据进行操作。
import cv2
pic = cv2.imread(“img.jpg”) #读入图片,获得[h,w,c]数组
pic = pic[startH:endH, startW:endW] # 裁剪
pic = pic[:,:,::-1] # 通道变换
pic = cv2.resize(pic, (neww,newh)) # resize
pic = cv2.flip(pic, 1) #水平翻转
pic = cv2.flip(pic, 0) #垂直翻转
# 旋转 首先获得旋转矩阵,然后旋转
rotationMatrix = cv2.getRotationMatrix2D((width/2, height/2), 45, .5)
pic = cv2.warpAffine(pic, rotationMatrix, (width, height))
# addWeighted参数(图1,图1权重,图2,图2权重,亮度,输出)
pic = cv2.addWeighted(img1,0.5, img2,0.5) # mixup
# 对比度, alpha1设置控制高低对比度。
pic = cv2.addWeighted(pic, alpha1, np.zeros(img.shape, img.dtype), 0, 0)
# 对比度, 通过brightness调整,设为整数
pic = cv2.addWeighted(pic, contrast, pic, 0, brightness)
# 高斯模糊
pic = cv2.GaussianBlur(pic, (7, 7), 3)
pic[startH:endH, startW:endW] = 0 # 擦除
pic[startH:endH, startW:endW] = src_pic # 复制粘贴
其他擦除,混合,马赛克数据增强,复制粘贴等均可通过设置画布,然后替换值获得。
一般在处理同尺寸多图,不同尺寸多图时可通过设置不同函数的参数得到。
参考一, 参考二
3、torchvision.transform
对tensor进行处理,一般适用于单图处理的情况,常与PIL函数共同使用。这个函数看官网介绍最清楚了,或者推荐这个展示例子,以下只选取其中几个函数展示。
from PIL import Image
pic = Image.open('img.png') # (C, H, W)
# 获取宽高,注意此时size函数不返回c通道数
w, h = pic.size
transforms.Compose([ # 组合多种变换
transforms.CenterCrop(10), # 中心裁剪
transforms.Resize(size), # resize
# 随机更改图像的亮度、对比度、饱和度和色调
transforms.ColorJitter(brightness, contrast, saturation, hue),
transforms.Grayscale(num_output_channels), # 转灰度图
transforms.RandomCrop(size), # 随机裁剪
transforms.RandomHorizontalFlip(p=0.5), # 随机水平翻转
transforms.RandomVerticalFlip(p=0.5), # 随机垂直翻转
transforms.RandomRotation(degrees), # 随机旋转(degrees,-degress)
transforms.GaussianBlur(ks, sigma), # 高斯模糊
# 转tensor,会自动归一化
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # 只对tensor处理
])
该方法在处理同尺寸多图时,部分操作可以将两张图concat完成,也可以自己写个随机函数处理,如
import random
def randflip(img1, img2, threshold):
if random.random() > threshold: # 生成一个随机数[0,1),水平翻转
img1 = transforms.RandomHorizontalFlip(p=1)(img1)
img2 = transforms.RandomHorizontalFlip(p=1)(img2)
if random.random() > threshold: # 生成一个随机数[0,1),垂直翻转
img1 = transforms.RandomVerticalFlip(p=1)(img1)
img2 = transforms.RandomVerticalFlip(p=1)(img2)
return img1, img2
但灵活性还是很差,不同尺寸的多图处理,或者很多官方没有的增扩操作不能实现。
参考一
4、torchvision.transform.function
作为transform更底层一点的函数,他不包含具体随机数,具备更灵活的特点。同样是搭配PIL使用的。在实现transform基础功能同时,可以处理不同尺寸的多图。
TvF使用方法有二:1)作为可拓展的torchvision.transform功能,构建transform类,并可集成在compose里。官方示例如下:
import torchvision.transforms.functional as TF
import random
class MyRotationTransform:
"""Rotate by one of the given angles."""
def __init__(self, angles):
self.angles = angles
def __call__(self, x):
angle = random.choice(self.angles)
return TF.rotate(x, angle)
rotation_transform = MyRotationTransform(angles=[-30, -15, 0, 15, 30])
2)作为自定义的增扩函数。此处缩写为TvF是避免与F(torch.nn.functional)或TF(tensorflow)混淆。
from PIL import Image
import torchvision.transforms.functional as TvF
img = Image.open('img.jpg')
c, h, w = TvF.get_dimensions(img) # 返回维度
img = TvF.crop(img, top, left, height, width) # 裁剪
img = TvF.erase(img, i, j, h, w, v[, inplace]) # 擦除,用给定v替代
img = TvF.rotate(img, angle) # 通过给定角度旋转
img = TvF.hflip(img) # 水平翻转
img = TvF.vflip(img) # 垂直翻转
img = TvF.adjust_brightness(img, brightness_factor) # 亮度调整
img = TvF.adjust_contrast(image,contrast_factor) # 对比度调整
img = TvF.adjust_gamma(img, gamma) # gamma变换
img = TvF.gaussian_blur(image, ks, sigma) # 高斯模糊
img = TvF.normalize(img, mean, std) # 标准化
总之还有很多函数可以查看官方文档,TvF在应对大部分增扩场景都能满足,比如尺寸不同的图像对进行crop,只要把HR的参数减半就可以应用到LR中。但有些增扩方法还是不如cv2灵活,比如rgb通道变换,Mosaic等我目前还不明确怎么通过TvF实现。
函数源代码
部分函数效果可视化
5、NVIDIA DALI
DALI出奇迹,是NVIDIA最近主推的数据增强方式。可以直接在GPU处理,显著提高数据增强速度。
官方文档
码住,以后熟练了再更。
6、albumentations
基于OpenCV的快速训练数据增强库,拥有非常简单且强大的可以用于多种任务(分割、检测)的接口,易于定制且添加其他框架非常方便。
官方示例
来源
边栏推荐
- [icml2022] ctrlformer: learn the transferable state representation of visual control through the transformer
- gorm数据库是否需要设置外键
- Daily development of common tools to improve efficiency
- ArrayList源码解析
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle
- Assembly language greedy snake and Tetris dual task design implementation details (III) -- Tetris detailed design
- Resttemplate multiple authentication information authorization
- Gradle下载与安装配置
- 理财产品如果过了开放日期怎么赎回?
- Recycleview lazy load failure (II)
猜你喜欢
![Dynamic programming [1] (knapsack problem)](/img/a6/3f60aa162b0597cf91a05bbc2d2b53.png)
Dynamic programming [1] (knapsack problem)

MFC interface library bcgcontrolbar v33.0 - Desktop alert window, grid control upgrade

Forwarding to remind metamask how to deal with the potential private key disclosure of the expansion program

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(二)——贪吃蛇详细设计

How to simulate a request or modify a requested domain name in Chrome browser

How to query the data in MySQL

mysql增加的语句是什么

如何查询mysql中所有表

Kubernetes migration of persistent volumes across storageclasses Complete Guide

Startup mode of Jupiter notebook and related problems
随机推荐
汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计
Dynamic programming [1] (knapsack problem)
mysql与oracle有没有区别
剑指 Offer II 029. 排序的循环链表
研报精选 | 麦肯锡《中国数字创新未来》趋势解读
1156 Sexy Primes
Envi classic annotation object how to recall modification and deletion of element legend scale added
07_ Knowledge that can change your perception
SD集训6.21总结
谷粒学院P40~43
NetCore3.1 ping网络是否畅通及获取服务器Cpu、内存使用率
mysql如何对列求和
QT creator 7.0 frequently asked questions and common usage
Gradle download and installation configuration
新手使用APICloud可视化开发搭建商城主页
Simple use of JS
SD6.20集训总结
Resttemplate multiple authentication information authorization
Inno setup change installation path learning
金鱼哥RHCA回忆录:DO447Ansible Tower导航