TensorFlow2实战-系列教程4:数据增强:keras工具包/Data Augmentation

本文主要是介绍TensorFlow2实战-系列教程4:数据增强:keras工具包/Data Augmentation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

🧡💛💚TensorFlow2实战-系列教程 总目录

有任何问题欢迎在下面留言
本篇文章的代码运行界面均在Jupyter Notebook中进行
本篇文章配套的代码资源已经上传

对于图像数据,将其进行翻转、放缩、平移、旋转操作就可以得到一组新的数据:
在这里插入图片描述

1、展示输入输出

import matplotlib.pyplot as plt
from PIL import Image
%matplotlib inline
from keras.preprocessing import image
import keras.backend as K
import os
import glob
import numpy as np
def print_result(path):name_list = glob.glob(path)fig = plt.figure(figsize=(12,16))for i in range(3):img = Image.open(name_list[i])sub_img = fig.add_subplot(131+i)sub_img.imshow(img)
img_path = './img/superman/*'
in_path = './img/'
out_path = './output/'
name_list = glob.glob(img_path)
print(name_list)
print_result(img_path)
  1. img_path 就是存放3张图像数据的路径,in_path 、out_path 暂时没用到
  2. name_list 查看一下三张数据的路径字符信息
  3. print_result就是一个专门用来打印3张图像的函数

打印结果:

[‘./img/superman\00000008.jpg’,
‘./img/superman\00000009.jpg’,
‘./img/superman\00000010.jpg’]

在这里插入图片描述

2、调整图像大小

datagen = image.ImageDataGenerator()
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False,  save_to_dir=out_path+'resize',save_prefix='gen', target_size=(224, 224))
  1. 创建一个数据增强的实例
  2. 指定参数加载图像数据
  3. save_to_dir=out_path+‘resize’,用到了前面的输出路径
  4. 指定了target_size参数后图像都会被重置成这个尺寸
for i in range(3):gen_data.next()
print_result(out_path+'resize/*')

从数据生成器中获取数据,将图像打印出来
打印结果:
在这里插入图片描述

3、旋转图像

datagen = image.ImageDataGenerator(rotation_range=45)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'rotation_range',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'rotation_range/*')
  1. 创建一个旋转的数据增强实例,
  2. 创建一个数据增强实例,实际上就是直接加载数据
  3. 将加载的图像数据重置尺寸
  4. 将重置尺寸的图像转换成ndarray格式
  5. 将旋转数据增强应用到重置尺寸的图像数据中
  6. 使用数据增强生成器重新从目录加载数据
  7. 保存加载的数据
  8. 使用for循环:
  9. 生成并处理三个图像,由于设置了 save_to_dir,这些图像将被保存。
  10. 打印三个图像

打印结果:

Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.

在这里插入图片描述

4、平移变换

datagen = image.ImageDataGenerator(width_shift_range=0.3,height_shift_range=0.3)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'shift',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'shift/*')

与3中不同的是,这段代码是进行平移变换进行数据增强,指定了平移变换的参数,width_shift_range=0.3,height_shift_range=0.3,这两个参数分别表示会在水平方向和垂直方向±30%的范围内随机移动

打印结果:

Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.

在这里插入图片描述

datagen = image.ImageDataGenerator(width_shift_range=-0.3,height_shift_range=0.3)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'shift2',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'shift2/*')

由于是随机的,这两段代码完全一样,但是结果却不同
打印结果:

Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
在这里插入图片描述

5、缩放

datagen = image.ImageDataGenerator(zoom_range=0.5)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'zoom',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'zoom/*')

这段代码与3中不同的就是,这里指定缩放参数来进行缩放数据增强
打印结果:

Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.

在这里插入图片描述

6、channel_shift

datagen = image.ImageDataGenerator(channel_shift_range=15)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'channel',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'channel/*')

这段代码与3中不同的就是,这里指定通道偏移参数来进行通道偏移数据增强
打印结果:

Found 3 images belonging to 1 classes.
Found 3 images belonging to 1 classes.
在这里插入图片描述

7、水平翻转

datagen = image.ImageDataGenerator(horizontal_flip=True)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'horizontal',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'horizontal/*')

这段代码与3中不同的就是,这里指定水平翻转参数来进行水平翻转数据增强
在这里插入图片描述

8、rescale重新缩放

datagen = image.ImageDataGenerator(rescale= 1/255)
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'rescale',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'rescale/*')

这段代码与3中不同的就是,这里指定rescale重新缩放参数来进行rescale重新缩放数据增强
通常用于归一化图像数据。将图像像素值从 [0, 255] 缩放到 [0, 1] 范围,有助于模型的训练
在这里插入图片描述

9、填充方法

  • ‘constant’: kkkkkkkk|abcd|kkkkkkkk (cval=k)
  • ‘nearest’: aaaaaaaa|abcd|dddddddd
  • ‘reflect’: abcddcba|abcd|dcbaabcd
  • ‘wrap’: abcdabcd|abcd|abcdabcd
datagen = image.ImageDataGenerator(fill_mode='wrap', zoom_range=[4, 4])
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'fill_mode',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'fill_mode/*')
  • fill_mode='wrap':当应用几何变换后,图像中可能会出现一些新的空白区域。fill_mode 定义了如何填充这些空白区域。在这种情况下,使用 'wrap' 模式,意味着空白区域将用图像边缘的像素“包裹”填充。
  • zoom_range=[4, 4]:这设置了图像缩放的范围。在这里,它被设置为在 4 倍范围内进行随机缩放。由于最小和最大缩放因子相同,这将导致所有图像都被放大 4 倍

用原图像填充,任何超出原始图像边界的区域将被图像的对边界像素填充
在这里插入图片描述

datagen = image.ImageDataGenerator(fill_mode='nearest', zoom_range=[4, 4])
gen = image.ImageDataGenerator()
data = gen.flow_from_directory(in_path, batch_size=1, class_mode=None, shuffle=True, target_size=(224, 224))
np_data = np.concatenate([data.next() for i in range(data.n)])
datagen.fit(np_data)
gen_data = datagen.flow_from_directory(in_path, batch_size=1, shuffle=False, save_to_dir=out_path+'nearest',save_prefix='gen', target_size=(224, 224))
for i in range(3):gen_data.next()
print_result(out_path+'nearest/*')

使用最近点填充,每个空白区域的像素将取其最近的非空白区域的像素值
在这里插入图片描述

这篇关于TensorFlow2实战-系列教程4:数据增强:keras工具包/Data Augmentation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/656671

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Java中的登录技术保姆级详细教程

《Java中的登录技术保姆级详细教程》:本文主要介绍Java中登录技术保姆级详细教程的相关资料,在Java中我们可以使用各种技术和框架来实现这些功能,文中通过代码介绍的非常详细,需要的朋友可以参考... 目录1.登录思路2.登录标记1.会话技术2.会话跟踪1.Cookie技术2.Session技术3.令牌技

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=