MATLAB Answers

Bounding box errors for rcnnobjectdetector

2 views (last 30 days)
Matpar
Matpar on 21 Jan 2020
Commented: Matpar on 27 Jan 2020
Hi Professionals,
I need some assitance solving this challenge please, I thought I had it but this issues is still evading me with the bounding boxes!
The bounding boxes was showing around the region of interests but now it is not!
This is my code and I will upload the data for it!
Please help me see my error and elaborate on this so I will not make the same mistake twice!
Thank you in advance for assistance
code:
clear
clc
%% Step 1 Deploying deepNetworkDesigner to edit Layers physically
gunfolder = '/Users/mmgp/Desktop/gunsGT';
save('gunlables.mat','gunfolder');
%% Step 2 Specifying Image Amount In Specified Folder
total_images = numel(gunfolder);
%% Step 3 Accessing Content of Folder TrainingSet Using Datastore
imds = imageDatastore(gunfolder,'IncludeSubFolders',true,'LabelSource','Foldernames');
%% Step 4 Setting Output Function(images may have size variation resizing for consistency with pretrain net)
imds.ReadFcn=@(loc)imresize(imread(loc),[227,227]);
%% Step 5 Counting Images In Each Category "If not equal this will create issues"
tbl=countEachLabel(imds);
%% Step 6 Making Category The Same Number Of Images
minSetCount=min(tbl{:,2});
%% Step 7 Splitting Inputs Into Training and Testing Sets
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
size(imdsTrain);
%% Step 8 Loading Pretrained Network
net = alexnet; %Trained on 1million+ images/classify images into 1000 object categories
% analyzeNetwork(net) % Display Alexnet architecture & network layer details
%% Step 9 Altering 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
%% Step 9 Counting Total Number Of Images Including Subfolders **IF AVAILABLE**
imgTotal = length(imds.Files);
%% Step 10 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),title('Randomised Images Displayed');
% Idx=Idx+1;
% end
% end
%% Step 11 Replacing 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);
%% Step 12 Specifying Image Categories/Clases:
numClasses = numel(categories(imdsTrain.Labels));
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',25,'BiasLearnRateFactor',25);
softmaxLayer
classificationLayer];
%% Step 13 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
%% Step 14 Resizing Images, Assists With Preventing Overfitting
augmentedTrainingSet = augmentedImageDatastore(inputSize ,imdsTrain,'ColorPreprocessing', 'gray2rgb');
augimdsValidation = augmentedImageDatastore(inputSize,imdsValidation,'ColorPreprocessing', 'gray2rgb');
%% Step 15 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 and increases the
% computationl speed
% Implementing **WITH** The RCNN Object Detector
opts = trainingOptions('sgdm',...
'Momentum',0.9,...
'InitialLearnRate', 1e-4,...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'Shuffle','every-epoch', ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 1e-4, ...
'MaxEpochs', 100,...
'MiniBatchSize',128,...
'Verbose', true);
[height,width,numChannels, ~] = size(imdsTrain);
imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize);
%% Step 16 Training network Consisting Of Transferred & New Layers.
netTransfer = trainNetwork(augmentedTrainingSet,layers,opts);
%% Step 17 Classifying Validation Images Utilising Fine-tuned Network
[YPred,scores] = classify(netTransfer,augimdsValidation);
%% Step 18 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
%% Step 19 Calculating Validation Data Classification Accuracy (Accuracy Labels Predicted Accurately By Network)
YValidation = imdsValidation.Labels;
accuracy = mean(YPred == YValidation);
%% Step 20 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]);
%% Step 21 Testing the R-CNN detector on a test image.
testimg = imread('24.jpg');
[bboxes,score,label] = detect(rcnn,testimg,'MiniBatchSize',128)
%% Step 22 Display strongest detection result.
[score, idx] = max(score);
bbox = bboxes(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);
Imgdetected = insertObjectAnnotation(testimg, 'rectangle', bbox, annotation);
figure
imshow(Imgdetected);

Answers (1)

Prabhan Purwar
Prabhan Purwar on 24 Jan 2020
Could you attach the gunsGT folder, so that we can replicate the issue.
  3 Comments
Matpar
Matpar on 27 Jan 2020
I tried that it did not allow me to upload
I went to my storage on Matlab cloud and deleted everything also and still the challenge persist
Can you teach me how to do it? please

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by