How to i build a structure like digitTest4DArrayData with OWN data
3 views (last 30 days)
Show older comments
Hello,
i'd like to use the imageDataAugmenter for my own dataset. To achieve this, i need to create my image database in the
same structure like digitTest4DArrayData has.
My images consist of .png files, listed up in different directories for the labels.
So basically, i want to run the following example code:
[XTrain,YTrain] = digitTrain4DArrayData;
imageSize = [56 56 1];
auimds = augmentedImageDatastore(imageSize,XTrain,YTrain,'DataAugmentation',augmenter)
minibatch = preview(auimds);
imshow(imtile(minibatch.input));
The only difference is that i want to use my own dataset, instead of the testset digitTrain4DArrayData.
How can i achieve this? I did not find any documentation on this issue.
Thank you!
Answers (1)
Tejas
on 17 Sep 2024
Hello Matthias,
To convert your dataset into the same structure as the ‘digitTest4DArrayData' test dataset, use the 'imageDatastore' function, as shown in the code snippet below.
function [XTrain,YTrain] = convertTodigitTrain4DArrayData(rootFolder)
% Create an imageDatastore to help load and manage the images
imds = imageDatastore(rootFolder, ...
'IncludeSubfolders', true, ...
'LabelSource', 'foldernames', ...
'FileExtensions', '.png');
% Read all images and their labels
numImages = numel(imds.Files);
imageSize = [56 56];
XTrain = zeros([imageSize 1 numImages], 'like', readimage(imds, 1));
YTrain = imds.Labels;
% Resize and load images into the 4D array
for i = 1:numImages
img = readimage(imds, i);
img = imresize(img, imageSize);
XTrain(:, :, :, i) = img;
end
end
To use the 'convertTodigitTrain4DArrayData'function, provide the path to the root directory containing the label subdirectories as the input argument.
[XTrain,YTrain] = convertTodigitTrain4DArrayData('dataset');
For more information on 'imageDatastore', use the following command to access the documentation:
web(fullfile(docroot, "matlab/ref/matlab.io.datastore.imagedatastore.html"))
0 Comments
See Also
Categories
Find more on Image Data Workflows 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!