How to classify a folder of images after training network

1 visualización (últimos 30 días)
Nathnael Esayas Bulti
Nathnael Esayas Bulti el 11 de Abr. de 2022
Respondida: Walter Roberson el 11 de Abr. de 2022
I am trying to classify a whole folder of images into 2 classes (eg. cats and dogs) and then I want to save the images classified in one class (eg. dogs).
So, in the last bit of the code I was able to classify 1 image at a time but would like to do it for multiple images and then save the images of one class.
outputFolder = fullfile('DataToClean');
rootFolder = fullfile(outputFolder, 'categories');
categories = {'Cars', 'NotCars'};
imds = imageDatastore(fullfile(rootFolder, categories),'LabelSource','foldernames');
tbl = countEachLabel(imds);
cars = find(imds.Labels == 'Cars',1);
notcars = find(imds.Labels == 'NotCars',1);
% figure
% subplot(2,2,1);
% imshow(readimage(imds,cars));
% subplot(2,2,2);
% imshow(readimage(imds,notcars));
net = resnet50();
% figure
% plot(net)
% title('Architecture Of ResNet-50')
% set(gca,'YLim',[150 170]);
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize,...
trainingSet, 'colorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,...
testSet, 'colorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
featureLayer = 'fc1000';
trainingFeatures = activations(net, augmentedTrainingSet,...
featureLayer, 'MiniBatchSize', 32, 'Outputas', 'columns');
trainingLabels = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures, trainingLabels,...
'Learner', 'Linear', 'ObservationsIn', 'columns');
testFeatures = activations(net, augmentedTestSet,...
featureLayer, 'MiniBatchSize', 32, 'Outputas', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
testLabels = testSet.Labels;
confMat = confusionmat(testLabels, predictLabels);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat));
newImage = imread(fullfile('test101.jpg'));
ds = augmentedImageDatastore(imageSize,...
newImage, 'colorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ds,...
featureLayer, 'MiniBatchSize', 32, 'Outputas', 'columns');
imLabels = predict(classifier, imageFeatures, 'ObservationsIn', 'columns');

Respuestas (1)

Walter Roberson
Walter Roberson el 11 de Abr. de 2022
newImage = imread(fullfile('test101.jpg'));
replace that with an image data store. Something like
newImage = imageDatastore(FolderToClassify);
where FolderToClassify has been set to the name of the folder to run predictions on.
You would then take the imLabels and use them to decide where to move (or copy) each of the images in the data store.

Categorías

Más información sobre Deep Learning Toolbox en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by