How do I use my trained CNN model to predict new pictures?

12 visualizaciones (últimos 30 días)
Hello there,
I created simple classification model using the following example:
and I got 91% accuracy, now I want to use this CNN model to try it on new images, How do I do that?
this is my code:
clear;
clc;
outputFolder = fullfile("binary_dataset");
rootFolder = fullfile(outputFolder, "Categories");
categories = {'Anomaly','No-Anomaly'}; % names of the folders
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource','foldernames');
tbl = countEachLabel(imds);
[imdsTrain,imdsValidation] = splitEachLabel(imds, 0.8, 'randomize');
inputSize = [40 24 1];
numClasses = 2;
layers = [
imageInputLayer(inputSize)
convolution2dLayer(5,20,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,20,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(5,20,'Padding',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',200, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(imdsTrain,layers,options);
YPred = classify(net,imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)

Respuesta aceptada

Abhishek Gupta
Abhishek Gupta el 19 de Feb. de 2021
Hi,
As per my understanding, you want to make predictions for new input using your trained network. You can do the same using the 'predict()' function in MATLAB: -
predictions = predict(net,newImages);
For more information, check out the documentation here: -

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by