当前位置:网站首页>【TensorFlow&PyTorch】图像数据增强API
【TensorFlow&PyTorch】图像数据增强API
2022-07-26 03:06:00 【折途】
前言:
在进行深度学习训练时,遇到训练效果较差、训练集数量小、有过拟合趋向时可以选择加大数据集数量来优化训练模型,但是大多数情况下,增加数据集数量所花费的时间精力是巨大的,所以我们更常用的方法是对现有的数据集进行数据增强。不如实实在在增加数据集数量,但是还是有一定的效果的,性价比高。(只要加几行代码)
TensorFlow的API在image下:(我用的2.0版本,不同的版本可能API不同,但是基本都可以在 iamge下找到)
from tensorflow import imagePyTorch的API在torchvision的transforms下:torchvision — Torchvision 0.13 documentation
https://pytorch.org/vision/stable/index.html
from torchvision import transforms以下列举几个本人认为常用的方法.
要详细的API点击上面链接去官方文档查看.
随机翻转图片:
TensorFlow:
随机上下翻转:
output=image.random_flip_up_down(input)
随机左右翻转:
output=image.random_flip_left_right(input)
PyTorch:
随机上下翻转:
layer=transform.RandomHorizontalFlip( )
随机左右翻转:
layer=transform.RandomVerticalFlip( )
随机旋转图像|n°|:
layer=transform.RandomRotation(n)
注意这里得到的是层(layer),还需要把图片塞到层里才能得到结果.而TensorFlow中的例子是直接可以得到结果.
例:output=layer(input)
随机裁剪图像:
TensorFlow:
将input随机裁剪成shape大小:
output=image.random_crop(input,[shape])
将input随机裁剪,中心部分保留central_fraction(0~1).例如central_fraction=0.5,则中心部分保留一半.
output=image.central_crop(input, central_fraction)
PyTorch:
得到将传入图像裁剪成shape的层:
layer=transforms.RandomCrop(shape)
得到将传入图像保留中心部分并裁剪成shape的层:
layer=transform.CenterCrop(shape)
正则化:
严格来说正则化不是数据增强,但是它可以让图像更加利于训练.以下举例仅限三通道RGB图像.
将图像的像素值变成均值为mean,方差为std的正态分布.
以下的mean和std是前人计算好的最优值(也许?)
TensorFlow:
TensorFlow貌似没有接口,所以得自己写:
mean=tf.constant([0.485,0,456,0.406]) std=tf.constant([0.229,0.224,0.225]) output=(input-mean)/std
PyTorch:
layer=transform.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
结尾:
TensorFlow的image下有很多处理图像的API(不局限于数据增强),感兴趣可以去看看.(英语不好的我表示看得很累)
由于以上PyTorch得到的是层,所以可以封装进一个容器里:
transform.Compose([
layer1,
layer2,
layer3,
………………
transform.ToTensor()#最后记得加上这个层,将图像转成tensorf才能被pytorch识别
])边栏推荐
- FPGA_Vivado软件初次使用流程_超详细
- LeetCode·每日一题·剑指 Offer || 115.重建序列·拓扑排序
- Teach you to rely on management hand in hand
- How to effectively prevent others from wearing the homepage snapshot of the website
- FPGA_ Initial use process of vivado software_ Ultra detailed
- Machine learning foundation plan 0-2: what is machine learning? What does it have to do with AI?
- (九)属性自省
- Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
- An article allows you to understand the relevance of cloud native containerization
- 一篇文章让你理解 云原生 容器化相关
猜你喜欢

Parallelloopbody in opencv

Difference between soft link and hard link

重装Win7系统如何进行?

Cycle and branch (I)
![[steering wheel] tool improvement: common shortcut key set of sublime text 4](/img/51/fe95e95ccd5cd6406f3b49e3bdba3f.png)
[steering wheel] tool improvement: common shortcut key set of sublime text 4

图像识别(六)| 激活函数

Arthas' dynamic load class (retransform)

STM32 - PWM learning notes

JVM memory model parsing

Information system project managers must recite the core examination site (50). The contract content is not clearly stipulated
随机推荐
How to close the case prompt icon of win11? Closing method of win11 case prompt Icon
(pc+wap) dream weaving template vegetable and fruit websites
Neo4j 导入csv数据报错:Neo4j load csv error : Couldn‘t load the external resource
AMD64 (x86_64) architecture ABI document:
Cycle and branch (I)
The LAAS protocol elephant of defi 2.0 is the key to revitalizing the development of defi track
[steering wheel] use the 60 + shortcut keys of idea to share with you, in order to improve efficiency (live template & postfix completion)
VR panoramic shooting and production of business center helps businesses effectively attract people
The source of everything, the choice of code branching strategy
Multithreaded programming
Image recognition (VI) | activation function
Opening method of win11 microphone permission
Jenkins' study notes are detailed
Jsd-2204-cool shark Mall (Management Commodity module) -day02
Summary of Huawei virtualization fusioncompute knowledge points
手把手教你依赖管理
YOLOv3: An Incremental Improvement
Arthas view the source code of the loaded class (JAD)
[NOIP2001 普及组]装箱问题
26 points that must be paid attention to for stability test
https://tensorflow.google.cn/versions/r2.0/api_docs/python/tf/image