keras对图像数据进行增强 | keras data augmentation
2024-10-19 01:00:45
本文首发于个人博客https://kezunlin.me/post/8db507ff/,欢迎阅读最新内容!
keras data augmentation
Guide
code
# import the necessary packages
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
import argparse
from keras_util import *
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-o", "--output", required=True,
help="path to output directory to store augmentation examples")
ap.add_argument("-p", "--prefix", type=str, default="image",
help="output filename prefix")
args = vars(ap.parse_args())
# load the input image, convert it to a NumPy array, and then
# reshape it to have an extra dimension
print("[INFO] loading example image...")
target_size = None
#target_size=(224,224)
image = load_img(args["image"], target_size=target_size)
image = img_to_array(image)
image = np.expand_dims(image, axis=0) # 1,h,w,c
# construct the image generator for data augmentation then
# initialize the total number of images generated thus far
# preprocessing_function: The function will run after the image is resized and augmented.
# The function should take one argument:
# one image (Numpy tensor with rank 3),
# and should output a Numpy tensor with the same shape.
# for 1 image --->(424,640,3)--->aug---(424,640,3)--->preprocess_input--->(424,640,3)
# for 1 image --->resize--->(224,224,3)--->aug---(224,224,3)--->preprocess_input--->(224,224,3)
aug = ImageDataGenerator(preprocessing_function=resnet.preprocess_input,
rotation_range=30,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode="nearest")
total = 0
# construct the actual Python generator
print("[INFO] generating images...")
imageGen = aug.flow(image,
batch_size=1,
save_to_dir=args["output"],
save_prefix=args["prefix"],
save_format="png")
next_image = next(imageGen)
print(next_image.shape)
print(next_image[0, :5,:5,0])
# loop over examples from our image data augmentation generator
for image in imageGen:
# increment our counter
total += 1
# if we have reached 10 examples, break from the loop
if total == 10:
break
output
target_size = None:
1 image --->(424,640,3)--->aug--->(424,640,3)--->preprocess_input--->(424,640,3)
target_size = (224,224):
1 image --->resize--->(224,224,3)--->aug--->(224,224,3)--->preprocess_input--->(224,224,3)
Reference
History
- 20190910: created.
Copyright
- Post author: kezunlin
- Post link: https://kezunlin.me/post/8db507ff/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
最新文章
- oracle通过修改控制文件scn推进数据库scn
- python之目录文件操作
- IT项目管理感悟
- CCF 模拟D 动态规划
- 常用的JavaScript验证正则表达式1
- .NET中 使用数组的注意事项
- shell下,进程的前台与后台运行
- js 多物体运动
- Android开发--ListPreferance 运行报错:android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)
- 内外连接、组函数、DDL、DML和TCL
- iOS开发蓝牙 蓝牙4.0的各种踩过的坑,希望你们少踩点
- 我的时间,GTD做主
- vbs和qtp一些脚本
- iOS MJRefresh上拉加载更多
- requests+正则表达式爬取ip
- 神奇高效的Linux命令行
- 如何使用JMeter开源性能测试工具来构建Web性能测试体系
- 服务器对接码云webhooks
- mongdb的索引及备份
- M - 约会安排 HDU - 4553 线段树 (最长连续段)
热门文章
- Python通过pymysql连接数据库并进行查询和更新SQL方法封装
- 第一篇:C++之hello world
- 【CentOS 7】CentOS 7各个版本镜像下载地址(转)
- yii2自定义操作按钮
- ASP.NET Core Web 应用程序系列(四)- ASP.NET Core 异步编程之async await
- JavaWeb问题记录——在Windows上启动Tomcat后命令行窗口乱码
- [browser navigator],写了个检测游览器版本
- ios11下适配UItableView
- 【转载】Android Context 到底是什么?
- RHEL 6.6配置网易CentOS镜像的yum源小结