필터 지우기
필터 지우기

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

조회 수: 9 (최근 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 CenterFile Exchange에서 Deep Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by