中文简易版文档: https://blog.csdn.net/qq_38451119/article/details/82428612
官方文档:https://imgaug.readthedocs.io/en/latest/source/dtype_support.html
github上的官方文档:https://github.com/aleju/imgaug
例子:https://www.jianshu.com/p/989b2f4b246d
1. 注意: 如果图像维度是[h,w,c],则要用 images_aug = seq.augment_image(images) ,也就是augment_image后面没有s,与之相反的是augment_images(有s)。
You provided a numpy array of shape (1200, 1920, 3) as input to augment_images(), which was interpreted as (N, H, W). The last dimension however has value 1 or 3, which indicates that you provided a single image with shape (H, W, C) instead. If that is the case, you should use augment_image(image) or augment_images([image]), otherwise you will not get the expected augmentations.
"you will not get the expected augmentations." % (images_copy.shape,))
2. per_channel参数含义: bool or float, optional Whether to use the same value for all channels (False) or to sample a new value for each channel (True). If this value is a float ``p``, then for ``p`` percent of all images`per_channel` will be treated as True, otherwise as False.
实例分析:
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
from PIL import Image
import cv2
from functools import reduce
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
sometimes = lambda aug: iaa.Sometimes(0.9, aug)
sometimes_p1 = lambda aug: iaa.Sometimes(0.9, aug)
def load_batch(batch_idx):
# dummy function, implement this
# Return a numpy array of shape (N, height, width, #channels)
# or a list of (height, width, #channels) arrays (may have different image
# sizes).
# Images should be in RGB for colorspace augmentations.
# (cv2.imread() returns BGR!)
# Images should usually be in uint8 with values from 0-255.
# return np.zeros((128, 32, 32, 3), dtype=np.uint8) + (batch_idx % 255)
im1 = Image.open('./timg.jpeg')
im1= np.array(im1)
return im1
def train_on_images(images,idx):
# dummy function, implement this
#images=cv2.cvtColor(images, cv2.COLOR_RGB2BGR)
cv2.imwrite('./images/'+str(idx)+'_messigray.jpg',images)
pass
seq = iaa.Sequential([
iaa.Fliplr(0.5), # horizontal flips
iaa.Flipud(0.5), # horizontal flips
iaa.Crop(percent=(0, 0.1)), # random crops
# Small gaussian blur with random sigma between 0 and 0.5.
# But we only blur about 50% of all images.
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.5))
),
# Strengthen or weaken the contrast in each image.
iaa.ContrastNormalization((0.5, 2.0)),
# Add gaussian noise.
# For 50% of all images, we sample the noise once per pixel.
# For the other 50% of all images, we sample the noise per pixel AND
# channel. This can change the color (not only brightness) of the
# pixels.
iaa.AdditiveGaussianNoise(loc=0, scale=(0,0.1*255), per_channel=0.5),
sometimes_p1(iaa.SaltAndPepper(p=0.02,per_channel=0.5)),
# Make some images brighter and some darker.
# In 20% of all cases, we sample the multiplier once per channel,
# which can end up changing the color of the images.
iaa.Multiply((0.8, 1.2), per_channel=0.2),
# Apply affine transformations to each image.
# Scale/zoom them, translate/move them, rotate them and shear them.
sometimes(iaa.Affine(
scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, # translate by -20 to +20 percent (per axis)
rotate=(-5, 5), # rotate by -45 to +45 degrees
shear=(-5, 5), # shear by -16 to +16 degrees
order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
cval=(0, 255), # if mode is constant, use a cval between 0 and 255
#mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
], random_order=True) # apply augmenters in random order
for batch_idx in range(20):
images = load_batch(batch_idx)
img_height=images.shape[0]
img_width=images.shape[1]
cv2.imwrite('./images/'+'messigray.jpg',images)
images_aug = seq.augment_image(images) # done by the library
train_on_images(images_aug,batch_idx)
同时更改heatmap的方法:
import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa
from PIL import Image
import cv2
from functools import reduce
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
sometimes = lambda aug: iaa.Sometimes(0.9, aug)
sometimes_p1 = lambda aug: iaa.Sometimes(0.9, aug)
def load_image(batch_idx):
# dummy function, implement this
# Return a numpy array of shape (N, height, width, #channels)
# or a list of (height, width, #channels) arrays (may have different image
# sizes).
# Images should be in RGB for colorspace augmentations.
# (cv2.imread() returns BGR!)
# Images should usually be in uint8 with values from 0-255.
# return np.zeros((128, 32, 32, 3), dtype=np.uint8) + (batch_idx % 255)
im1 = Image.open('./a1_235_1_R.jpg').convert('L')
##print(np.array(im1).shape)
im1= np.array(im1)[np.newaxis, :, :, np.newaxis]
return im1
def load_heatmap(batch_idx):
# dummy function, implement this
# Return a numpy array of shape (N, height, width, #channels)
# or a list of (height, width, #channels) arrays (may have different image
# sizes).
# Images should be in RGB for colorspace augmentations.
# (cv2.imread() returns BGR!)
# Images should usually be in uint8 with values from 0-255.
# return np.zeros((128, 32, 32, 3), dtype=np.uint8) + (batch_idx % 255)
im1 = Image.open('./a1_235_1_R_1.png').convert('L')
im1= np.array(im1)[np.newaxis, :, :, np.newaxis].astype(np.float32)
return im1
def train_on_images(images,idx,i):
# dummy function, implement this
#images=cv2.cvtColor(images, cv2.COLOR_RGB2BGR)
images=images.squeeze(3)
images=images.squeeze(0)
cv2.imwrite('./images/'+str(idx)+"_"+str(i)+'_messigray.jpg',images.astype(np.uint8))
pass
def train_on_heatmaps(images,idx,i):
# dummy function, implement this
#images=cv2.cvtColor(images, cv2.COLOR_RGB2BGR)
images=images.squeeze(3)
images=images.squeeze(0)
images=images*255
cv2.imwrite('./images/'+str(idx)+"_"+str(i)+'_messigray.jpg',images.astype(np.uint8))
pass
seq = iaa.Sequential([
iaa.Fliplr(0.5), # horizontal flips
iaa.Flipud(0.5), # horizontal flips
#iaa.Crop(percent=(0, 0.1)), # random crops
# Small gaussian blur with random sigma between 0 and 0.5.
# But we only blur about 50% of all images.
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.2))
),
# Strengthen or weaken the contrast in each image.
iaa.ContrastNormalization((0.6, 1.4)),
# Add gaussian noise.
# For 50% of all images, we sample the noise once per pixel.
# For the other 50% of all images, we sample the noise per pixel AND
# channel. This can change the color (not only brightness) of the
# pixels.
iaa.AdditiveGaussianNoise(loc=0, scale=(0,0.02*255), per_channel=0.5),
sometimes_p1(iaa.SaltAndPepper(p=0.01,per_channel=0.5)),
# Make some images brighter and some darker.
# In 20% of all cases, we sample the multiplier once per channel,
# which can end up changing the color of the images.
iaa.Multiply((0.8, 1.2), per_channel=0.2),
# Apply affine transformations to each image.
# Scale/zoom them, translate/move them, rotate them and shear them.
sometimes(iaa.Affine(
#scale={"x": (0.9, 1.1), "y": (0.9, 1.1)}, # scale images to 80-120% of their size, individually per axis
#translate_percent={"x": (-0.05, 0.05), "y": (-0.05, 0.05)}, # translate by -20 to +20 percent (per axis)
rotate=(-3, 3), # rotate by -45 to +45 degrees
shear=(-3, 3), # shear by -16 to +16 degrees
order=[0, 1], # use nearest neighbour or bilinear interpolation (fast)
cval=(0, 255), # if mode is constant, use a cval between 0 and 255
#mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
)),
], random_order=True) # apply augmenters in random order
for batch_idx in range(20):
images = load_image(batch_idx)
heatmaps = load_heatmap(batch_idx)
img_height=images.shape[0]
img_width=images.shape[1]
cv2.imwrite('./images/'+'messigray.jpg',images)
seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start
images_aug, heatmaps_aug = seq(images=images,heatmaps=heatmaps) # done by the library
#images_aug = seq(images=images) # done by the library
train_on_images(images_aug,batch_idx, 0)
train_on_heatmaps(heatmaps_aug,batch_idx, 1)