Code Monkey home page Code Monkey logo

imgaug's Introduction

imgaug

This python library helps you with augmenting images for your machine learning projects. It converts a set of input images into a new, much larger set of slightly altered images.

Build Status codecov Codacy Badge

  Image Heatmaps Seg. Maps Keypoints Bounding Boxes,
Polygons
Original Input input images input heatmaps input segmentation maps input keypoints input bounding boxes
Gauss. Noise
+ Contrast
+ Sharpen
non geometric augmentations, applied to images non geometric augmentations, applied to heatmaps non geometric augmentations, applied to segmentation maps non geometric augmentations, applied to keypoints non geometric augmentations, applied to bounding boxes
Affine affine augmentations, applied to images affine augmentations, applied to heatmaps affine augmentations, applied to segmentation maps affine augmentations, applied to keypoints affine augmentations, applied to bounding boxes
Crop
+ Pad
crop and pad augmentations, applied to images crop and pad augmentations, applied to heatmaps crop and pad augmentations, applied to segmentation maps crop and pad augmentations, applied to keypoints crop and pad augmentations, applied to bounding boxes
Fliplr
+ Perspective
Horizontal flip and perspective transform augmentations, applied to images Horizontal flip and perspective transform augmentations, applied to heatmaps Horizontal flip and perspective transform augmentations, applied to segmentation maps Horizontal flip and perspective transform augmentations, applied to keypoints Horizontal flip and perspective transform augmentations, applied to bounding boxes

More (strong) example augmentations of one input image:

64 quokkas

Table of Contents

  1. Features
  2. Installation
  3. Documentation
  4. Recent Changes
  5. Example Images
  6. Code Examples
  7. Citation
  • Many augmentation techniques
    • E.g. affine transformations, perspective transformations, contrast changes, gaussian noise, dropout of regions, hue/saturation changes, cropping/padding, blurring, ...
    • Optimized for high performance
    • Easy to apply augmentations only to some images
    • Easy to apply augmentations in random order
  • Support for
    • Images (full support for uint8, for other dtypes see documentation)
    • Heatmaps (float32), Segmentation Maps (int), Masks (bool)
      • May be smaller/larger than their corresponding images. No extra lines of code needed for e.g. crop.
    • Keypoints/Landmarks (int/float coordinates)
    • Bounding Boxes (int/float coordinates)
    • Polygons (int/float coordinates)
    • Line Strings (int/float coordinates)
  • Automatic alignment of sampled random values
    • Example: Rotate image and segmentation map on it by the same value sampled from uniform(-10°, 45°). (0 extra lines of code.)
  • Probability distributions as parameters
    • Example: Rotate images by values sampled from uniform(-10°, 45°).
    • Example: Rotate images by values sampled from ABS(N(0, 20.0))*(1+B(1.0, 1.0))", where ABS(.) is the absolute function, N(.) the gaussian distribution and B(.) the beta distribution.
  • Many helper functions
    • Example: Draw heatmaps, segmentation maps, keypoints, bounding boxes, ...
    • Example: Scale segmentation maps, average/max pool of images/maps, pad images to aspect ratios (e.g. to square them)
    • Example: Convert keypoints to distance maps, extract pixels within bounding boxes from images, clip polygon to the image plane, ...
  • Support for augmentation on multiple CPU cores

The library supports python 2.7 and 3.4+.

Installation: Anaconda

To install the library in anaconda, perform the following commands:

conda config --add channels conda-forge
conda install imgaug

You can deinstall the library again via conda remove imgaug.

Installation: pip

Then install imgaug either via pypi (can lag behind the github version):

pip install imgaug

or install the latest version directly from github:

pip install git+https://github.com/aleju/imgaug.git

For more details, see the install guide

To deinstall the library, just execute pip uninstall imgaug.

Example jupyter notebooks:

More notebooks: imgaug-doc/notebooks.

Example ReadTheDocs pages:

More RTD documentation: imgaug.readthedocs.io.

All documentation related files of this project are hosted in the repository imgaug-doc.

  • 0.4.0: Added new augmenters, changed backend to batchwise augmentation, support for numpy 1.18 and python 3.8.
  • 0.3.0: Reworked segmentation map augmentation, adapted to numpy 1.17+ random number sampling API, several new augmenters.
  • 0.2.9: Added polygon augmentation, added line string augmentation, simplified augmentation interface.
  • 0.2.8: Improved performance, dtype support and multicore augmentation.

See changelogs/ for more details.

The images below show examples for most augmentation techniques.

Values written in the form (a, b) denote a uniform distribution, i.e. the value is randomly picked from the interval [a, b]. Line strings are supported by (almost) all augmenters, but are not explicitly visualized here.

meta
Identity ChannelShuffle      
Identity ChannelShuffle      
See also: Sequential, SomeOf, OneOf, Sometimes, WithChannels, Lambda, AssertLambda, AssertShape, RemoveCBAsByOutOfImageFraction, ClipCBAsToImagePlanes
arithmetic
Add Add
(per_channel=True)
AdditiveGaussianNoise AdditiveGaussianNoise
(per_channel=True)
Multiply
Add Add per_channel=True AdditiveGaussianNoise AdditiveGaussianNoise per_channel=True Multiply
Cutout Dropout CoarseDropout
(p=0.2)
CoarseDropout
(p=0.2, per_channel=True)
Dropout2d
Cutout Dropout CoarseDropout p=0.2 CoarseDropout p=0.2, per_channel=True Dropout2d
SaltAndPepper CoarseSaltAndPepper
(p=0.2)
Invert Solarize JpegCompression
SaltAndPepper CoarseSaltAndPepper p=0.2 Invert Solarize JpegCompression
See also: AddElementwise, AdditiveLaplaceNoise, AdditivePoissonNoise, MultiplyElementwise, TotalDropout, ReplaceElementwise, ImpulseNoise, Salt, Pepper, CoarseSalt, CoarsePepper, Solarize
artistic
Cartoon        
Cartoon        
blend
BlendAlpha
with EdgeDetect(1.0)
BlendAlphaSimplexNoise
with EdgeDetect(1.0)
BlendAlphaFrequencyNoise
with EdgeDetect(1.0)
BlendAlphaSomeColors
with RemoveSaturation(1.0)
BlendAlphaRegularGrid
with Multiply((0.0, 0.5))
BlendAlpha with EdgeDetect1.0 BlendAlphaSimplexNoise with EdgeDetect1.0 BlendAlphaFrequencyNoise with EdgeDetect1.0 BlendAlphaSomeColors with RemoveSaturation1.0 BlendAlphaRegularGrid with Multiply0.0, 0.5
See also: BlendAlphaMask, BlendAlphaElementwise, BlendAlphaVerticalLinearGradient, BlendAlphaHorizontalLinearGradient, BlendAlphaSegMapClassIds, BlendAlphaBoundingBoxes, BlendAlphaCheckerboard, SomeColorsMaskGen, HorizontalLinearGradientMaskGen, VerticalLinearGradientMaskGen, RegularGridMaskGen, CheckerboardMaskGen, SegMapClassIdsMaskGen, BoundingBoxesMaskGen, InvertMaskGen
blur
GaussianBlur AverageBlur MedianBlur BilateralBlur
(sigma_color=250,
sigma_space=250)
MotionBlur
(angle=0)
GaussianBlur AverageBlur MedianBlur BilateralBlur sigma_color=250, sigma_space=250 MotionBlur angle=0
MotionBlur
(k=5)
MeanShiftBlur      
MotionBlur k=5 MeanShiftBlur      
collections
RandAugment        
RandAugment        
color
MultiplyAndAddToBrightness MultiplyHueAndSaturation MultiplyHue MultiplySaturation AddToHueAndSaturation
MultiplyAndAddToBrightness MultiplyHueAndSaturation MultiplyHue MultiplySaturation AddToHueAndSaturation
Grayscale RemoveSaturation ChangeColorTemperature KMeansColorQuantization
(to_colorspace=RGB)
UniformColorQuantization
(to_colorspace=RGB)
Grayscale RemoveSaturation ChangeColorTemperature KMeansColorQuantization to_colorspace=RGB UniformColorQuantization to_colorspace=RGB
See also: WithColorspace, WithBrightnessChannels, MultiplyBrightness, AddToBrightness, WithHueAndSaturation, AddToHue, AddToSaturation, ChangeColorspace, Posterize
contrast
GammaContrast GammaContrast
(per_channel=True)
SigmoidContrast
(cutoff=0.5)
SigmoidContrast
(gain=10)
LogContrast
GammaContrast GammaContrast per_channel=True SigmoidContrast cutoff=0.5 SigmoidContrast gain=10 LogContrast
LinearContrast AllChannels-
HistogramEqualization
HistogramEqualization AllChannelsCLAHE CLAHE
LinearContrast AllChannels- HistogramEqualization HistogramEqualization AllChannelsCLAHE CLAHE
See also: Equalize
convolutional
Sharpen
(alpha=1)
Emboss
(alpha=1)
EdgeDetect DirectedEdgeDetect
(alpha=1)
 
