Bounding Box Is Not Showing For RCNNobjectdetector
1 view (last 30 days)
Show older comments
Hi professionals,
I managed to get to the bounding box and it is not consistant, sometimes is it showing and working most times it is not!!
The image is appearing with the Region Of Interest but the bounding box is not around that said region!!!
Can someone help to me understanding what I am doing incorrectly or interpret the status of the issue Pretty Pretty Please!!!
everything seems to work but the bounding box!!
Here is my code:
clear
clc
% deepNetworkDesigner
gunfolder = '/Users/mmgp/Desktop/gunsGT';
save('gunlables.mat','gunfolder');
%% Specifying Image Amount In Specified Folder
total_images = numel(gunfolder);
%% Accessing Content of Folder TrainingSet Using Datastore
imds = imageDatastore(gunfolder,'IncludeSubFolders',true,'LabelSource','Foldernames');
%% Setting Output Function(images my have size variation resizing for consistency with pretrain net)
imds.ReadFcn=@(loc)imresize(imread(loc),[227,227]);
%% Counting Images In Each Category "If not equal this will create issues"
tbl=countEachLabel(imds);
%% Making Category The Same Number Of Images
minSetCount=min(tbl{:,2});
%% Splitting Inputs Into Training and Testing Sets
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
size(imdsTrain)
%% Loading Pretrained Network
net = alexnet; %Trained on 1million+ images/classify images into 1000 object categories
% analyzeNetwork(net) % Display Alexnet architecture & network layer details
%% Alter InputSize Of 1st Layer/ Alexnet Image requirements is 277 width 277 height by 3 colour channels
inputSize = net.Layers(1).InputSize;%Displays the input size of Alexnet
%% Counting Total Number Of Images Including Subfolders **IF AVAILABLE**
imgTotal = length(imds.Files);
%% Displaying Multiple Randomized Images Within The Dataset
% a = 4;
% b = 4;
% n = randperm(imgTotal, a*b);
%
% figure(),
% Idx = 1;
% for j=1:a
% for k=1:b
% img=readimage(imds,n(Idx));
% subplot(a,b,Idx)
% imshow(img);
% Idx=Idx+1;
% end
% end
%% Replace Final Layer/Last 3 Configure For 1000 classes
% Finetuning these 3 layers for new classification
% Extracting all Layers except the last 3
layersTransfer = net.Layers(1:end-3);
%% List the image categories/Clases:
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',25,'BiasLearnRateFactor',25)
softmaxLayer
classificationLayer];
%% Training The Network
% Resizing images in datastore to meet Alexnet's size requirements
% Utilising Augmented Data Store for automatic resizing of training images
%% Augmented Data Store Prevents Over Fitting By Randomly Flipping Along The Vertical Axis
% Stopping the network from memorizing exact details of the training data
% Also Randomly Translates them up to 30 pixels horizontally & Vertically
pixelRange = [-30 30];
imageAugmenter = imageDataAugmenter( ...
'RandXReflection',true, ...
'RandXTranslation',pixelRange, ...
'RandYTranslation',pixelRange);
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
'DataAugmentation',imageAugmenter);
%% Utilising Data Augmentation For Resizing Validation Data
% implemented without specifying overfit prevention procedures
% By not specifying these procedures the system will be precise via
% predicitons
%% Resizing Images, Assists With Preventing Overfitting
augmentedTrainingSet = augmentedImageDatastore(inputSize ,imdsTrain,'ColorPreprocessing', 'gray2rgb');
augimdsValidation = augmentedImageDatastore(inputSize,imdsValidation,'ColorPreprocessing', 'gray2rgb');
%% Specifying Training Options
% Keep features from earlier layers of pretrained networked for transfer learning
% Specify epoch training cycle, the mini-batch size and validation data
% Validate the network for each iteration during training
% (SGDM)groups the full dataset into disjoint mini-batches This reaches convergence faster
% as it updates the network?s weight value more frequently increase the
% computationl speed
%% Implementing For ***VISUAL*** Graphical Representations
% options = trainingOptions('sgdm', ...
% 'MiniBatchSize',32, ...
% 'MaxEpochs',20, ...
% 'InitialLearnRate',0.004, ...
% 'Shuffle','every-epoch', ...
% 'ValidationData',augimdsValidation, ...
% 'ValidationFrequency',3, ...
% 'Verbose',true, ...
% 'Plots','training-progress');
%% Implementing **WITH** The RCNN Object Detector
opts = trainingOptions('sgdm',...
'Momentum',0.9,...
'MiniBatchSize', 20,...
'InitialLearnRate', 1e-4,...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 1e-5, ...
'MaxEpochs', 80);
% opts = trainingOptions('sgdm','MiniBatchSize', 130,'InitialLearnRate', 1e-4,'MaxEpochs', 200);
[height,width,numChannels, ~] = size(imdsTrain);
imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize);
%% Training network Consisting Of Transferred & New Layers.
netTransfer = trainNetwork(augmentedTrainingSet,layers,opts);
%% Classifying Validation Images Utilising Fine-tuned Network
[YPred,scores] = classify(netTransfer,augimdsValidation);
%% Displaying 4 Validation Image Samples With Predicted Labels
% idx = randperm(numel(imdsValidation.Files),4);
% figure
% for i = 1:4
% subplot(2,2,i)
% I = readimage(imdsValidation,idx(i));
% imshow(I)
% label = YPred(idx(i));
% title(string(label));
% end
%% Calculating Validation Data Classification Accuracy (Accuracy Labels Predicted Accurately By Network)
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation);
%% Training the R-CNN detector. Training can take a few minutes to complete.
% Loading .MAT file, the ground truths and the Network layers
load('gTruth.mat');
% Positive and Negative Overlap Range Controls Which Image Patch is Used
rcnn = trainRCNNObjectDetector(gTruth, netTransfer, opts, 'NegativeOverlapRange', [0 0.3]);
%% Testing the R-CNN detector on a test image.
testimg = imread('Gun00011.jpg');
[bbox, score, label] = detect(rcnn, testimg, 'MiniBatchSize', 20);
%% Display strongest detection result.
[score, idx] = max(score)
bbox = bbox(idx, :)
annotation = sprintf('%s: (Confidence = %f)', label(idx), score)
detectedImg = insertObjectAnnotation(testimg, 'rectangle', bbox, annotation);
figure
imshow(detectedImg);
%% Remove the image directory from the path.
rmpath(gunfolder);
0 Comments
Answers (1)
Shashank Gupta
on 23 Jan 2020
Hi Matthew Parris,
I see what you are intent to do, But I have some confusion/doubt in your code. I can’t seem to find the RCNN Box regression Layer, the one which is responsible to give you the bounding box coordinates. I am also wondering how you managed to get some bounding box. The code which you share only contains classificationLayer which just seem to classify the number of objects the network should detect. May be try adding "box regression layer" and see if that solve your problem.
Let me know if something good comes up.
5 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!