jjzjj

神经网络数据增强transforms的相关操作(持续更新)

燃烧吧哥们 2023-08-18 原文

transforms的相关操作(Pytorch)


日志:
        2022.6.18:加入transforms.TenCrop()

一、图像的相关变化

1、格式转换

(1)transforms.ToTensor()

可将PIL格式、数组格式转换为tensor格式

img_path = "E:\expression_recognition\\2.jpg"
img = Image.open(img_path)  # #数组类型PIL类型都可
a1 = transforms.ToTensor()
img_tensor = (img)

(2)transforms.ToPILImage()

tensor格式 或者 数组类型的数据转换为 PIL 格式

img_path="E:\expression_recognition\\1.jpg"
img = Image.open(img_path)
img_array = numpy.array(img)
a1 = transforms.ToTensor()
img_tensor = a1(img)

# 1、数组转换为PIL格式
a2 = transforms.ToPILImage()
img_array = numpy.uint8(img_array)  # 满足类型需求
img_PIL = a2(img_array)

# 2、tensor转换为PIL格式
img_tensor = img_tensor.float()  # 满足类型需求
img_PIL = a2(img_tensor)

(3)transforms.Normalize()

注意该操作只能对tensor格式进行操作,因此放transforms.ToTensor()之后
对图像进行标准化处理

括号中的值transforms.Resize()
mean,std以三通道为例:[0.5,0.5,0.5],[0.5,0.5,0.5]

2、图像大小和颜色变换

(1)transforms.Resize()

可对tensor类型,PIL类型进行缩放,不可对数组类型进行缩放

括号中的值transforms.Resize()
W图片缩放为(W*W)
(H,W)图片缩放为(H*W)
a = transforms.Resize([256,512])   # [H,W]
img_resize = a2(img)
img_resize.show()

(2)transforms.ColorJitter()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

括号中的值transforms.ColorJitter(brightness, contrast, saturation, hue)
brightness亮度
contrast对比度
saturation饱和度
hue色调
a3 = transforms.ColorJitter(brightness=0.5, contrast=0.5, saturation=0.5, hue=0.5)
img_colorjitter = a3(img)
img_colorjitter.show()

左端原图,右端变换完的图

(3)transforms.Grayscale()和transforms.RandomGrayscale()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

功能
transforms.Grayscale()将图像变为灰度图像
transforms.RandomGrayscale()将图像随机变为灰度图像
a4 = transforms.Grayscale(num_output_channels=1)
img_grayscale = a4(img)
img_grayscale.show()
a5 = transforms.RandomGrayscale(p=0.1)
img_randomgrayscale = a5(img)
img_randomgrayscale.show()

3、图像的裁剪与旋转

(1)随机裁剪transforms.RandomCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
随机裁剪有很多参数,但是目前用到的只有这一种参数

参数功能
size按照所给的size大小进行裁剪,size可以是(h,w)或直接是w
import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

a6 = transforms.RandomResizedCrop((124,124))
a7 = transforms.RandomResizedCrop(256)
img_randomresizedcrop1 = a6(img)
img_randomresizedcrop2 = a7(img)
plt.subplot(1,3,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,3,2),plt.imshow(img_randomresizedcrop1),plt.title("转换后的图1")
plt.subplot(1,3,3),plt.imshow(img_randomresizedcrop2),plt.title("转换后的图2")
plt.show()

(2)中心裁剪transforms.CenterCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
与随机裁剪基本一样,只不过是从中心进行裁剪

参数功能
size按照所给的size大小进行中心裁剪,size可以是(h,w)或直接是w

(3)随机长宽比裁剪transforms.RandomResizedCrop()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改
主要用到以下两个参数:

参数功能
size输出的分辨率大小
scale随机crop的大小区间,如scale=(0.8, 1.0)(前面是h的倍数,后面是w的倍数),表示随机crop出来的图片会在的0.8倍至1倍之间
import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

a6 = transforms.RandomResizedCrop((64,128), scale=(0.08, 1.0))
img_randomresizedcrop = a6(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomresizedcrop),plt.title("转换后的图")
plt.show()

(4)10次裁剪transforms.TenCrop()

裁剪图片的中间部分和四个角并进行翻转得到四张图片(默认水平翻转)

参数功能
size裁剪图片的大小
vertical_flip是否垂直翻转

默认采用水平翻转,第一张为原图

a7 = transforms.TenCrop(224)
img1 = a7(img)
plt.figure(figsize=(20,18))
plt.subplot(3,4,1),plt.imshow(img),plt.title(1)
for i in range(0,10):
    plt.subplot(3,4,i+2),plt.imshow(img1[i]),plt.title(str(i+1))
plt.show()

采用垂直翻转,第一张为原图

a8 = transforms.TenCrop(224,vertical_flip=True)
img2 = a8(img)
plt.figure(figsize=(20,18))
plt.subplot(3,4,1),plt.imshow(img),plt.title(0)
for i in range(0,10):
    plt.subplot(3,4,i+2),plt.imshow(img2[i]),plt.title(str(i+1))
plt.show()

(5)概transforms.RandomHorizontalFlip()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

a7 = transforms.RandomHorizontalFlip(p=0.5)
img_randomhorizontalflip = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomhorizontalflip),plt.title("翻转后的图")
plt.show()
参数功能
p随机水平翻转的概率

(6)概率垂直翻转transforms.RandomVerticalFlip()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

参数功能
p随机垂直翻转的概率
import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

a7 = transforms.RandomVerticalFlip(p=0.5)
img_randomverticalflip = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomverticalflip),plt.title("翻转后的图")
plt.show()

(7)随机旋转:transforms.RandomRotation()

可对tensor类型,PIL类型进行修改,不可对数组类型进行修改

参数功能
70旋转范围为:(-70,70)
(30,60)旋转范围为:(30,60)
import matplotlib.pyplot as plt
# 解决中文显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

a7 = transforms.RandomRotation((30,50))
img_randomrotation = a7(img)
plt.subplot(1,2,1),plt.imshow(img),plt.title("原图")
plt.subplot(1,2,2),plt.imshow(img_randomrotation),plt.title("随机旋转后的图")
plt.show()

4、transforms.Compose([])

transforms.Compose是将上述数据变换包装起来依次执行。另外有不对的地方望指出。

有关神经网络数据增强transforms的相关操作(持续更新)的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 使用 rails 4 设计而不更新用户 - 2

    我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它​​不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数

  4. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  5. ruby - Ruby 有 `Pair` 数据类型吗? - 2

    有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳

  6. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  7. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  8. 世界前沿3D开发引擎HOOPS全面讲解——集3D数据读取、3D图形渲染、3D数据发布于一体的全新3D应用开发工具 - 2

    无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD

  9. FOHEART H1数据手套驱动Optitrack光学动捕双手运动(Unity3D) - 2

    本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01  客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02  数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit

  10. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

随机推荐