MATLAB Answers

augmentedImageDatastore center crop does not return datastore with labels

12 views (last 30 days)
OJ27 on 14 May 2020
Edited: Sai Bhargav Avula on 26 May 2020
I have an image datastore which includes images and labels. Before feeding to the network, I want to crop the images at the center. However, I noticed that imdsTrain_crop does not have label information as imdsTrain does.
imdsTrain_crop = augmentedImageDatastore([28,28],imdsTrain,'OutputSizeMode','centercrop');
Notice below how the ImageDatastore object has Labels but the augmentedImageDatastore does not. Is there any way to work around this?
I know that augmentedImageDatastore.Files will have information of the filepath for each image, which I can read and then label accoordingly, but this seems troublesome when there could be a simpler solution.


Sign in to comment.

Answers (1)

Sai Bhargav Avula
Sai Bhargav Avula on 26 May 2020
Edited: Sai Bhargav Avula on 26 May 2020
One way to address this is to use pixelLabelDatastore for loading the labels
pixelLabelImageDatastore to create the datastore for training.
imdsTrain = imageDataStore(imageDir);
pxdsTrain = pixelLabelDatastore(labelDir,ClassNames,labelIds);
trainingData = pixelLabelImageDatastore(imds,pxds,'OutputSizeMode','centercrop','OutputSize',[28,28]);
or transform function over the datastores.
Hope this helps!


OJ27 on 26 May 2020
aupxdsTrain = augmentedImageDatastore([28, 28],pxdsTrain);
Running this line returns an error "Invalid input data type."
OJ27 on 26 May 2020
The reason why this is troublesome is because I would like to use
YTest = imdsTest.Labels;
for classification accuracy. But after cropping, there is no .Labels action to extract the class label. This seems something that should be added to the augmentedImageDatastore operation.
Sai Bhargav Avula
Sai Bhargav Avula on 26 May 2020
The augumentedImageDatastore generally is used to randomly perturbs(augument) the training data for each epoch, so that each epoch uses a slightly different data set. This is majorly to resize images to make them compatible with the input size of your deep learning network. Hence it doesnot hold the label information. But you can use the augmented datastore for training the network and label info is taken inherently.
For explicit training as mentioned there are many ways like performing transform on the datastores seperately etc., Here I have mentioned to use pixelLabelImageDatastore. I have attached the example code for better understanding.
dataSetDir = fullfile(toolboxdir('vision'),'visiondata','triangleImages');
imageDir = fullfile(dataSetDir,'trainingImages');
labelDir = fullfile(dataSetDir,'trainingLabels');
classNames = ["triangle","background"];
labelIDs = [255 0];
imds = imageDatastore(imageDir);
auimds = augmentedImageDatastore([28,28],imds);
pxds = pixelLabelDatastore(labelDir, classNames, labelIDs);
trainingData = pixelLabelImageDatastore(imds,pxds,'OutputSizeMode','centercrop','OutputSize',[28,28]);
When you read the data
ans =
1×2 table
inputImage pixelLabelImage
_____________ ___________________
{28×28 uint8} {28×28 categorical}
Which you can use to evaluate accuracies explicitly. I hope this explains and what you are looking for

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!