keras_cv进行数据增强

2023-10-26

使用keras_cv来做分类数据增强

以下直接上流程,具体的原理和代码上github查看源码及配合tensorflow官网及keras官网来做处理。当前(2022.10.8)这些文档还不是很全。

import os
import numpy as np
import tensorflow as tf 
from tensorflow import keras
import keras_cv
import matplotlib.pyplot as plt
from PIL import Image,ImageEnhance,ImageOps
#多GPU,只使用第二,三个
gpu = tf.config.list_physical_devices('GPU')
tf.config.set_visible_devices(gpu[1:3],'GPU')
tf.config.experimental.set_memory_growth(gpu[1],True)
tf.config.experimental.set_memory_growth(gpu[2],True)


print(keras_cv.__version__) #当前keras_cv 的版本
2022-10-08 09:54:14.941532: I tensorflow/core/util/util.cc:169] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.


0.3.4
image = tf.io.read_file('imagenet/n02106662/n02106662_38997.JPEG')
image = tf.image.decode_image(image,channels=3,expand_animations=False)
# image = tf.cast(image,tf.float32)
# image = tf.image.convert_image_dtype(image,tf.float32)
image.dtype
tf.uint8
plt.imshow(image)
<matplotlib.image.AxesImage at 0x7f74fb181b50>

在这里插入图片描述



