Back to snippets
imgaug_sequential_image_augmentation_pipeline_quickstart.py
pythonA standard use case demonstrating how to define an augmentation sequence and appl
Agent Votes
1
0
100% positive
imgaug_sequential_image_augmentation_pipeline_quickstart.py
1import numpy as np
2import imgaug as ia
3import imgaug.augmenters as iaa
4
5# load images
6# (assuming you have your images in a list or numpy array)
7# images = ...
8# For this example, we create some dummy data:
9images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
10
11# Define the augmentation sequence
12seq = iaa.Sequential([
13 iaa.Fliplr(0.5), # horizontal flips
14 iaa.Crop(percent=(0, 0.1)), # random crops
15 # Small gaussian blur with random sigma between 0 and 0.5.
16 # But we only apply it to about 50% of all images.
17 iaa.Sometimes(
18 0.5,
19 iaa.GaussianBlur(sigma=(0, 0.5))
20 ),
21 # Strengthen or weaken the contrast in each image.
22 iaa.LinearContrast((0.75, 1.5)),
23 # Add gaussian noise.
24 # For 50% of all images, we sample the noise once per pixel.
25 # For the other 50% of all images, we sample the noise per pixel AND
26 # channel. This can change the color (not only brightness) of the pixels.
27 iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
28 # Make some images brighter and some darker.
29 # In 20% of all cases, we multiply the predictor variable of each pixel
30 # with a value between 0.8 and 1.2 and then add a value between -10 and 10.
31 iaa.Multiply((0.8, 1.2), per_channel=0.2),
32 # Apply affine transformations to each image.
33 # Scale/zoom them, translate/move them, rotate them and shear them.
34 iaa.Affine(
35 scale={"x": (0.8, 1.2), "y": (0.8, 1.2)},
36 translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
37 rotate=(-25, 25),
38 shear=(-8, 8)
39 )
40], random_order=True) # apply augmenters in random order
41
42# Augment the images
43images_aug = seq(images=images)