Clear Filters
Clear Filters

How to use a transformed data store to train the network ?

3 views (last 30 days)
Whenever I use transformation for an Image data store (DS) for resizing the images, the resulting transformed DS consists of only images without its labels.
resizedImds = transform(Image DS, @(x) imresize(x, [227 227]));
I need a transformed data store with the labels stored in image data store [Labels= imagesdatastore.Labels].

Answers (1)

Lokesh
Lokesh on 25 Apr 2024
Hi Badavath,
It is my understanding that you are encountering an issue when transforming an Image Datastore, as the resulting transformed Image Datastore only contains the resized images, without retaining the original labels associated with each image.
As a workaround, you can explicitly add the labels to the output data of your transform function, using a cell array to combine it with the image data. To do this, you will also need to specify that the TransformedDatastore should pass in a second argument that contains the info for each read image.
Here is a sample code snippet:
imds = imageDatastore(fullfile(matlabroot,"toolbox","matlab"),...
"IncludeSubfolders",true,"FileExtensions",".tif","LabelSource","foldernames")
imdsReSz = transform(imds,@resizeAndLabel,"IncludeInfo",true);
imgReSz1 = read(imdsReSz)
imgReSz1 =
1×2 cell array
{227×227×3 uint8} {[demos]} % contains image and label
function [resizedImageWithLabel, info] = resizeAndLabel(image, info)
% Resize the image to the desired size
targetSize = [227, 227];
resizedImage = imresize(image, targetSize);
% Combine the resized image and its label into a cell array
% This maintains the association between the image and its label
resizedImageWithLabel = {resizedImage, info.Label};
end
Please refer to the following MATLAB Answer that is related to a similar issue:

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!