Sharpen alpha=1 Emboss alpha=1 EdgeDetect DirectedEdgeDetect alpha=1  
See also: Convolve
debug
See also: SaveDebugImageEveryNBatches
edges
Canny        
Canny        
flip
Fliplr Flipud  
Fliplr Flipud  
See also: HorizontalFlip, VerticalFlip
geometric
Affine Affine: Modes  
Affine Affine: Modes  
Affine: cval PiecewiseAffine  
Affine: cval PiecewiseAffine  
PerspectiveTransform ElasticTransformation
(sigma=1.0)
 
PerspectiveTransform ElasticTransformation sigma=1.0  
ElasticTransformation
(sigma=4.0)
Rot90  
ElasticTransformation sigma=4.0 Rot90  
WithPolarWarping
+Affine
Jigsaw
(5x5 grid)
 
WithPolarWarping +Affine Jigsaw 5x5 grid  
See also: ScaleX, ScaleY, TranslateX, TranslateY, Rotate
imgcorruptlike
GlassBlur DefocusBlur ZoomBlur Snow Spatter
GlassBlur DefocusBlur ZoomBlur Snow Spatter
See also: GaussianNoise, ShotNoise, ImpulseNoise, SpeckleNoise, GaussianBlur, MotionBlur, Fog, Frost, Contrast, Brightness, Saturate, JpegCompression, Pixelate, ElasticTransform
pillike
Autocontrast EnhanceColor EnhanceSharpness FilterEdgeEnhanceMore FilterContour
Autocontrast EnhanceColor EnhanceSharpness FilterEdgeEnhanceMore FilterContour
See also: Solarize, Posterize, Equalize, EnhanceContrast, EnhanceBrightness, FilterBlur, FilterSmooth, FilterSmoothMore, FilterEdgeEnhance, FilterFindEdges, FilterEmboss, FilterSharpen, FilterDetail, Affine
pooling
AveragePooling MaxPooling MinPooling MedianPooling  
AveragePooling MaxPooling MinPooling MedianPooling  
segmentation
Superpixels
(p_replace=1)
Superpixels
(n_segments=100)
UniformVoronoi RegularGridVoronoi: rows/cols
(p_drop_points=0)
RegularGridVoronoi: p_drop_points
(n_rows=n_cols=30)
Superpixels p_replace=1 Superpixels n_segments=100 UniformVoronoi RegularGridVoronoi: rows/cols p_drop_points=0 RegularGridVoronoi: p_drop_points n_rows=n_cols=30
RegularGridVoronoi: p_replace
(n_rows=n_cols=16)
       
