当前位置:网站首页>图像数据预处理
图像数据预处理
2022-07-07 23:11:00 【booze-J】
1.下载数据集
首先我们需要先到网上下载猫狗数据集:
猫狗分类数据集下载地址:https://pan.baidu.com/s/1i4SKqWH
密码:d8mt
2.数据集划分
刚开始下载的数据train和test都是猫和狗混合的图片,需要修改一下重新划分一下train和test中的猫和狗分别划分出来。文件结构如下:
|_image
|_train
|_dog
|_cat
|_test
|_dog
|_cat
由于训练时长的问题,这里只用到了2000张图片进行训练,1000图片进行验证。可以自行决定训练和测试数据集的大小。
3.数据预处理代码
代码运行平台为jupyter-notebook,文章中的代码块,也是按照jupyter-notebook中的划分顺序进行书写的,运行文章代码,直接分单元粘入到jupyter-notebook即可。
from keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img
- rotation_range是一个0~180的度数,用来指定随机选择图片的角度
- width_shift和height_shift用来指定水平和竖直方向随机移动的程度,这是两个0~1之间的比
- rescale值将在执行其他处理前乘到整个图像上,我们的图像在RGB通道都是0~255的整数,这样的操作可能使图像的值过高或过低,所以我们将这个值定为0~1之间的数
- shear_range是用来进行剪切变换的程度,参考剪切变换
- zoom_range用来进行随机的放大
- horizontal_flip随机的对图片进行水平翻转,这个参数适用于水平翻转不影响图片语义的时候
- fill_mode用来指定当需要进行像素填充,比如旋转、水平和竖直位移时,如何填充新出现的像素
datagen = ImageDataGenerator(
rotation_range=40, # 随机旋转角度
width_shift_range=0.2, # 随机水平平移
height_shift_range=0.2, # 随机竖直平移
rescale=1./255, # 数值归一化
shear_range=0.2, # 随机裁剪
zoom_range=0.2, # 随机放大
horizontal_flip=True, # 水平翻转
fill_mode="nearest" # 填充方式
)
这里我们以一张图片先来演示数据处理的效果:
# 载入图片
img = load_img("./image/train/cat/cat.1.jpg")
# 将图片转化为array数据格式
x = img_to_array(img)
# (280, 300, 3) = (H,W,channels)
print(x.shape)
# 给图片增加一个维度 加这个维度主要是因为训练的时候需要一个四维的图片
x = x.reshape((1,)+x.shape)
# (1, 280, 300, 3) = (batch_size,H,W,channels)
print(x.shape)
i = 0
# 生成21张图片
# flow随机生成图片 save_prefix为新生成名字的前缀
for batch in datagen.flow(x,batch_size=1,save_to_dir='temp',save_prefix="cat",save_format="jpeg"):
# 执行20次
i += 1
if i>20:
break
测试的图片:
代码运行结果:
可以看到这个数据增强的效果还是不错的哈!
边栏推荐
- 3 years of experience, can't you get 20K for the interview and test post? Such a hole?
- QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
- 炒股开户怎么最方便,手机上开户安全吗
- Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
- 【笔记】常见组合滤波电路
- Deep dive kotlin synergy (XXII): flow treatment
- Basic mode of service mesh
- Installation and configuration of sublime Text3
- How to insert highlighted code blocks in WPS and word
- After going to ByteDance, I learned that there are so many test engineers with an annual salary of 40W?
猜你喜欢

语义分割模型库segmentation_models_pytorch的详细使用介绍

Service Mesh介绍,Istio概述

国外众测之密码找回漏洞

RPA云电脑,让RPA开箱即用算力无限?

C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks

去了字节跳动,才知道年薪 40w 的测试工程师有这么多?

Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
![[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output](/img/79/f5cffe62d5d1e4a69b6143aef561d9.png)
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output

Lecture 1: the entry node of the link in the linked list
![Cause analysis and solution of too laggy page of [test interview questions]](/img/8d/3ca92ce5f9cdc85d52dbcd826e477d.jpg)
Cause analysis and solution of too laggy page of [test interview questions]
随机推荐
Qt不同类之间建立信号槽,并传递参数
接口测试要测试什么?
What has happened from server to cloud hosting?
22年秋招心得
Is it safe to open an account on the official website of Huatai Securities?
手机上炒股安全么?
New library online | information data of Chinese journalists
What is load balancing? How does DNS achieve load balancing?
【测试面试题】页面很卡的原因分析及解决方案
Cve-2022-28346: Django SQL injection vulnerability
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
ABAP ALV LVC template
詹姆斯·格雷克《信息简史》读后感记录
玩转Sonar
New library launched | cnopendata China Time-honored enterprise directory
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
C# 泛型及性能比较
赞!idea 如何单窗口打开多个项目?
股票开户免费办理佣金最低的券商,手机上开户安全吗
牛客基础语法必刷100题之基本类型