problem in recognizing in CNN model

1 view (last 30 days)
Hello
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, ...
'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.8,'randomized');
numTrainImages = numel(imdsTrain.Labels);
idx = randperm(numTrainImages,16);
figure
for i = 1:16
subplot(4,4,i)
I = readimage(imdsTrain,idx(i));
imshow(I)
end
net = alexnet;
inputSize = net.Layers(1).InputSize
layersTransfer = net.Layers(1:end-3);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
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, ...
'Plots','training-progress');
DATA_NET = trainNetwork(augimdsTrain,layers,options);
[YPred,~] = classify(DATA_NET,augimdsValidation);
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation)
save DATA_NET
******FOR TESTING
[filename,pathname] = uigetfile('*.*','select the input image');
filewithpath = strcat(pathname,filename);
I = imread(filewithpath);
I = imresize(I,[227 227]);
figure
imshow(I)
[label,Prob] = classify(DATA_NET,I);
title({char(label),num2str(max(Prob),2)});

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.
  2 Comments
ALHasaan ALHarazi
ALHasaan ALHarazi on 12 Apr 2021
Thank you for your help. I Appreciate it.
Gaia Cioffi
Gaia Cioffi on 13 May 2022
Hi, I'm facing a similar problem training a CNN, but I cannot use any of these options to solve it.
Can you suggest me other alternatives? Thank you.

Sign in to comment.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!