RegularGridVoronoi: p_replace n_rows=n_cols=16        
See also: Voronoi, RelativeRegularGridVoronoi, RegularGridPointsSampler, RelativeRegularGridPointsSampler, DropoutPointsSampler, UniformPointsSampler, SubsamplingPointsSampler
size
CropAndPad Crop  
CropAndPad Crop  
Pad PadToFixedSize
(height'=height+32,
width'=width+32)
 
Pad PadToFixedSize height'=height+32, width'=width+32  
CropToFixedSize
(height'=height-32,
width'=width-32)
     
CropToFixedSize height'=height-32, width'=width-32      
See also: Resize, CropToMultiplesOf, PadToMultiplesOf, CropToPowersOf, PadToPowersOf, CropToAspectRatio, PadToAspectRatio, CropToSquare, PadToSquare, CenterCropToFixedSize, CenterPadToFixedSize, CenterCropToMultiplesOf, CenterPadToMultiplesOf, CenterCropToPowersOf, CenterPadToPowersOf, CenterCropToAspectRatio, CenterPadToAspectRatio, CenterCropToSquare, CenterPadToSquare, KeepSizeByResize
weather
FastSnowyLandscape
(lightness_multiplier=2.0)
Clouds Fog Snowflakes Rain
FastSnowyLandscape lightness_multiplier=2.0 Clouds Fog Snowflakes Rain
See also: CloudLayer, SnowflakesLayer, RainLayer

Example: Simple Training Setting

A standard machine learning situation. Train on batches of images and augment each batch via crop, horizontal flip ("Fliplr") and gaussian blur:

import numpy as np
import imgaug.augmenters as iaa

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)

def train_on_images(images):
    # dummy function, implement this
    pass

# Pipeline:
# (1) Crop images from each side by 1-16px, do not resize the results
#     images back to the input size. Keep them at the cropped size.
# (2) Horizontally flip 50% of the images.
# (3) Blur images using a gaussian kernel with sigma between 0.0 and 3.0.
seq = iaa.Sequential([
    iaa.Crop(px=(1, 16), keep_size=False),
    iaa.Fliplr(0.5),
    iaa.GaussianBlur(sigma=(0, 3.0))
])

for batch_idx in range(100):
    images = load_batch(batch_idx)
    images_aug = seq(images=images)  # done by the library
    train_on_images(images_aug)

Example: Very Complex Augmentation Pipeline

Apply a very heavy augmentation pipeline to images (used to create the image at the very top of this readme):

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

# random example images
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
sometimes = lambda aug: iaa.Sometimes(0.5, aug)

# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.

seq = iaa.Sequential(
    [
        # apply the following augmenters to most images
        iaa.Fliplr(0.5), # horizontally flip 50% of all images
        iaa.Flipud(0.2), # vertically flip 20% of all images
        # crop images by -5% to 10% of their height/width
        sometimes(iaa.CropAndPad(
            percent=(-0.05, 0.1),
            pad_mode=ia.ALL,
            pad_cval=(0, 255)
        )),
        sometimes(iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
            translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)}, # translate by -20 to +20 percent (per axis)
            rotate=(-45, 45), # rotate by -45 to +45 degrees
            shear=(-16, 16), # 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)
        )),
        # execute 0 to 5 of the following (less important) augmenters per image
        # don't execute all of them, as that would often be way too strong
        iaa.SomeOf((0, 5),
            [
                sometimes(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
                iaa.OneOf([
                    iaa.GaussianBlur((0, 3.0)), # blur images with a sigma between 0 and 3.0
                    iaa.AverageBlur(k=(2, 7)), # blur image using local means with kernel sizes between 2 and 7
                    iaa.MedianBlur(k=(3, 11)), # blur image using local medians with kernel sizes between 2 and 7
                ]),
                iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5)), # sharpen images
                iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0)), # emboss images
                # search either for all edges or for directed edges,
                # blend the result with the original image using a blobby mask
                iaa.SimplexNoiseAlpha(iaa.OneOf([
                    iaa.EdgeDetect(alpha=(0.5, 1.0)),
                    iaa.DirectedEdgeDetect(alpha=(0.5, 1.0), direction=(0.0, 1.0)),
                ])),
                iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5), # add gaussian noise to images
                iaa.OneOf([
                    iaa.Dropout((0.01, 0.1), per_channel=0.5), # randomly remove up to 10% of the pixels
                    iaa.CoarseDropout((0.03, 0.15), size_percent=(0.02, 0.05), per_channel=0.2),
                ]),
                iaa.Invert(0.05, per_channel=True), # invert color channels
                iaa.Add((-10, 10), per_channel=0.5), # change brightness of images (by -10 to 10 of original value)
                iaa.AddToHueAndSaturation((-20, 20)), # change hue and saturation
                # either change the brightness of the whole image (sometimes
                # per channel) or change the brightness of subareas
                iaa.OneOf([
                    iaa.Multiply((0.5, 1.5), per_channel=0.5),
                    iaa.FrequencyNoiseAlpha(
                        exponent=(-4, 0),
                        first=iaa.Multiply((0.5, 1.5), per_channel=True),
                        second=iaa.LinearContrast((0.5, 2.0))
                    )
                ]),
                iaa.LinearContrast((0.5, 2.0), per_channel=0.5), # improve or worsen the contrast
                iaa.Grayscale(alpha=(0.0, 1.0)),
                sometimes(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)), # move pixels locally around (with random strengths)
                sometimes(iaa.PiecewiseAffine(scale=(0.01, 0.05))), # sometimes move parts of the image around
                sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1)))
            ],
            random_order=True
        )
    ],
    random_order=True
)
images_aug = seq(images=images)

Example: Augment Images and Keypoints

Augment images and keypoints/landmarks on the same images:

import numpy as np
import imgaug.augmenters as iaa

