imageDataAugmenter - Configure image data augmentation - MATLAB (original) (raw)

Configure image data augmentation

Creation

Syntax

Description

aug = imageDataAugmenter creates animageDataAugmenter object with default property values consistent with the identity transformation.

aug = imageDataAugmenter(`Name,Value`) configures a set of image augmentation options using name-value pairs to setproperties. You can specify multiple name-value pairs. Enclose each property name in quotes.

example

Properties

expand all

FillValue — Fill value

numeric scalar | numeric vector

Fill value used to define out-of-bounds points when resampling, specified as a numeric scalar or numeric vector.

For grayscale and color images, the default fill value is0. For categorical images, the default fill value is an '<undefined>' label and thetrainnet function ignores filled pixels when training.

Example: 128

RandXReflection — Random reflection

false (default) | true

Random reflection in the left-right direction, specified as a logical scalar. When RandXReflection is true (1), each image is reflected horizontally with 50% probability. When RandXReflection isfalse (0), no images are reflected.

RandYReflection — Random reflection

false (default) | true

Random reflection in the top-bottom direction, specified as a logical scalar. When RandYReflection is true (1), each image is reflected vertically with 50% probability. When RandYReflection isfalse (0), no images are reflected.

RandRotation — Range of rotation

[0 0] (default) | 2-element numeric vector | function handle

Range of rotation, in degrees, applied to the input image, specified as one of the following.

By default, augmented images are not rotated.

Example: [-45 45]

RandScale — Range of uniform scaling

[1 1] (default) | 2-element numeric vector | function handle

Range of uniform (isotropic) scaling applied to the input image, specified as one of the following.

By default, augmented images are not scaled.

Example: [0.5 4]

RandXScale — Range of horizontal scaling

[1 1] (default) | 2-element vector of positive numbers | function handle

Range of horizontal scaling applied to the input image, specified as one of the following.

By default, augmented images are not scaled in the horizontal direction.

Note

If you specify RandScale, thenimageDataAugmenter ignores the value ofRandXScale when scaling images.

Example: [0.5 4]

RandYScale — Range of vertical scaling

[1 1] (default) | 2-element vector of positive numbers | function handle

Range of vertical scaling applied to the input image, specified as one of the following.

By default, augmented images are not scaled in the vertical direction.

Note

If you specify RandScale, thenimageDataAugmenter ignores the value ofRandYScale when scaling images.

Example: [0.5 4]

RandXShear — Range of horizontal shear

[0 0] (default) | 2-element numeric vector | function handle

Range of horizontal shear applied to the input image, specified as one of the following. Shear is measured as an angle in degrees, and is in the range (–90, 90).

By default, augmented images are not sheared in the horizontal direction.

Example: [0 45]

RandYShear — Range of vertical shear

[0 0] (default) | 2-element numeric vector | function handle

Range of vertical shear applied to the input image, specified as one of the following. Shear is measured as an angle in degrees, and is in the range (–90, 90).

By default, augmented images are not sheared in the vertical direction.

Example: [0 45]

RandXTranslation — Range of horizontal translation

[0 0] (default) | 2-element numeric vector | function handle

Range of horizontal translation applied to the input image, specified as one of the following. Translation distance is measured in pixels.

By default, augmented images are not translated in the horizontal direction.

Example: [-5 5]

RandYTranslation — Range of vertical translation

[0 0] (default) | 2-element numeric vector | function handle

Range of vertical translation applied to the input image, specified as one of the following. Translation distance is measured in pixels.

By default, augmented images are not translated in the vertical direction.

Example: [-5 5]

Object Functions

augment Apply identical random transformations to multiple images

Examples

collapse all

Create Image Data Augmenter to Resize and Rotate Images

Create an image data augmenter that preprocesses images before training. This augmenter rotates images by random angles in the range [0, 360] degrees and resizes images by random scale factors in the range [0.5, 1].

augmenter = imageDataAugmenter( ... 'RandRotation',[0 360], ... 'RandScale',[0.5 1])

augmenter = imageDataAugmenter with properties:

       FillValue: 0
 RandXReflection: 0
 RandYReflection: 0
    RandRotation: [0 360]
       RandScale: [0.5000 1]
      RandXScale: [1 1]
      RandYScale: [1 1]
      RandXShear: [0 0]
      RandYShear: [0 0]
