augmentedImageDatastore for image segmentation
Show older comments
Hello,
I wish to create an augmented image datastore that I can use in training. Previously I used to augment all image pairs with my own custom function before training but then my project supervisor gave me the idea to augment during training to let the network see many more different images. I understand another approach would be to just augment even more images before training and decrease the number of epochs but I wish to succeed using MATLAB's built in augmenter as well. Here is the problem I am facing:
size(X_train) = [224 224 3 200]
size(Y_train) = [224 224 200]
For the provided example in MATLAB's documentation of augmentedImageDatastore, Y_train is just a 1D categorical array. In my case, I need to augment the X data as well as the Y data, with the same augmentation on each pair. I tried something like this:
%% Built-in augmenter
imageAugmenter = imageDataAugmenter( ...
'RandRotation',[0 360], ...
'RandXTranslation',[-5 5], ...
'RandYTranslation',[-5 5], ...
'RandXReflection', true, ...
'RandYReflection', true );
training = combine(ds_X_training, ds_Y_training);
aug_training = augmentedImageDatastore([224 224 3], training, 'DataAugmentation', imageAugmenter);
And I get the error:

This works fine, however:
X_aug_training = augmentedImageDatastore([224 224 3], ds_X_aug_training, 'DataAugmentation', imageAugmenter);
I understand the error arrises because I can't feed a combined datastore or pixelLabelDatastore into augmentedImageDatastore. I saw some examples on augmentation of pixellabel images; Augment Pixel Labels for Semantic Segmentation but the article did not mention anything about augmentedImageDatastore, which is the one I am interested in because it wont save augmented images in memory while training.
Accepted Answer
More Answers (1)
Birju Patel
on 1 Apr 2024
0 votes
I recommend combining imageDatastore and pixelLabelDatastore and then using a transform to implement data augmentation for semantic segmentation.
Here is an example:
augmentedImageDatastore was not designed to augment data for semantic segmentation.
2 Comments
Matt J
on 1 Apr 2024
Unfortunately, though, this requires the Computer Vision Toolbox.
Massimo Del Guasta
13 minutes ago
Edited: Massimo Del Guasta
9 minutes ago
Thank You, the method works, but with a slight change in my case: when transforming the label dataset, depending on the applied transformation (e.g. rotation), some 'pixels' of the (cathegorical) label are declared as <undefined> (equivalent to 'NaN'..) at the end of the transformation (e.g. the edges of the picture in the case of rotation). This means that the augmented database contains a new class after the transformation, and can't be used in the training of my sematic segmentation net. In order to avoid this inconvenience, I had to force <undefined> pixels into one of my existing classes, (e.g in my case the 'background' class):
ds = combine(imdsTrain,pxds); %%%% my original semantic segmentation database
augds = transform(ds,@aumentoSemanticDataStore); %%%% the augmented sem. datastore
%%%%%%%%%%%%%%%%%%% the called function: %%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = aumentoSemanticDataStore(data)
I = data{1};C = data{2};
% Define your random affine transform.
tform = randomAffine2d("XReflection",true,'XTranslation',[-20 20], 'YTranslation',[-10 10]);
rout = affineOutputView(size(I),tform);
% Transform image and bounding box labels.
augmentedImage = imwarp(I,tform,"OutputView",rout);
augmentedLabel = imwarp(C,tform,"OutputView",rout);
%%%%% correcting for the <undefined> pixels!!
TF = isundefined(augmentedLabel);
augmentedLabel(TF)="background";
% Return augmented data.
out = {augmentedImage,augmentedLabel};
end
Categories
Find more on Semantic Segmentation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
