Hey Ivan, I understand you are having difficulty in mapping the labels to the images that you have loaded in the datastore.
Since each image has more than one label i.e. the list of fruits, you need to create a single label that has the list of fruits in the appropriate label.
A step-by-step process to achieve this can be summarized as follows:
- Loading the image as a datastore
- Loading the labels .CSV File
- Adding a new column called “CombinedLabel” which is populated with the list of fruits that have the value 1 in that row.
- Iterating through the list of image’s names and assigning the corresponding combined label to it
I have downloaded the data set from the following link:
The example code snippet below should help you get started, ensuring that the dataset is on MATLAB Path:
clear;
% Loading the label csv as a table
labelsTable = readtable('Labels_Test.csv');
% Loading the images as a datastore
imds = imageDatastore('Fruits_Dataset_Test', 'IncludeSubfolders', true, 'LabelSource', 'none');
% Extract file names from the image datastore
% File name with extension is the string that comes after the last backslash
imdsFileNames = cellfun(@(x) regexp(x, '([^\\]+)$', 'match', 'once'), imds.Files, 'UniformOutput', false);
% Create a new column in labelsTable for combined labels
% First column is 'FileName' hence ignored
fruitNames = labelsTable.Properties.VariableNames(2:end);
% Initialize empty cell array
labelsTable.CombinedLabel = repmat({[]}, size(labelsTable, 1), 1);
% Loop through each row in the table and combine labels for each image
for i = 1:size(labelsTable, 1)
% Last column in the combined label hence ignored
presentFruits = fruitNames(labelsTable{i, 2:end-1} == 1);
% Combine fruit names with commas
labelsTable.CombinedLabel{i} = strjoin(presentFruits, ',');
end
% Create a lookup table with filenames and their combined labels
lookupTable = labelsTable(:, {'FileName', 'CombinedLabel'});
% Align labels with the filenames in the datastore
% Initialize a cell array to hold labels for each image in the datastore
imdsLabels = cell(size(imds.Files));
% Map each filename in the datastore to its label
for i = 1:length(imdsFileNames)
filename = imdsFileNames{i};
idx = find(strcmp(lookupTable.FileName, filename));
if ~isempty(idx)
imdsLabels{i} = lookupTable.CombinedLabel{idx};
else
imdsLabels{i} = 'Unknown'; % Or any other placeholder for unmatched images
end
end
% Assign the labels back into to the image datastore
imds.Labels = categorical(imdsLabels);
Inspecting the data source, we can see that the labels have now been added:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1702891/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1702896/image.png)
Best Regards,
Jacob