[AI数据]数据增强之路
程序员文章站
2022-05-06 19:06:39
1.维度位置增强(包括Scaling/Cropping/Flipping/Padding/Rotation/Translation/Affine transformation)颜色增强(包括Brightness/Contrast/Saturation/Hue)2.方法2.1.imgaug法2.1.1 旋转import imageioimport imgaug as iaimport imgaug.augmenters as iaaimport numpy as npi....
1.维度
位置增强(包括Scaling/Cropping/Flipping/Padding/Rotation/Translation/Affine transformation)
颜色增强(包括Brightness/Contrast/Saturation/Hue)
2.方法
2.1.imgaug法
2.1.1 旋转
import imageio
import imgaug as ia
import imgaug.augmenters as iaa
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib
%matplotlib inline
image = imageio.imread("car2.jpeg")
ia.imshow(image)
rotate=iaa.Affine(rotate=(-50, 30))
rotated_image=rotate.augment_image(image)
ia.imshow(rotated_image)
2.1.2 增加噪声
gaussian_noise=iaa.AdditiveGaussianNoise(10,20)
noise_image=gaussian_noise.augment_image(image)
ia.imshow(noise_image)
2.1.3 裁剪
crop = iaa.Crop(percent=(0, 0.3)) # crop image
corp_image=crop.augment_image(image)
ia.imshow(corp_image)
2.1.4 扭曲
shear = iaa.Affine(shear=(0,40))
shear_image=shear.augment_image(image)
ia.imshow(shear_image)
2.1.5 翻转
#flipping image horizontally
flip_hr=iaa.Fliplr(p=1.0)
flip_hr_image= flip_hr.augment_image(image)
ia.imshow(flip_hr_image)
flip_vr=iaa.Flipud(p=1.0)
flip_vr_image= flip_vr.augment_image(image)
ia.imshow(flip_vr_image)
2.1.6 改变亮度
image = imageio.imread("car2.jpeg")
contrast=iaa.GammaContrast(gamma=2.0)
contrast_image =contrast.augment_image(image)
ia.imshow(contrast_image)
2.2 pytorch法
https://iq.opengenus.org/data-augmentation/
2.3 tensorflow法
https://medium.com/@prasad.pai/data-augmentation-techniques-in-cnn-using-tensorflow-371ae43d5be9
2.4 keras法
# 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
# 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...")
image = load_img(args["image"])
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
# construct the image generator for data augmentation then
# initialize the total number of images generated thus far
aug = ImageDataGenerator(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="jpg")
# loop over examples from out 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
3.参考
https://towardsdatascience.com/data-augmentation-techniques-in-python-f216ef5eed69
本文地址:https://blog.csdn.net/guaguastd/article/details/107573275