使用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()
]
)