RandXTranslation: [0 0]
RandYTranslation: [0 0]

Create an augmented image datastore using the image data augmenter. The augmented image datastore also requires sample data, labels, and an output image size.

[XTrain,YTrain] = digitTrain4DArrayData; imageSize = [56 56 1]; auimds = augmentedImageDatastore(imageSize,XTrain,YTrain,'DataAugmentation',augmenter)

auimds = augmentedImageDatastore with properties:

     NumObservations: 5000
       MiniBatchSize: 128
    DataAugmentation: [1x1 imageDataAugmenter]
  ColorPreprocessing: 'none'
          OutputSize: [56 56]
      OutputSizeMode: 'resize'
DispatchInBackground: 0

Preview the random transformations applied to the first eight images in the image datastore.

minibatch = preview(auimds); imshow(imtile(minibatch.input));

Figure contains an axes object. The hidden axes object contains an object of type image.

Preview different random transformations applied to the same set of images.

minibatch = preview(auimds); imshow(imtile(minibatch.input));

Figure contains an axes object. The hidden axes object contains an object of type image.

Train Network with Augmented Images

Train a convolutional neural network using augmented image data. Data augmentation helps prevent the network from overfitting and memorizing the exact details of the training images.

Load the sample data, which consists of synthetic images of handwritten digits. XTrain is a 28-by-28-by-1-by-5000 array, where:

labelsTrain is a categorical vector containing the labels for each observation.

Set aside 1000 of the images for network validation.

idx = randperm(size(XTrain,4),1000); XValidation = XTrain(:,:,:,idx); XTrain(:,:,:,idx) = []; TValidation = labelsTrain(idx); labelsTrain(idx) = [];

Create an imageDataAugmenter object that specifies preprocessing options for image augmentation, such as resizing, rotation, translation, and reflection. Randomly translate the images up to three pixels horizontally and vertically, and rotate the images with an angle up to 20 degrees.

imageAugmenter = imageDataAugmenter( ... 'RandRotation',[-20,20], ... 'RandXTranslation',[-3 3], ... 'RandYTranslation',[-3 3])

imageAugmenter = imageDataAugmenter with properties:

       FillValue: 0
 RandXReflection: 0
 RandYReflection: 0
    RandRotation: [-20 20]
       RandScale: [1 1]
      RandXScale: [1 1]
      RandYScale: [1 1]
      RandXShear: [0 0]
      RandYShear: [0 0]
RandXTranslation: [-3 3]
RandYTranslation: [-3 3]

Create an augmentedImageDatastore object to use for network training and specify the image output size. During training, the datastore performs image augmentation and resizes the images. The datastore augments the images without saving any images to memory. trainnet updates the network parameters and then discards the augmented images.

imageSize = [28 28 1]; augimds = augmentedImageDatastore(imageSize,XTrain,labelsTrain,'DataAugmentation',imageAugmenter);

Specify the convolutional neural network architecture.

layers = [ imageInputLayer(imageSize)

convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer   

maxPooling2dLayer(2,'Stride',2)

convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer   

maxPooling2dLayer(2,'Stride',2)

convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer   

fullyConnectedLayer(10)
softmaxLayer];

Specify the training options. Choosing among the options requires empirical analysis. To explore different training option configurations by running experiments, you can use the Experiment Manager app.

opts = trainingOptions('sgdm', ... 'MaxEpochs',15, ... 'Shuffle','every-epoch', ... 'Plots','training-progress', ... 'Metrics','accuracy', ... 'Verbose',false, ... 'ValidationData',{XValidation,TValidation});

Train the neural network using the trainnet function. For classification, use cross-entropy loss. By default, the trainnet function uses a GPU if one is available. Training on a GPU requires a Parallel Computing Toolbox™ license and a supported GPU device. For information on supported devices, see GPU Computing Requirements (Parallel Computing Toolbox). Otherwise, the trainnet function uses the CPU. To specify the execution environment, use the ExecutionEnvironment training option.

net = trainnet(augimds,layers,"crossentropy",opts);

Tips

Version History

Introduced in R2017b