images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
images[:, 64, 64, :] = 255
points = [
    [(10.5, 20.5)],  # points on first image
    [(50.5, 50.5), (60.5, 60.5), (70.5, 70.5)]  # points on second image
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

# augment keypoints and images
images_aug, points_aug = seq(images=images, keypoints=points)

print("Image 1 center", np.argmax(images_aug[0, 64, 64:64+6, 0]))
print("Image 2 center", np.argmax(images_aug[1, 64, 64:64+6, 0]))
print("Points 1", points_aug[0])
print("Points 2", points_aug[1])

Note that all coordinates in imgaug are subpixel-accurate, which is why x=0.5, y=0.5 denotes the center of the top left pixel.

Example: Augment Images and Bounding Boxes

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
images[:, 64, 64, :] = 255
bbs = [
    [ia.BoundingBox(x1=10.5, y1=15.5, x2=30.5, y2=50.5)],
    [ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=50.5),
     ia.BoundingBox(x1=40.5, y1=75.5, x2=70.5, y2=100.5)]
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

images_aug, bbs_aug = seq(images=images, bounding_boxes=bbs)

Example: Augment Images and Polygons

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
images[:, 64, 64, :] = 255
polygons = [
    [ia.Polygon([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
    [ia.Polygon([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0)])]
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

images_aug, polygons_aug = seq(images=images, polygons=polygons)

Example: Augment Images and LineStrings

LineStrings are similar to polygons, but are not closed, may intersect with themselves and don't have an inner area.

import numpy as np
import imgaug as ia
import imgaug.augmenters as iaa

images = np.zeros((2, 128, 128, 3), dtype=np.uint8)  # two example images
images[:, 64, 64, :] = 255
ls = [
    [ia.LineString([(10.5, 10.5), (50.5, 10.5), (50.5, 50.5)])],
    [ia.LineString([(0.0, 64.5), (64.5, 0.0), (128.0, 128.0), (64.5, 128.0),
                    (128.0, 0.0)])]
]

seq = iaa.Sequential([
    iaa.AdditiveGaussianNoise(scale=0.05*255),
    iaa.Affine(translate_px={"x": (1, 5)})
])

images_aug, ls_aug = seq(images=images, line_strings=ls)

Example: Augment Images and Heatmaps

Heatmaps are dense float arrays with values between 0.0 and 1.0. They can be used e.g. when training models to predict facial landmark locations. Note that the heatmaps here have lower height and width than the images. imgaug handles that case automatically. The crop pixel amounts will be halved for the heatmaps.

import numpy as np
import imgaug.augmenters as iaa

# Standard scenario: You have N RGB-images and additionally 21 heatmaps per
# image. You want to augment each image and its heatmaps identically.
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
heatmaps = np.random.random(size=(16, 64, 64, 1)).astype(np.float32)

seq = iaa.Sequential([
    iaa.GaussianBlur((0, 3.0)),
    iaa.Affine(translate_px={"x": (-40, 40)}),
    iaa.Crop(px=(0, 10))
])

images_aug, heatmaps_aug = seq(images=images, heatmaps=heatmaps)

Example: Augment Images and Segmentation Maps

This is similar to heatmaps, but the dense arrays have dtype int32. Operations such as resizing will automatically use nearest neighbour interpolation.

import numpy as np
import imgaug.augmenters as iaa

# Standard scenario: You have N=16 RGB-images and additionally one segmentation
# map per image. You want to augment each image and its heatmaps identically.
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
segmaps = np.random.randint(0, 10, size=(16, 64, 64, 1), dtype=np.int32)

seq = iaa.Sequential([
    iaa.GaussianBlur((0, 3.0)),
    iaa.Affine(translate_px={"x": (-40, 40)}),
    iaa.Crop(px=(0, 10))
])

images_aug, segmaps_aug = seq(images=images, segmentation_maps=segmaps)

Example: Visualize Augmented Images

Quickly show example results of your augmentation sequence:

import numpy as np
import imgaug.augmenters as iaa

images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
seq = iaa.Sequential([iaa.Fliplr(0.5), iaa.GaussianBlur((0, 3.0))])

# Show an image with 8*8 augmented versions of image 0 and 8*8 augmented
# versions of image 1. Identical augmentations will be applied to
# image 0 and 1.
seq.show_grid([images[0], images[1]], cols=8, rows=8)

Example: Visualize Augmented Non-Image Data

imgaug contains many helper function, among these functions to quickly visualize augmented non-image results, such as bounding boxes or heatmaps.

import numpy as np
import imgaug as ia

image = np.zeros((64, 64, 3), dtype=np.uint8)

# points
kps = [ia.Keypoint(x=10.5, y=20.5), ia.Keypoint(x=60.5, y=60.5)]
kpsoi = ia.KeypointsOnImage(kps, shape=image.shape)
image_with_kps = kpsoi.draw_on_image(image, size=7, color=(0, 0, 255))
ia.imshow(image_with_kps)

# bbs
bbsoi = ia.BoundingBoxesOnImage([
    ia.BoundingBox(x1=10.5, y1=20.5, x2=50.5, y2=30.5)
], shape=image.shape)
image_with_bbs = bbsoi.draw_on_image(image)
image_with_bbs = ia.BoundingBox(
    x1=50.5, y1=10.5, x2=100.5, y2=16.5
).draw_on_image(image_with_bbs, color=(255, 0, 0), size=3)
ia.imshow(image_with_bbs)

# polygons
psoi = ia.PolygonsOnImage([
    ia.Polygon([(10.5, 20.5), (50.5, 30.5), (10.5, 50.5)])
], shape=image.shape)
image_with_polys = psoi.draw_on_image(
    image, alpha_points=0, alpha_face=0.5, color_lines=(255, 0, 0))
ia.imshow(image_with_polys)

# heatmaps
hms = ia.HeatmapsOnImage(np.random.random(size=(32, 32, 1)).astype(np.float32),
                         shape=image.shape)
image_with_hms = hms.draw_on_image(image)
ia.imshow(image_with_hms)

LineStrings and segmentation maps support similar methods as shown above.

Example: Using Augmenters Only Once

While the interface is adapted towards re-using instances of augmenters many times, you are also free to use them only once. The overhead to instantiate the augmenters each time is usually negligible.

from imgaug import augmenters as iaa
import numpy as np

images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

# always horizontally flip each input image
images_aug = iaa.Fliplr(1.0)(images=images)

# vertically flip each input image with 90% probability
images_aug = iaa.Flipud(0.9)(images=images)

# blur 50% of all images using a gaussian kernel with a sigma of 3.0
images_aug = iaa.Sometimes(0.5, iaa.GaussianBlur(3.0))(images=images)

Example: Multicore Augmentation

Images can be augmented in background processes using the method augment_batches(batches, background=True), where batches is a list/generator of imgaug.augmentables.batches.UnnormalizedBatch or imgaug.augmentables.batches.Batch. The following example augments a list of image batches in the background:

import skimage.data
import imgaug as ia
import imgaug.augmenters as iaa
from imgaug.augmentables.batches import UnnormalizedBatch

# Number of batches and batch size for this example
nb_batches = 10
batch_size = 32

# Example augmentation sequence to run in the background
augseq = iaa.Sequential([
    iaa.Fliplr(0.5),
    iaa.CoarseDropout(p=0.1, size_percent=0.1)
])

# For simplicity, we use the same image here many times
astronaut = skimage.data.astronaut()
astronaut = ia.imresize_single_image(astronaut, (64, 64))

# Make batches out of the example image (here: 10 batches, each 32 times
# the example image)
batches = []
for _ in range(nb_batches):
    batches.append(UnnormalizedBatch(images=[astronaut] * batch_size))

# Show the augmented images.
# Note that augment_batches() returns a generator.
for images_aug in augseq.augment_batches(batches, background=True):
    ia.imshow(ia.draw_grid(images_aug.images_aug, cols=8))

If you need more control over the background augmentation, e.g. to set seeds, control the number of used CPU cores or constraint the memory usage, see the corresponding multicore augmentation notebook or the API about Augmenter.pool() and imgaug.multicore.Pool.

Example: Probability Distributions as Parameters

Most augmenters support using tuples (a, b) as a shortcut to denote uniform(a, b) or lists [a, b, c] to denote a set of allowed values from which one will be picked randomly. If you require more complex probability distributions (e.g. gaussians, truncated gaussians or poisson distributions) you can use stochastic parameters from imgaug.parameters:

import numpy as np
from imgaug import augmenters as iaa
from imgaug import parameters as iap

images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

# Blur by a value sigma which is sampled from a uniform distribution
# of range 10.1 <= x < 13.0.
# The convenience shortcut for this is: GaussianBlur((10.1, 13.0))
blurer = iaa.GaussianBlur(10 + iap.Uniform(0.1, 3.0))
images_aug = blurer(images=images)

# Blur by a value sigma which is sampled from a gaussian distribution
# N(1.0, 0.1), i.e. sample a value that is usually around 1.0.
# Clip the resulting value so that it never gets below 0.1 or above 3.0.
blurer = iaa.GaussianBlur(iap.Clip(iap.Normal(1.0, 0.1), 0.1, 3.0))
images_aug = blurer(images=images)

There are many more probability distributions in the library, e.g. truncated gaussian distribution, poisson distribution or beta distribution.

Example: WithChannels

Apply an augmenter only to specific image channels:

import numpy as np
import imgaug.augmenters as iaa

# fake RGB images
images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)

# add a random value from the range (-30, 30) to the first two channels of
# input images (e.g. to the R and G channels)
aug = iaa.WithChannels(
  channels=[0, 1],
  children=iaa.Add((-30, 30))
)

images_aug = aug(images=images)

If this library has helped you during your research, feel free to cite it:

@misc{imgaug,
  author = {Jung, Alexander B.
            and Wada, Kentaro
            and Crall, Jon
            and Tanaka, Satoshi
            and Graving, Jake
            and Reinders, Christoph
            and Yadav, Sarthak
            and Banerjee, Joy
            and Vecsei, Gábor
            and Kraft, Adam
            and Rui, Zheng
            and Borovec, Jirka
            and Vallentin, Christian
            and Zhydenko, Semen
            and Pfeiffer, Kilian
            and Cook, Ben
            and Fernández, Ismael
            and De Rainville, François-Michel
            and Weng, Chi-Hung
            and Ayala-Acevedo, Abner
            and Meudec, Raphael
            and Laporte, Matias
            and others},
  title = {{imgaug}},
  howpublished = {\url{https://github.com/aleju/imgaug}},
  year = {2020},
  note = {Online; accessed 01-Feb-2020}
}

imgaug's People

Contributors

adamwkraft avatar aleju avatar apatsekin avatar avn3r avatar borda avatar chi-hung avatar creinders avatar erotemic avatar fchouteau avatar fmder avatar gaborvecsei avatar jgraving avatar joybanerjee08 avatar kacper1095 avatar kilianyp avatar nektor211 avatar petemir avatar raphaelmeudec avatar sarthakyadav avatar ss18 avatar stnk20 avatar vallentin avatar wkentaro avatar zhengrui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imgaug's Issues

Import imgaug error

When I was running "from imgaug import augmenters as iaa"
It has an error:
Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.

But the libmkl_avx2.so and libmkl_def.so are in the folder ~/anaconda2/lib/.

Image Translation

I am not sure how to ask this question properly so bare with me.

I am going to try to use an illustrative example:

  1. Consider a door on its hinges. Lets say door opens away from you (so that you have to push the door open rather than pull it towards you). Picture the door rotating on its hinge away from you, the door nob now appears further away from you, assuming you pushed the door and stayed in the same spot.

Is there an image translation that does the equivalent of this? So the "door knob" portions of the image would appear further away and the portions of the image closest to the "hinge" would appear closer.

Similar idea to these?

image

I am not looking for a 3d images I am just wanting some portions of my image closer and some portions further away.

Saving the resulted images one by one

Hello,

I am running the example code on my set of images and it's working great. But I want to save the resulted images one by one rather than saving them in a grid.

How can we convert these lines of code to save the images one by one rather than a grid?

grid = seq.draw_grid(image, cols=8, rows=8)
misc.imsave("examples_grid.jpg", grid)

Thanks

Understanding of Function "Sometimes()"

@aleju Thank you for providing such an amazing package which makes convenient for training deep networks for visual recognition. In the example codes,

# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
st = lambda aug: iaa.Sometimes(0.5, aug)

I am still confused by the functionality of Sometimes(0.5), would you please give more comments on this function? Thank you .

loading imgaug in python scripts vs in jupyter notebooks

2017-07-02 17 21 29
I met some errors in the use of the library. when I launch my anaconda environment and come into Python script,the following line works well:
from imgaug import augmenters as iaa
but when I come into Jupyter book rather than Python script, the same line will get an error.
why is it?

_2017-07-02t09-17-47 972z

Cant load augmenters

As said in the README, I copied the required files into my directory and ran the
from imgaug import augmenters as iaa

but I get an error

"ImportError: cannot import name 'augmenters'"

What is the "parents" parameter?

Can you tell me the meaning of this parameter?? It's almost everywhere

Also. Keypoints_on_images seems to be a list object. Are individual keypoints list objects themselves? Or numpy array?

AttributeError: 'module' object has no attribute 'ALL'

In the example of "heavy augmentations" I get the error

order=ia.ALL, # use any of scikit-image's interpolation methods
AttributeError: 'module' object has no attribute 'ALL'

I fixed this by replacing ia.ALL with "ALL"
Does the variable ALL accept other values?

PS: Great library. Very good work

Random Crop Position of a Fixed Size

Not a bug, a question.

Is it possible to use the crop function to randomly crop an image to a fixed size? I.e.: crop 256x256 images to 224x224 at a random position (within the image area)?

Thank you.

cval as parameter to __init__

[Enhancement] It would be nice if cval was added to the __init__ function as a parameter with a description. I understand you can set it manually at the moment but it takes a while to even see if the code supports setting the background color.

loading augmenters in python scripts vs in jupyter notebooks

I came across this error just now. When loading the augmenters module in Jupyter notebooks, the following line works

import augmenters as iaa

However, the same line fails if used in a script (in the same directory).
Any Idea what might be happening?? I think it's the (rather notorious) relative import system of python that's to blame.

PS: I forgot to tell you. I've been testing all of it in Python 3.5, and the import troubles (earlier and now this) are the only problems I have encountered.

What is the functionality of "seq.to_deterministic()"?

I notice that in the example of README.md, all examples related with augmenting landmark points have a following line of code. However, it seems that the examples that only augment images do not.
seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start
Is it necessary only for landmarks augmentation?

Can't do a Quick View of Sequential Aug similar to Readme

Hi Alex! This library is amazing!! However when running the code below I get this error: RuntimeError: Could not execute image viewer.

import augmenters as iaa
import numpy as np

images = np.random.randint(0, 255, (16, 128, 128, 3), dtype=np.uint8)
seq = iaa.Sequential([iaa.Fliplr(0.5), iaa.GaussianBlur((0, 3.0))])

seq.show_grid(images[0], cols=8, rows=8)

seq.show_grid([images[0], images[1]], cols=8, rows=8)

Python3 installation failed

I am using python3 without conda/env.

After running:
sudo pip3 install git+https://github.com/aleju/imgaug
I receive:

Collecting git+https://github.com/aleju/imgaug
  Cloning https://github.com/aleju/imgaug to /tmp/pip-mn453yqe-build
*** Error in `/usr/bin/python3': free(): invalid pointer: 0x0000000001af2990 ***
======= Backtrace: =========
...
======= Memory map: ========
...
Complete output from command python setup.py egg_info:
    running egg_info
    creating pip-egg-info/imgaug.egg-info
    writing top-level names to pip-egg-info/imgaug.egg-info/top_level.txt
    writing requirements to pip-egg-info/imgaug.egg-info/requires.txt
    writing pip-egg-info/imgaug.egg-info/PKG-INFO
    writing dependency_links to pip-egg-info/imgaug.egg-info/dependency_links.txt
    writing manifest file 'pip-egg-info/imgaug.egg-info/SOURCES.txt'
    reading manifest file 'pip-egg-info/imgaug.egg-info/SOURCES.txt'
    writing manifest file 'pip-egg-info/imgaug.egg-info/SOURCES.txt'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code -6 in /tmp/pip-mn453yqe-build/

I am using manually build OpenCV 3.1.0-dev (from pyimagesearch tutorial)

How to Install??

This looks amazing!! How to install and make use of this library?

Pickle Errors with Convolve Augmenters

Four augmenters that use the Convolve class: (Sharpen, Emboss, EdgeDetect and DirectEdgeDetect) are giving me pickle errors.

AttributeError: Can't pickle local object 'Emboss.<locals>.create_matrices'
Any easy way to fix this or make them pickle safe?

Regards @aleju

The augmentation is too slow!

Can you tell me which operation is slow so i can remove it. Thx!

here is my code

seq = iaa.Sequential([
        iaa.Fliplr(0.5), 
        iaa.Flipud(0.5),
        st(iaa.GaussianBlur((0, 1.0))), 
        st(iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))), 
        st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))),
        st(iaa.Sometimes(0.5,
            iaa.EdgeDetect(alpha=(0, 0.4)),
            iaa.DirectedEdgeDetect(alpha=(0, 0.4), direction=(0.0, 1.0)),
        )),
        st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)), 
        st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), 
        st(iaa.Add((-10, 10), per_channel=0.5)), 
        st(iaa.Multiply((0.5, 1.5), per_channel=0.5)),
        st(iaa.ContrastNormalization((0.5, 1.5), per_channel=0.5)),
        st(iaa.ElasticTransformation(alpha=(0.5, 2.5), sigma=0.25))
    ],
    random_order=True 
)

Publish on PyPi?

Could you publish this package to PyPi? It would make installing (e.g. pip install imgaug) and using it in other packages more easy. Thanks!

ZCA whitening and Related Questions.

it's more of a question rather than an issue.
Have you considered implementing ZCA whitening?
I have a simple implementation. But It doesn't seem to work on color images.

For example, on CIFAR10 dataset, the result of ZCA whitening is distorted images where only few color pixels remain and rest of the image gets all distorted.

Also, ZCA whitening acts as a low pass filter according to some sources. What are other techniques that act as a low pass filter? Gaussian Blurring???

Undefined `load_batches` in Example

In the example found here, the first example references some load_batches command that's not defined earlier. Searching in the augmenters.py file (imported as iaa) also did not yield any results).

What is that method?

Explanation of Affine Transforms

How does the Affine Transformation works?
What I mean to ask is, out of the selected batch, upon transforming the image batch with the transformer,
are there non-transformed images in the resulting batch or has every sample obtained certainly gone under some transformation?
LIke, I know the Flip transformers only transform specified ratio of images. What about other transformers?

Affine operations result in a cropped image or black margins

Hi, thanks for writing this repo! It's a great help in what I do :)

One question:
When I scale the image by 1.5 (affine transform) I lose information in the sides (it's more of a zoom than a resize function). Is there any way to simply resize the image?

Similar examples would be:

  • When I use affine to scale the image by 0.5, I'm getting an image with a lot of black and have to perform a crop later
  • When I rotate an image, the corners become cropped in the result.

The solution I am looking for is something like adding a flag "canvas_fits_image" which will perform the affine transformation and resize the canvas accordingly.

If it doesn't exist, I might add it myself sometime. Thanks!

ImportError: cannot import name augmenters

Hi, when I try to import the augmenters file from imgaug root folder using iPython I get this error
ImportError: cannot import name augmenters

Also, when I do the import import augmenters as iaa from the imgaug/imgaug folder, I get this error

       1 from __future__ import print_function, division, absolute_import
----> 2 from . import imgaug as ia
      3 from .parameters import StochasticParameter, Deterministic, Binomial, Choice, DiscreteUniform, Normal, Uniform
      4 from abc import ABCMeta, abstractmethod
      5 import random

ValueError: Attempted relative import in non-package

However, when importing from a python script it works fine

Augmentation by Perspective Transform

Hi!

I think the case of augmentation by perspective transform is very useful for real-life applications. Would you be interested in such function?

See http://www.euclideanspace.com/maths/discrete/groups/categorise/finite/cube/rotFace.png . Let the plane with a green 1 be an image. I'd suggest to parametrize the augmentation by angles around z and y, i.e. img_aug = aug_by_perspective(img, angle_vert, angle_horiz).

Implementing this should be relatively simple using OpenCV.

[MacOS] IOError when running generate_example_images.py

When I clone the repo, and run the generate_example_images.py, I get a runtime error:

$ cd ~/repos/imgaug
$ python generate_example_images.py
[draw_per_augmenter_images] Loading image...
[draw_per_augmenter_images] Initializing...
[draw_per_augmenter_images] Augmenting...
Traceback (most recent call last):
  File "generate_example_images.py", line 290, in <module>
    main()
  File "generate_example_images.py", line 18, in main
    draw_per_augmenter_images()
  File "generate_example_images.py", line 252, in draw_per_augmenter_images
    misc.imsave("examples.jpg", output_image.draw())
  File "generate_example_images.py", line 271, in draw
    rows_drawn = [self.draw_row(title, images, subtitles) for title, images, subtitles in self.rows]
  File "generate_example_images.py", line 277, in draw_row
    title_cell = ia.draw_text(title_cell, x=2, y=2, text=title, color=[0, 0, 0], size=12)
  File "/Users/erickim/repos/imgaug/imgaug/imgaug.py", line 129, in draw_text
    font = ImageFont.truetype("DejaVuSans.ttf", size)
  File "/usr/local/lib/python2.7/site-packages/PIL/ImageFont.py", line 238, in truetype
    return FreeTypeFont(font, size, index, encoding)
  File "/usr/local/lib/python2.7/site-packages/PIL/ImageFont.py", line 127, in __init__
    self.font = core.getfont(font, size, index, encoding)
IOError: cannot open resource

A quick fix is to modify imgaug/imgaug.py:128 and give the absolute path of the DejaVuSans.ttf file that is included in the repo:

diff --git a/imgaug/imgaug.py b/imgaug/imgaug.py
index 7e94c82..b2b2485 100644
--- a/imgaug/imgaug.py
+++ b/imgaug/imgaug.py
@@ -9,6 +9,7 @@ import math
 from scipy import misc
 import multiprocessing
 import threading
+import os
 import sys
 import six
 import six.moves as sm
@@ -125,7 +126,8 @@ def draw_text(img, y, x, text, color=[0, 255, 0], size=25):

     shape = img.shape
     img = Image.fromarray(img)
-    font = ImageFont.truetype("DejaVuSans.ttf", size)
+    font = ImageFont.truetype(os.path.join(os.path.abspath(os.path.split(__file__)[0]), "DejaVuSans.ttf"), size)
+
     context = ImageDraw.Draw(img)
     context.text((x, y), text, fill=tuple(color), font=font)
     img_np = np.asarray(img)

Thoughts on this change?

Data augmentation not working on all my dataset

Hello,

l passed this code generate_example_images.py (in a for loop ) on a dataset of 200.000 images. After that l noticed that not all the images are augmented . For instance , additivegaussian noise for some images it worked perfectly but for other they remained the same. This also apply for other data augmentation method.
What is wrong ?

Thank you

Getting black image!

First of all thank you very much for this.
When I try to run your example code with these two images, I always get a black image!
buckskin_s_000005
buckskin_s_000331

This is the whole code which is given in the first page , I just replaced the random numpy array statement with these two images!:

import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np

im = caffe.io.load_image('buckskin_s_000331.png')
im2 = caffe.io.load_image('buckskin_s_000005.png')
images = np.zeros([2,32,32,3])
images[0] = im
images[1] = im2
# Sometimes(0.5, ...) applies the given augmenter in 50% of all cases,
# e.g. Sometimes(0.5, GaussianBlur(0.3)) would blur roughly every second image.
st = lambda aug: iaa.Sometimes(0.3, aug)

# Define our sequence of augmentation steps that will be applied to every image
# All augmenters with per_channel=0.5 will sample one value _per image_
# in 50% of all cases. In all other cases they will sample new values
# _per channel_.
seq = iaa.Sequential([
        iaa.Fliplr(0.5), # horizontally flip 50% of all images
        iaa.Flipud(0.5), # vertically flip 50% of all images
        st(iaa.Superpixels(p_replace=(0, 1.0), n_segments=(20, 200))), # convert images into their superpixel representation
        st(iaa.Crop(percent=(0, 0.1))), # crop images by 0-10% of their height/width
        st(iaa.GaussianBlur((0, 3.0))), # blur images with a sigma between 0 and 3.0
        st(iaa.Sharpen(alpha=(0, 1.0), strength=(0.75, 1.5))), # sharpen images
        st(iaa.Emboss(alpha=(0, 1.0), strength=(0, 2.0))), # emboss images
        # search either for all edges or for directed edges
        st(iaa.Sometimes(0.5,
            iaa.EdgeDetect(alpha=(0, 0.7)),
            iaa.DirectedEdgeDetect(alpha=(0, 0.7), direction=(0.0, 1.0)),
        )),
        st(iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.2), per_channel=0.5)), # add gaussian noise to images
        st(iaa.Dropout((0.0, 0.1), per_channel=0.5)), # randomly remove up to 10% of the pixels
        st(iaa.Invert(0.25, per_channel=True)), # invert color channels
        st(iaa.Add((-10, 10), per_channel=0.5)), # change brightness of images (by -10 to 10 of original value)
        st(iaa.Multiply((0.5, 1.5), per_channel=0.5)), # change brightness of images (50-150% of original value)
        st(iaa.ContrastNormalization((0.5, 2.0), per_channel=0.5)), # improve or worsen the contrast
        st(iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
            translate_px={"x": (-16, 16), "y": (-16, 16)}, # translate by -16 to +16 pixels (per axis)
            rotate=(-45, 45), # rotate by -45 to +45 degrees
            shear=(-16, 16), # shear by -16 to +16 degrees
            order=ia.ALL, # use any of scikit-image's interpolation methods
            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)
        )),
        st(iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25)) # apply elastic transformations with random strengths
    ],
    random_order=True # do all of the above in random order
)

images_aug = seq.augment_images(images)
plt.imshow(images_aug[0])
plt.show()

image

what is wrong here?

Augment Keypoints not always correct

Hi!

Thanks a lot for the library, it really works well. The only thing I have issues with is the keypoint augmentation, which works sometimes but in other cases it returns a set of negative coordinate values which are then of course not plotted inside the image when I use 'draw_on_image' and show it using matplotlib afterwards.
I'm currently using this augmentation sequence:

seq = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontally flip 50% of the images
    iaa.Flipud(0.5), # vertically flip 50% of all images
    #iaa.GaussianBlur(sigma=(0, 3.0)), # blur images with a sigma of 0 to 3.0
    iaa.Affine(
            scale={"x": (0.8, 1.2), "y": (0.8, 1.2)}, # scale images to 80-120% of their size, individually per axis
            translate_px={"x": (-16, 16), "y": (-16, 16)}, # translate by -16 to +16 pixels (per axis)
            rotate=(-45, 45), # rotate by -45 to +45 degrees
            shear=(-16, 16), # shear by -16 to +16 degrees
            order=ia.ALL, # use any of scikit-image's interpolation methods
            #cval=(0, 1.0), # if mode is constant, use a cval between 0 and 1.0
            #mode=ia.ALL # use any of scikit-image's warping modes (see 2nd image from the top for examples)
        )
    ],
    random_order=True # do all of the above in random order
)

seq_det = seq.to_deterministic() # call this for each batch again, NOT only once at the start

For example, when I use this input image:

image

I get results like this:

image

Does anyone have an idea what could be the problem?

Thanks a lot!

UPDATE: It seems like the keypoint augmentation only works if a single command is used in iaa.Affine() at once. If use several, only the first is use to augment keypoints. What works is to use several iaa.Affine() commands in series, with each containing a different image manipulation, which is fine for now.

setup.py does not recognize opencv2 of Anaconda

When run the setup, error happens. opencv is installed on Anaconda.
Is it possible to install imgaug on Anaconda?
...
Processing ./dist/imgaug-0.2.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-K5MRPU-build/setup.py", line 6, in
raise Exception("Could not find package 'cv2' (OpenCV). It cannot be automatically installed, so you will have to manually install it.")
Exception: Could not find package 'cv2' (OpenCV). It cannot be automatically installed, so you will have to manually install it.

OSError: [Errno 2] No such file or directory

I run "sudo -H python -m pip install imgaug-0.2.4.tar.gz" command to user my pip in Cellar to install imgaug, and got the following error.
And i tried "pip install imgaug-0.2.4.tar.gz", and it seems that it uses 2.7.10 version python in osx system. However, my opencv is installed in 2.7.11 python in Cellar folder.
How can i solve this problem?


Processing ./imgaug-0.2.4.tar.gz
Error [Errno 2] No such file or directory while executing command python setup.py egg_info
Exception:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/usr/local/lib/python2.7/site-packages/pip/commands/install.py", line 335, in run
wb.build(autobuilding=True)
File "/usr/local/lib/python2.7/site-packages/pip/wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in _prepare_file
abstract_dist.prep_for_dist()
File "/usr/local/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "/usr/local/lib/python2.7/site-packages/pip/req/req_install.py", line 439, in run_egg_info
command_desc='python setup.py egg_info')
File "/usr/local/lib/python2.7/site-packages/pip/utils/init.py", line 667, in call_subprocess
cwd=cwd, env=env)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in init
errread, errwrite)
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Other type of augmentation : add vertical and horizontal lines to image based sequence

Hello,

l have a set of images based sequence. l want to increase my dataset by doing some data augmentation as follow. for each image add horizontal line, vertical line at on the most left char and most right char and in the middle.

Here is the original image :

number

and l want to get something like this :

Adding horizontal line :

number-h

Adding vertical line on the middle

number-v-middle

Adding vertical line on the left
number-v

Adding vertical line on the right

number-v2

thank you

Rotate Affine augment Keypoints

Hi,

I am using sequential augment with only rotate affine transform with -30, 30 range.

Then I wanted to augment keypoints. I did this with the point (100, 100). But, the augmented point is not in the correct position. I ran the key point augmentation for other sequential non-affine augmentations and they seemed to work fine.

I used the same visualization technique in the README.md with the "draw_on_image" method

Can you please help me with this.

Changes : Review documentation style

I have added (some) documentation for the Augmenter and Noop classes in the augmenters.py file.

Review the changes in My fork and make suggestions.

This is just a glimpse. Much work will be needed on documentation (as more often than not I am not quite sure what a parameter signifies, etc).

Whether I need to install Keras ?

I found the "Sequential model" in examples of this library. I learn that the model belongs to Keras. Whether it means that I need to install Keras in addition ?

_2017-07-02t10-03-28 546z

Unexpected determinism

Hi, I've got the following code:

def augment(im, y):
    im_arr = np.array(im)

    # See documentation for details regarding transformations: https://github.com/aleju/imgaug
    fliplr_rate = 0.5
    angle = 10
    additive, contrast_norm = (45, 0.1)
    gaussian_noise, dropout = (0.05, 0.01)
    shear, shift = (2, 20)
    aug_img_only = iaa.Sequential([
        iaa.Sometimes(0.5, iaa.OneOf([
            iaa.Add((-additive, additive)),
            iaa.ContrastNormalization((1 - contrast_norm, 1 + contrast_norm))
        ])),
        iaa.Sometimes(0.5, iaa.OneOf([
            iaa.AdditiveGaussianNoise(scale=gaussian_noise * 255, per_channel=True),
            iaa.Dropout(dropout)
        ]))
    ])
    aug_img_mask = iaa.Sequential([
        iaa.Fliplr(fliplr_rate),
        iaa.Affine(rotate=(-angle, angle)),
        iaa.Sometimes(0.5, iaa.Affine(
            shear=(-shear, shear),
            translate_px={'x': (-shift, shift), 'y': (-shift, shift)})
                      )
    ])
    aug_img_only.reseed()
    aug_img_only_det, aug_img_mask_det = aug_img_only.to_deterministic(), aug_img_mask.to_deterministic()
    im_arr = aug_img_only_det.augment_images([im_arr])[0]
    im_arr = aug_img_mask_det.augment_images([im_arr])[0]
    y = aug_img_mask_det.augment_images([y])[0]

    im = Image.fromarray(im_arr)
    return im, y

I've got a ML system which has input images and known masks of areas of interest, which I later want to predict. I want to augment the images and the masks in the same way for some transformations, and apply other transformations (such as dropout, etc.) only to the original image.
Here, in the code, im is the original image in PIL object format, im_arr is the original image transformed to numpy array, and y is the mask numpy array.
Now, everytime I run this code, for example, 5 times, with the same picture and mask, I get the same 5 augmentations. Meaning, that the first picture comes out the same every time, so does the second and so on.
Just to clarify, here is the code I use to run it:

for i in range(5):
    im = Image.open('image.jpg')
    y = np.load('mask.npy')
    im, y = augment(im, y)

Why would this behavior happen? I reinstantiate the augmenters every time the function is called (as can be seen in the code), and only after the reinstantiation do I call to_deterministic().
What am I missing?

Thanks in advance!

processed image is abnormal

when I load a image , do some augmentations ,the result is abnormal with lots of strides
`
seq = iaa.Sequential([
#iaa.Flipud(0.5),

#iaa.Dropout(0.02, name="Dropout"),

#iaa.GaussianBlur(5)

#iaa.AdditiveGaussianNoise(0, 10, True)

iaa.Sharpen(alpha=(0, 1.0), lightness=(0.75, 1.5))

#iaa.Crop(px=(0, 16)), # crop images from each side by 0 to 16px (randomly chosen)

#iaa.Fliplr(0.5), # horizontally flip 50% of the images

#iaa.GaussianBlur(sigma=(0, 5.0)) # blur images with a sigma of 0 to 3.0

])
img = scipy.misc.imread(image_dir)
images_aug = seq.augment_images(img)
misc.imshow(images_aug)
`

Generalization : `np.uint8, [0-255]` -> any dtype, range

Hi,

Is it difficult to generalize image augmentations without the restrictions of np.uint8 and 0-255 ?
I saw in the code that there are some augmentations that have a hard-coded np.clip and astype np.uint8.
I started locally to modify test.py to launch tests on float32 data, however tests are not exaustive...
I'm interested in this feature and can help with implementation.

Add augmentation for segmentation/annotation

Segmentation labels (annotation) are usually provided as a matrix of labels (integers).
For this task the same augmentation as the images should be performed, except interpolation should be 'nearest neighbours' rather than bilinear or similar one.
Some form of retrieving the sampled parameters (for example: rotation angle was 32 degrees) would be usefull so we could re-create the same augmentation, but with different interpolation.
Deterministic augmentation doesn't help in this case.
In the meantime, as a workaround we can use:


from imgaug import augmenters as iaa
rotate_max=30
seq = iaa.Sequential([
    iaa.Fliplr(0.5), # horizontally flip 50% of the images
],random_state=0)
rotate=iaa.Affine(rotate=(0, rotate_max),mode='edge')
rotate_nn_interpolation=iaa.Affine(rotate=(0, rotate_max),order=[0],mode='edge')
seq_nn_interpolation=seq.deepcopy()
seq.append(rotate)
seq_nn_interpolation.append(rotate_nn_interpolation)

aug_seed=0
for batch in...:
    batch_images = images[batch, :, :, :]
    batch_labels = labels[batch, :,:]
    seq.reseed(aug_seed)
    seq_nn_interpolation.reseed(aug_seed)
    batch_images=seq.augment_images(batch_images)
    batch_labels=seq_nn_interpolation.augment_images(batch_labels)
    aug_seed+=1

Same distortions for all images in a batch

Suppose I have video-frames and I would like to apply same distortions/transformation/augmentations to all the frames while passing them in a single batch(and then other batch with other parameters for the same transformation set). How can this be done?
I am sorry if my query seems to be too naive

Rotation, which changes shape

As far as I can see your rotation is intended for square images. I am interested in using rotation like in the following code:

from scipy.ndimage.interpolation import rotate
# some code
new_image = rotate(image, angle, reshape=True)

Thus, it will transform image with shape (h,w) into image with shape (w,h) without any scaling by one of the axies. Can I make it using your library in some way or should I make a pull request?

Image Augmentation for 3D images

Hi,
Imgaug is a very good tool for generating images. I am trying to generate different data from my 3D MRI image data. As the code is only supporting 2D images or RGB images, imgaug is giving failure in case of my data. This might seem as my personal different problem, but I want your suggestion of how can we change the code so that it will support 3D images. My images are 25625660 sizes. Its a dicom image, and it has 60 slices. Generating augmented 2D image will not touch Z axis, which is not acceptable in my case. Please suggest something using which I can achieve the same feature, mainly I will use affine,piecewise affine and elastic transformation for generating new data.

Thanks

Install Script is a success

Hello!
This is not an issue, but instead a review of the installation process.

The installation script has successfully worked for

  1. virtualenvs
  2. global installs
  3. conda and anaconda

tested with both python 2.7 and python3.5

This was good work !!! 👍

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.