- 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
Manage image dataset with .csv file
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello everyone, I have been working on an ssd object detector and i have the DeepFruits dataset which comes with about 16k images to train but the labels are all on a .csv file. I have created a datastore for the image side of the training set but when it comes to the labels side I'm not sure where to begin. I have already imported the .csv file and i have a struct with two fields. Below i will include what those two fields look like. I was planning on using the boxLabelDatastore but I'm not sure if i even can using that file. Both of these datastore files were going to be then combined and used with trainSSDObjectDetector. Any help is appreciated. Thank you.
0 commentaires
Réponses (1)
Jacob Mathew
le 24 Mai 2024
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:
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:
Best Regards,
Jacob
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!