MATLAB Answers

Errors With Training the RCNN dectector trainRCNNobjectDetector

9 views (last 30 days)
Matpar
Matpar on 19 Jan 2020
Answered: Raunak Gupta on 24 Jan 2020
Hi Professionals,
I need a push here "PLEASE"!!!
I am trying to really understand what these errors mean in my code that leaves me bewildered!!
I am traning a network and the RCNN decoder bit argues about this error that is challenging me!
Can a professional Please Assist me? I really don;t get it!!
MY CODE:
clear
clc
deepNetworkDesigner
gunfolder = '/Users/mmgp/Desktop/gunsGT'
save('gunlables.mat','gunfolder');
gTruth=('gTruth.mat');
%% 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')
%% 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);
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',25,'BiasLearnRateFactor',25)
softmaxLayer
classificationLayer];
%% Load .MAT file, the ground truths and the Network layers
load('gimlab.mat', 'gTruth', 'net');
%% 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
opts = trainingOptions('sgdm', ...
'Momentum', 0.9, ...
'InitialLearnRate', 0.004, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 8, ...
'L2Regularization', 0.004, ...
'MaxEpochs', 10, ...
'MiniBatchSize', 128, ...
'Verbose', true, 'Plots','training-progress');
doTraining = true;
[height,width,numChannels, ~] = size(imdsTrain);
imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize);
%% Train the R-CNN detector. Training can take a few minutes to complete.
rcnn = trainRCNNObjectDetector(gTruth, net, opts, 'NegativeOverlapRange', [0 0.3]);
%% 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)
MY ERRORS
Error using trainRCNNObjectDetector
Expected input number 1, trainingData, to be one of these types:
table
Error in vision.internal.cnn.validation.checkGroundTruth (line 2)
validateattributes(gt, {'table'},{'nonempty'}, name, 'trainingData',1);
Error in trainRCNNObjectDetector>parseInputs (line 311)
vision.internal.cnn.validation.checkGroundTruth(trainingData, fname);
Error in trainRCNNObjectDetector (line 248)
[network, params] = parseInputs(trainingData, network, options, mfilename, varargin{:});
Error in guntest2 (line 117)
rcnn = trainRCNNObjectDetector(gTruth, net, opts, 'NegativeOverlapRange', [0 0.3]);
>>

Answers (1)

Raunak Gupta
Raunak Gupta on 24 Jan 2020
Hi,
I think this is a similar question that is posted here. You may find the answer mentioned to that question useful.

Community Treasure Hunt

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

Start Hunting!

Translated by