MATLAB Answers

problem in recognizing in CNN model

2 views (last 30 days)
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);

Accepted Answer

ALHasaan ALHarazi
ALHasaan ALHarazi on 12 Apr 2021
Edited: ALHasaan ALHarazi on 12 Apr 2021
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.

More Answers (1)

Divya Gaddipati
Divya Gaddipati on 12 Apr 2021
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 Comment
ALHasaan ALHarazi
ALHasaan ALHarazi on 12 Apr 2021
Thank you for your help. I Appreciate it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!