MATLAB Answers

problem in recognizing in CNN model

조회 수: 3(최근 30일)
ALHasaan ALHarazi
ALHasaan ALHarazi 2021년 4월 1일
편집: ALHasaan ALHarazi 2021년 4월 12일
I traind a network on 4 types of fruits using alexnet example in the. The model classifies the type of the fruit in testing. however when I test any image like a labtob image the model classify it into one of the fruits.
what can I do so that if there is no fruit in the image the model predect no fruit.
imds = imageDatastore('Dataset_alexnet', ...
'IncludeSubfolders',true, ...
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,'randomized');
numTrainImages = numel(imdsTrain.Labels);
idx = randperm(numTrainImages,16);
for i = 1:16
I = readimage(imdsTrain,idx(i));
net = alexnet;
inputSize = net.Layers(1).InputSize
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',20, ...
'MaxEpochs',10, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
DATA_NET = trainNetwork(augimdsTrain,layers,options);
[YPred,~] = classify(DATA_NET,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
[filename,pathname] = uigetfile('*.*','select the input image');
filewithpath = strcat(pathname,filename);
I = imread(filewithpath);
I = imresize(I,[227 227]);
[label,Prob] = classify(DATA_NET,I);

채택된 답변

ALHasaan ALHarazi
ALHasaan ALHarazi 2021년 4월 12일
편집: ALHasaan ALHarazi 2021년 4월 12일
I used Faster R CNN object detector to solve this problem. It detects if there is one of the 4 fruits ro even all of them in the image . and when testing any other image it does not classify it since the model cant detect any of the fruits.

추가 답변(1개)

Divya Gaddipati
Divya Gaddipati 2021년 4월 12일
Traditionally, when a model is trained on a particular set of classes, it assumes that only known classes appear in the test environment. So, whatever test image you provide, the model only knows how to classify it into one of the classes that it is trained on. That means when an image with an unknown object is given, the model tries to match it with the known classes and returns the class with the closest match. This is generally known as the Open set classification problem.
There are few options to handle this:
- Create the fifth class: Unknown. In this case, you must add other random images to the train data and label them unknown. Then train the classifier and see what the result is.
- Thresholding. In the case where the object in the test image is one of the 4 fruits, probability will be high. In the case where the image is something else, the probabilities are most probably about the same for all 4 classes. In other words, probability for neither of the classes really stand out. That is a situation in which you pick the highest probability of the ones generated and set the output class to be the class of that probability, regardless the value of probability is 0.4 or something like that. To resolve this, you can set a threshold at, let's say 0.7, and say if neither of probabilities is over that threshold, there is something else on the picture.
There are other alternatives as well which you could explore.
  댓글 수: 1
ALHasaan ALHarazi
ALHasaan ALHarazi 2021년 4월 12일
Thank you for your help. I Appreciate it.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by