ImageDatastore from text file

1 回表示 (過去 30 日間)
Lin
Lin 2020 年 11 月 9 日
編集済み: Subhadeep Koley 2020 年 11 月 10 日
Hi
I need to classify some image data using the trained resnet network.
For the testing data, I need to call them using text file which consist from a sequence directory such as follows:
testingdata.txt:
/DataSet/label1/1.jpg
/DataSet/label1/2.jpg
/DataSet/label1/3.jpg
/DataSet/label1/4.jpg
/DataSet/label2/1.jpg
/DataSet/label2/2.jpg
/DataSet/label2/3.jpg
/DataSet/label2/4.jpg
How to read those image dataset directory from text file, to be used as testing data for deep learning classification.?

採用された回答

Subhadeep Koley
Subhadeep Koley 2020 年 11 月 9 日
Try this code snippet. The below code will read images, whose locations are specified in the testingdata.txt file and will also label them according to their foldernames.
% Read the filenames and put them in cell array
fId = fopen('testingdata.txt');
tline = fgetl(fId);
tlines = cell(0, 1);
while ischar(tline)
tlines{end+1, 1} = (tline);
tline = fgetl(fId);
end
% Create an imageDatastore object
imds = imageDatastore(tlines, 'LabelSource', 'foldernames');
  2 件のコメント
Lin
Lin 2020 年 11 月 10 日
thank you very much for your answer.
How about if the directory in the text file list is not uniform such as follows:
testingdata.txt:
/DataSet/label1/imageA/1.jpg
/DataSet/label1/imageB/imageA1/1.jpg
/DataSet/label1/imageC/A/3.jpg
/DataSet/label1/imageD/a/1.jpg
/DataSet/label2/imageA1/1a/2.jpg
/DataSet/label2/imageB/a/2a.jpg
/DataSet/label2/imageC/C1/1c.jpg
/DataSet/label2/imageA/1a/4.jpg
Subhadeep Koley
Subhadeep Koley 2020 年 11 月 10 日
編集済み: Subhadeep Koley 2020 年 11 月 10 日
@LS This can be achieved with a bit more processing. There are a lot of ways to achieve this. The below code might help you.
% Read the filenames and put them in cell array
fId = fopen('testingdata.txt');
tline = fgetl(fId);
tlines = cell(0, 1);
while ischar(tline)
tlines{end+1, 1} = tline;
tline = fgetl(fId);
end
% Create an imageDatastore object
imds = imageDatastore(tlines);
% Modify image labels of the imageDatastore object
expression = 'label[\d]';
funHandle = @(str)regexpi(str, expression, 'match');
customLabels = cellfun(funHandle, squeeze(imds.Files));
imds.Labels = categorical(customLabels);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDisplay and Presentation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by