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:
labelsTable = readtable('Labels_Test.csv');
imds = imageDatastore('Fruits_Dataset_Test', 'IncludeSubfolders', true, 'LabelSource', 'none');
imdsFileNames = cellfun(@(x) regexp(x, '([^\\]+)$', 'match', 'once'), imds.Files, 'UniformOutput', false);
fruitNames = labelsTable.Properties.VariableNames(2:end);
labelsTable.CombinedLabel = repmat({[]}, size(labelsTable, 1), 1);
for i = 1:size(labelsTable, 1)
presentFruits = fruitNames(labelsTable{i, 2:end-1} == 1);
labelsTable.CombinedLabel{i} = strjoin(presentFruits, ',');
lookupTable = labelsTable(:, {'FileName', 'CombinedLabel'});
imdsLabels = cell(size(imds.Files));
for i = 1:length(imdsFileNames)
filename = imdsFileNames{i};
idx = find(strcmp(lookupTable.FileName, filename));
imdsLabels{i} = lookupTable.CombinedLabel{idx};
imdsLabels{i} = 'Unknown';
imds.Labels = categorical(imdsLabels);
Inspecting the data source, we can see that the labels have now been added:
Best Regards,
Jacob