Bounding Box Is Not Showing For RCNNobjectdetector
1 visualizzazione (ultimi 30 giorni)
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:
gunfolder = '/Users/mmgp/Desktop/gunsGT';
%% 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)
%% Counting Images In Each Category "If not equal this will create issues"
%% Making Category The Same Number Of Images
%% Splitting Inputs Into Training and Testing Sets
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%% 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);
% Idx = 1;
% for j=1:a
% for k=1:b
%% 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 = [
%% 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( ...
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain, ...
%% Utilising Data Augmentation For Resizing Validation Data
% implemented without specifying overfit prevention procedures
% By not specifying these procedures the system will be precise via
%% 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, ...
%% Implementing **WITH** The RCNN Object Detector
opts = trainingOptions('sgdm',...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 1e-5, ...
% 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);
% for i = 1:4
% I = readimage(imdsValidation,idx(i));
% label = YPred(idx(i));
%% 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
% 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);
%% Remove the image directory from the path.
Shashank Gupta il 23 Gen 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.