Trying to move images to different folder based on CNN classification, and failing
1 次查看(过去 30 天)
显示 更早的评论
Hi folks, I'm trying to pass my images for classification to a CNN and then move each image (after being classified) to a folder based on its classification. Currently, I don't get any errors but the code just compiles without any outputs...Any help would be most appreciated!
for k = 1:fileNumber
currentImage = fullfile(imageList(k).folder, imageList(k).name);
img = imread(currentImage);
labelName = classify(DaveNet, img);
switch labelName
case 'Anisotropy'
movefile(currentImage, AnisotropyClassificationPath);
case 'Isotropy'
movefile(currentImage, IsotropyClassificationPath);
case 'Filler'
movefile(currentImage, FillerClassificationPath);
otherwise
movefile(currentImage, BadCropsClassificationPath);
end
end
My CNN for reference:
imds = imageDatastore(TrainingDatasetPath, 'IncludeSubfolders',true,...
'LabelSource','foldernames');
labelCount = countEachLabel(imds)
numTrainFiles = 500;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
layers = [
imageInputLayer([227 227 3])
convolution2dLayer(3, 8, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 16, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 32, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 64, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
convolution2dLayer(3, 128, 'Padding', 'same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(4)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm','InitialLearnRate',0.001, ...
'MaxEpochs', 5,'Shuffle','every-epoch', ...
'ValidationData',imdsValidation,'ValidationFrequency',30, ...
'Verbose',false, 'Plots','training-progress');
net = trainNetwork(imdsTrain, layers, options);
YPred = classify(net, imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation)/numel(YValidation)
0 个评论
采纳的回答
Jon
2020-3-13
编辑:Jon
2020-3-13
I would suggest checking the return status and error messages from movefile by assigning some left hand arguments to the movefile command.
For example
switch labelName
case 'Anisotropy'
[isGood,msg] = movefile(currentImage, AnisotropyClassificationPath);
case 'Isotropy'
[isGood,msg] = movefile(currentImage, IsotropyClassificationPath);
% and so on
end
% check for errors
if ~isGood
disp(msg)
end
Maybe the destination path is not formulated correctly or you have a permission issue with the destination folder
Output Arguments
status — Move status
0 | 1
Move status, indicating if the attempt to move the file or folder is successful, returned as 0 or 1. If the attempt is successful, the value of statusis 1. Otherwise, the value is 0.
Data Types: logical
msg — Error message
character vector
Error message, returned as a character vector. If an error or warning occurs, msg contains the message text of the error or warning. Otherwise, msg is empty, ''.
msgID — Error message identifier
character vector
Error message identifier, returned as a character vector. If an error or warning occurs, msgID contains the message identifier of the error or warning. Otherwise, msgID is e
2 个评论
Jon
2020-3-13
Are you referring to the numel of YValidation?
I'm not that familiar with the CNN functions that you are using, but if you have less validation data than you think you should have maybe the total number of data sets is less than you think so when you use
numTrainFiles = 500;
[imdsTrain,imdsValidation] = splitEachLabel(imds,numTrainFiles,'randomize');
if the length of imds is say only 509 then you will only have 509-numTrainFiles, or 509-500 = 9
Just a thought.
Otherwise I would suggest single stepping through your code using the MATLAB debug tool and looking at the values of the various variables as you go, and you should be able to see where things are going wrong. If you aren't familiar with the debugger it is probably the single best thing you can learn about to make your life better in MATLAB, please see https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!