本文首发于个人博客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

keras对图像数据进行增强 | keras data augmentation的更多相关文章

  1. 图像数据增强 (Data Augmentation in Computer Vision)

    1.1 简介 深层神经网络一般都需要大量的训练数据才能获得比较理想的结果.在数据量有限的情况下,可以通过数据增强(Data Augmentation)来增加训练样本的多样性, 提高模型鲁棒性,避免过拟 ...

  2. Keras Data augmentation(数据扩充)

    在深度学习中,我们经常需要用到一些技巧(比如将图片进行旋转,翻转等)来进行data augmentation, 来减少过拟合. 在本文中,我们将主要介绍如何用深度学习框架keras来自动的进行data ...

  3. 深度学习中的Data Augmentation方法(转)基于keras

    在深度学习中,当数据量不够大时候,常常采用下面4中方法: 1. 人工增加训练集的大小. 通过平移, 翻转, 加噪声等方法从已有数据中创造出一批"新"的数据.也就是Data Augm ...

  4. keras系列︱图像多分类训练与利用bottleneck features进行微调(三)

    引自:http://blog.csdn.net/sinat_26917383/article/details/72861152 中文文档:http://keras-cn.readthedocs.io/ ...

  5. 【Tool】Augmentor和imgaug——python图像数据增强库

    Augmentor和imgaug--python图像数据增强库 Tags: ComputerVision Python 介绍两个图像增强库:Augmentor和imgaug,Augmentor使用比较 ...

  6. Keras中图像维度介绍

    报错问题: ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_1/convolution' ...

  7. 我的Keras使用总结(5)——Keras指定显卡且限制显存用量,常见函数的用法及其习题练习

    Keras 是一个高层神经网络API,Keras是由纯Python编写而成并基于TensorFlow,Theano以及CNTK后端.Keras为支持快速实验而生,能够将我们的idea迅速转换为结果.好 ...

  8. Keras官方中文文档:keras后端Backend

    所属分类:Keras Keras后端 什么是"后端" Keras是一个模型级的库,提供了快速构建深度学习网络的模块.Keras并不处理如张量乘法.卷积等底层操作.这些操作依赖于某种 ...

  9. 【48】数据扩充(Data augmentation)

    数据扩充(Data augmentation) 大部分的计算机视觉任务使用很多的数据,所以数据扩充是经常使用的一种技巧来提高计算机视觉系统的表现.我认为计算机视觉是一个相当复杂的工作,你需要输入图像的 ...

随机推荐

  1. Java+TestNG+Maven+Excel+IDEA接口自动化入门(一)环境配置

    前置: 1.本机环境安装了maven并配置环境变量 2.本机环境安装了IDEA软件 3.本机环境安装了Java jdk 8版本 4.有一定java和maven基础 因为以上网上例子很多,就不再重复赘述 ...

  2. 如何通过QT designer设置不让窗口最大化

    最近使用QT写一个小窗口的程序,窗口通过QT designer制作之后,运行时可以最大化操作,且最大化之后界面上控件也不会随窗口变化而变化,但由于人都比较懒,直接在QT designer设置窗口属性时 ...

  3. Yii2 中常用的增删改查操作总结

    一.新增 1.使用save() $model = new User(); $model->name = 'test'; $model->phone = '13000000000'; $mo ...

  4. Java实现网络聊天中使用的socket API与Linux socket API之间的关系

    尝试着用Java编写一个网络聊天程序,发现总不如网上写的好,所以就直接引用了网上大神的优秀代码.代码如下: package project1; import java.awt.*; import ja ...

  5. lxml导入

    通常的导入方式 from lxml import etree python有自带的ElementTree库,但lxml在其基础上新增了特有的功能 如果代码仅使用ElementTree API,不依赖于 ...

  6. 云数据库MongoDB版清理oplog日志和compact命令详解

    1.问题描述: 今天看到公司mongodb的oplog有点大,看到云数据库MongoDB版日志清理策略. MongoDB数据库在长期频繁地删除/写入数据或批量删除了大量数据,将产生很多物理空间碎片. ...

  7. Googleplaystore数据分析

    本次所用到的数据分析工具:numpy.pandas.matplotlib.seaborn 一.分析目的 假如接下来需要开发一款APP,想了解开发什么类型的APP会更受欢迎,此次分析可以对下一步计划进行 ...

  8. spring源码分析6: ApplicationContext的初始化与BeanDefinition的搜集入库

    先前几篇都是概念的讲解:回顾下 BeanDefinition 是物料 Bean是成品 BeanFactory是仓库,存储物料与成品 ApplicationContext初始化搜集物料入库,触发生产线, ...

  9. 在Asp.Net或.Net Core中配置使用MarkDown富文本编辑器有开源模板代码(代码是.net core3.0版本)

    研究如何使用Markdown你们可能要花好几天才能搞定,但是看我的文章或者下载了源码,你搞定一般在10分钟之内.我先给各位介绍下它: Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯 ...

  10. C# loop executed one by one wait the former completed

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...