Hello ,
I am using this code which was written by another user for diabetic retinopathy detection. I am trying to split my dataset to either Yes or No folders, which is in a folder that is created by the code called 'Train Dataset Two Classes\'. The issue that I am facing is that it only writes the image to either only YES or NO. I have no idea why it does this.
Here's the code:
% Two-class Data path
two_class_datapath='Train Dataset Two Classes\';
myFolder = 'C:\Users\adamy\Desktop\MATLAB\train_images\';
% Class Names
class_names={'No','Yes'};
mkdir(sprintf('%s%s',two_class_datapath,class_names{1}))
mkdir(sprintf('%s%s',two_class_datapath,class_names{2}))
% Read the Excel Sheet with Labels
[num_data,text_data]=xlsread('train.xlsx');
% Determine the Labels
train_labels=num_data(:,1);
% Merge all labels marked into Mild, Medium, Severe and Proliferative DR
% into a single category 'Yes'
train_labels(train_labels~=0)=2;
% Rest of the dataset belongs to 'No' category
train_labels(train_labels==0)=1;
% Filename
filename=text_data(2:end,1);
% Now, write these images 2-folders 'Yes' or 'No' for us to develop a deep
% learning architecture utilizing Deep learning toolbox
% Determine the Files put them in separate folder
for idx=1:length(filename)
% You could uncomment if you would like to see live progress
fprintf('Processing %d among %d files:%s \n',idx,length(filename),filename{idx})
% Read the image
current_filename=strrep(filename{idx}, char(39), '');
img=imread(sprintf('%s%s.png',myFolder,current_filename));
% Write the image in the respective folder
imwrite(img,sprintf('%s%s%s%s.png',two_class_datapath,class_names{train_labels(idx)},'\',current_filename));
clear img;
end

 採用された回答

Voss
Voss 2022 年 4 月 4 日

0 投票

Looks like the train_labels you are getting from the first column of that xlsx file are all NaN (and NaN is not equal to 0 so they all get set to 2 and go into the YES group):
% Read the Excel Sheet with Labels
[num_data,text_data]=xlsread('train.xlsx');
% Determine the Labels
train_labels=num_data(:,1)
train_labels = 3662×1
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
% Merge all labels marked into Mild, Medium, Severe and Proliferative DR
% into a single category 'Yes'
train_labels(train_labels~=0)=2;
% Rest of the dataset belongs to 'No' category
train_labels(train_labels==0)=1;
nnz(train_labels == 1)
ans = 0
nnz(train_labels == 2)
ans = 3662
The solution is to get train_labels from the second column instead:
% Determine the Labels
train_labels=num_data(:,2)
train_labels = 3662×1
2 4 1 0 0 4 0 2 2 1
% Merge all labels marked into Mild, Medium, Severe and Proliferative DR
% into a single category 'Yes'
train_labels(train_labels~=0)=2;
% Rest of the dataset belongs to 'No' category
train_labels(train_labels==0)=1;
nnz(train_labels == 1)
ans = 1805
nnz(train_labels == 2)
ans = 1857

2 件のコメント

adam yousry
adam yousry 2022 年 4 月 4 日
Yup that fixed it thank you very much. I though NAN meant 0 or non existent so i assumed MATLAB would just skip it,
Thank again
Voss
Voss 2022 年 4 月 4 日
You're welcome!

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 4 月 3 日

0 投票

Exactly what does "split" mean to you? Do you have image files in one input folder, myFolder, and you want to copy (with copyfile) or create (with imwrite) into either a "Yes" subfolder or a "No" folder or subfolder?
I don't like making really long complicated filenames the way you did it:
imwrite(img,sprintf('%s%s%s%s.png',two_class_datapath,class_names{train_labels(idx)},'\',current_filename));
It's easy to make mistakes that way. Plus you didn't use fullfile() which nicely takes care of whether there is a slash between filename components so you don't have to worry about that. I'd do:
% Create input file name.
baseFileName = sprintf('%s.png', current_filename);
inputFullFileName = fullfile(myFolder, baseFileName);
fprintf('Reading in image : "%s".\n', inputFullFileName);
% Read in input image.
img = imread(inputFullFileName);
% Display input image (optional).
image(img);
axis('on', 'image');
impixelinfo;
drawnow;
% Save to output folder.
outputFolder = fullfile(two_class_datapath, class_names{train_labels(idx)});
outputFullFileName = fullfile(outputFolder, baseFileName);
fprintf('Saving image to : "%s".\n', outputFullFileName);
imwrite(img, outputFullFileName);
% Or alternatively you can use copyfile() instead of imwrite().
% copyfile(inputFullFileName, outputFullFileName)

3 件のコメント

adam yousry
adam yousry 2022 年 4 月 3 日
編集済み: adam yousry 2022 年 4 月 3 日
Hi, by split i mean I have a folder which contains all the images, using the excel dataset I have I want to place them in their respectful folder. I have tried your method and it still saves it to only YES. Im assuming its something to do with my logic at
train_labels(train_labels~=0)=2;
% Rest of the dataset belongs to 'No' category
train_labels(train_labels==0)=1;
but im not sure why. In the dataset, if an eye has DR(diabetic retinopathy) it has a value of 1-4 depending on the severity. Although I will admit, your method has made my code look cleaner and i thank you for that
Image Analyst
Image Analyst 2022 年 4 月 4 日
You have to look at your original train_labels and see why they are all non-zero. If you still need help, attach your original train_labels (before you to the reassignments you did above) in a .mat file.
save('answers.mat', 'train_labels')
or (probably better) attach 'train.xlsx'
adam yousry
adam yousry 2022 年 4 月 4 日
Train.xlss is attached to this and so is num_data. Num_data is the last variable which has all the correct variables before it becomes train_labels.Where all the variables become 2.
[num_data,text_data]=xlsread('train.xlsx');
% Determine the Labels
train_labels=num_data(:,1);
Here is also the code which extracts num_data and places it in train_labels

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

カテゴリ

ヘルプ センター および File ExchangeGet Started with Statistics and Machine Learning Toolbox についてさらに検索

製品

リリース

R2021a

質問済み:

2022 年 4 月 3 日

コメント済み:

2022 年 4 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by