MATLAB: Trying to move images to different folder based on CNN classification, and failing

classificationimage analysisimage processingMATLABneural networkneural networks

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)

Best Answer

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
Related Question