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

21 ビュー (過去 30 日間)
Abdulaziz Alotaibi
Abdulaziz Alotaibi 2021 年 2 月 16 日
回答済み: Abhishek Gupta 2021 年 2 月 19 日
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)

採用された回答

Abhishek Gupta
Abhishek Gupta 2021 年 2 月 19 日
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: -

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by