augmenter = keras_cv.layers.preprocessing.Augmenter(
    layers=[
        # keras_cv.layers.preprocessing.RandomCropAndResize(target_size=(224,224),crop_area_factor=(0.8,1.0), aspect_ratio_factor=(3/4.0,4/3.0)),
        # keras_cv.layers.preprocessing.RandomFlip(mode='horizontal'), #
        # keras_cv.layers.preprocessing.MaybeApply(layer=keras_cv.layers.preprocessing.ChannelShuffle(),rate=0.5),
        # keras_cv.layers.preprocessing.MaybeApply(layer=keras_cv.layers.preprocessing.Grayscale(output_channels=3),rate=0.2),
        # keras_cv.layers.preprocessing.Equalization(value_range=[0,255]),
        # keras_cv.layers.preprocessing.RandomJpegQuality(factor=(75,100)),
        # keras_cv.layers.preprocessing.MaybeApply(keras_cv.layers.preprocessing.RandomGaussianBlur(kernel_size=3,factor=(0.,5.0)),0.2),
        # keras_cv.layers.preprocessing.MaybeApply(keras_cv.layers.preprocessing.RandomRotation(factor=0.08),0.5)
        # keras_cv.layers.preprocessing.FourierMix(alpha=0.5) # this need batchsize data
        # keras_cv.layers.preprocessing.AugMix(value_range=[0,255],severity=0.3,num_chains=3,chain_depth=[1,3],alpha=1.0)
        # keras_cv.layers.preprocessing.RandomCutout(height_factor=(0.0,0.5),weight_factor=(0.0,0.5),fill_mode="gaussian_noise"),
        # keras_cv.layers.preprocessing.GridMask(ratio_factor=(0.0,0.3),rotation_factor=(0.0,0.1),fill_mode="gaussian_noise"),
        # keras_cv.layers.preprocessing.RandomBrightness(factor=0.5),
        # keras_cv.layers.preprocessing.RandomContrast(factor=0.5),
        # keras_cv.layers.preprocessing.RandomSaturation(factor=(0.1,0.9))
        # keras_cv.layers.preprocessing.RandomHue(factor=0.2,value_range=[0,255]),
        # keras_cv.layers.preprocessing.RandomAugmentationPipeline(layers=[
        #     keras_cv.layers.preprocessing.Augmenter(layers=[
        #         keras_cv.layers.preprocessing.RandomAugmentationPipeline(layers=[keras_cv.layers.preprocessing.RandomBrightness(factor=0.5),keras_cv.layers.preprocessing.RandomContrast(factor=0.5), \
        #             keras_cv.layers.RandomSaturation(factor=(0.1,0.9)),keras_cv.layers.RandomHue(factor=0.2,value_range=[0,255])], augmentations_per_image=1,rate=1.0),
        #         keras_cv.layers.preprocessing.AugMix(value_range=[0,255],severity=0.3,num_chains=3,chain_depth=[1,3],alpha=1.0) # augmix不含颜色的处理
        #     ]),
        #     keras_cv.layers.preprocessing.RandAugment(value_range=(0, 255),magnitude=0.3,magnitude_stddev=0.1)],augmentations_per_image=1,rate=1.0)
        # keras_cv.layers.preprocessing.RandomAugmentationPipeline(layers=[keras_cv.layers.preprocessing.RandomBrightness(factor=0.5),keras_cv.layers.preprocessing.RandomContrast(factor=0.5), \
        #     keras_cv.layers.preprocessing.RandomSaturation(factor=(0.1,0.9)),keras_cv.layers.preprocessing.RandomHue(factor=0.2,value_range=[0,255])], \
        #         augmentations_per_image=1,rate=1.0),
        # keras_cv.layers.preprocessing.RandomColorJitter(value_range=[0,255],brightness_factor=0.5,contrast_factor=0.5,saturation_factor=(0.1,0.9),hue_factor=0.2),
        # keras_cv.layers.preprocessing.AugMix(value_range=[0,255],severity=0.3,num_chains=3,chain_depth=[1,3],alpha=1.0)
        # keras_cv.layers.preprocessing.RandAugment(value_range=(0, 255)),
        # keras_cv.layers.preprocessing.CutMix(),
        # keras_cv.layers.preprocessing.preprocessing.MixUp()
        ]
)
plt.figure(figsize=(60,60))
for i in range(30):
    plt.subplot(6,5,i+1)
    im = augmenter(image)
    print(im.dtype)
    print(tf.reduce_max(im))
    im1 = tf.cast(im,tf.uint8)
    plt.imshow(im1)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(254.99686, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(204.13892, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(251.00674, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(241.88087, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(246.78668, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(190.48203, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(132.34175, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(167.4629, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(199.27293, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(165.47491, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(206.75331, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(253.02667, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(235.15588, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(184.35695, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(254.46008, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(225.19394, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(249.16089, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(227.499, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(169.73618, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(191.14867, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(243.1288, shape=(), dtype=float32)
<dtype: 'float32'>
tf.Tensor(255.0, shape=(), dtype=float32)

在这里插入图片描述
以上的各种增强对于分类特别有用,有些需要是batch size的data才可以。
有几个特别需要注意:

1、keras_cv.layers.preprocessing.Augmenter(layers=[])

会把layers中的各层依次执行

2、keras_cv.layers.preprocessing.MaybeApply(layer=,rate=)

rate=0到1 ,表明一个layer在执行时的百分比,rate=1表示一定执行

3、keras_cv.layers.preprocessing.RandomArgumentationPipeline(layers=[],augmentations_per_image=,rate=)

augmentations_per_image可以理解为从layers中选几个层来执行,rate则是每个层执行的可能性

4、keras_cv.layers.preprocessing.RandomChoice(layers=[])

这个和3中当augmentations_per_image=1,rate=1.0时是一样的,从layers中选一个出来进行执行。

还要注意的是以上各层可以嵌套。
处理batch size的数据增强如下:

batch_augmenter = keras_cv.layers.preprocessing.Augmenter(
    layers=[

        keras_cv.layers.preprocessing.FourierMix(alpha=0.5) # this need 
        # keras_cv.layers.preprocessing.CutMix(),
        # keras_cv.layers.preprocessing.MixUp()
        ]
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

keras_cv进行数据增强 的相关文章

随机推荐

  • Maven(六) eclipse 使用Maven deploy命令部署构建到Nexus

    转载于 http blog csdn net jun55xiu article details 43051627 1 应用场景 SYS UTIL 系统工具 项目部署 构建成JAR包 SYS UTIL XXX jar 存储到Nexus私服上
  • spring boot 使用application.properties 进行外部配置

    application properties大家都不陌生 我们在开发的时候 经常使用它来配置一些可以手动修改而且不用编译的变量 这样的作用在于 打成war包或者jar用于生产环境时 我们可以手动修改环境变量而不用再重新编译 spring b
  • python里的pypi是干什么用的_【python工具篇】pip和pypi

    PyPI the Python Package Index The Python Package Index is a repository of software for the Python programming language T
  • HTTP中GET,POST和PUT的区别

    一 HTTP中定义了以下几种请求方法 1 GET 2 POST 3 PUT 4 DELETE 5 HEAD 6 TRACE 7 OPTIONS 二 各个方法介绍 1 GET方法 对这个资源的查操作 2 DELETE方法 对这个资源的删操作
  • 电脑检测不到第二个显示器的解决方法

    一般是因为显示适配器被失效了 右击开始菜单 选择 设备管理器 再选择 显示适配器 这时图标上一般会带上感叹号 右击后选择禁用 再选择启用就能检测到第二个显示器
  • 第一个跑马灯实验

    如何新建一个工程 1 打开工程模板 删除其他不重要的库文件 把main 函数里的内容删除 不用的外设固件库文件可以删掉 节省编译时间 rcc 时钟使能 usart 串口 复用映射 setbits 设置高电平 resetbits 低电平 2
  • 「PAT甲级真题解析」Advanced Level 1006 Sign In and Sign Out

    PAT Advanced Level Practice 1006 Sign In and Sign Out 如果对你有帮助 要点个赞让我知道喔 文章目录 问题分析 完整描述步骤 伪代码描述 完整提交代码 问题分析 题目给出一组学生进入机房的
  • 计量经济学及Stata应用 陈强 第八章自相关习题8.3

    8 3使用数据集gasoline dta估计美国1953 2004年的汽油需求函数 考虑如下回归 其中 被解释变量lgasq为人均汽油消费量的对数 解释变量lincome为人均收入的对数 lgasp为汽油价格指数的对数 lpnc为新车价格指
  • ROS建模仿真(1)-创建机器人模型

    ROS建模仿真 1 创建机器人模型 创建catkin creat pkg功能包 创建机器人描述文件 创建launch文件 创建catkin creat pkg功能包 创建机器人描述文件 创建launch文件 创建catkin creat p
  • Mac OS上使用ffmpeg的“血泪”总结

    标题真不是夸张 这几天在整理视频相关的处理流程 为了获得一些性能数据 打算在自己的MacBook Pro 上面装ffmepg 这一折腾4 5天就过去了 有些问题 在解决之后就豁然开朗了 没有解决之前 真的是百思不得其解 中间就好像隔着一层纱
  • AF_INET和AF_PACKET区别

    http blog csdn net kzm2008 article details 5372834 man 7 ip man 7 packet Packet sockets are used to receive or send raw
  • 单片机蓝桥杯——定时中断实现数码管显示、按键判断

    1 1ms定时中断T0 控制数码管显示 1 关于中断 关于定时中断的初始化函数可直接在STC ISP软件上生成 如下图所示 注意 初始化函数中并没有打开EA和ET0 需要自己加上 2 关于数码管显示 数码管段码 segCode 0 segC
  • Flutter提供者模式说明

    在本文中 我们将介绍Flutter中的Provider模式 Google的工作小组建议使用提供程序模式 他们还在Flutter的Pragmatic State Management中的 Google I O 2019上进行了介绍 其他一些模
  • Nginx的Gzip压缩

    Nginx的Gzip压缩 Nginx开启Gzip压缩功能 可以使网站的css js xml html 文件在传输时进行压缩 提高访问速度 进而优化Nginx性能 在Nginx配置文件中可以配置Gzip的使用 相关指令可以在http区域 se
  • Java流程控制--分支结构

    Java流程控制 分支结构 if 单分支 结构 if 条件表达式 这个表达式的结果是布尔值 要么是false 要么是true 如果上面 中的表达式返回结果是true 那么执行 中代码 如果上面 中的表达式返回结果是false 那么不执行 中
  • Unity的Audio组件命令有哪些

    Unity 的 Audio 组件命令有以下几种 Play 播放音频 Pause 暂停音频 UnPause 取消暂停音频 Stop 停止播放音频 SetScheduledStartTime 设置音频开始播放的时间 SetScheduledEn
  • SpringBoot使用Redisson做延迟队列案列(超详细)

    背景 有些场景下 需要延迟触发一些任务 比如 延迟几秒钟发送短信或者邮件 某些业务系统回调 需要延时几秒钟后回调 当然 实现延时触发的方式有很多 我这里采用 redisson 的 RDelayedQueue 一是因为接入简单 二是没有分布式
  • post使用form-data和x-www-form-urlencoded的本质区别

    一是数据包格式的区别 二是数据包中非ANSCII字符怎么编码 是百分号转码发送还是直接发送 一 application x www form urlencoded 1 它是post的默认格式 使用js中URLencode转码方法 包括将na
  • 修改onnx模型输出示例

    前言 如图是netron github链接 软件中打开的onnx模型 可以看到右边模型的最终输出结果是分类值predict 0而非概率值 那么如何获取中间过程的概率值 或者说怎么把右边的图砍掉一截变成左边的图呢 代码 读入模型 import
  • keras_cv进行数据增强

    使用keras cv来做分类数据增强 以下直接上流程 具体的原理和代码上github查看源码及配合tensorflow官网及keras官网来做处理 当前 2022 10 8 这些文档还不是很全 import os import numpy