Creating Fast R-CNN for Object Detection
7 次查看(过去 30 天)
显示 更早的评论
I am trying to create Fast-R-CNN for Object Detection, but I am taking error. I have 1 class on the dataset 90 sample.
imds = imageDatastore('C:\Users\', 'IncludeSubfolders',...
true, 'FileExtensions','.jpg','LabelSource','foldernames');
rng(0)
shuffledIndices = randperm(height(gTruthTBL));
idx = floor(0.6 * height(gTruthTBL));
trainingIdx = 1:idx;
trainingDataTbl = gTruthTBL(shuffledIndices(trainingIdx),:);
validationIdx = idx+1 : idx + 1 + floor(0.1 * length(shuffledIndices) );
validationDataTbl = gTruthTBL(shuffledIndices(validationIdx),:);
testIdx = validationIdx(end)+1 : length(shuffledIndices);
testDataTbl = gTruthTBL(shuffledIndices(testIdx),:);
imdsTrain = imageDatastore(trainingDataTbl{:,'imageFilename'});
bldsTrain = boxLabelDatastore(trainingDataTbl(:,'Unviable'));
imdsValidation = imageDatastore(validationDataTbl{:,'imageFilename'});
bldsValidation = boxLabelDatastore(validationDataTbl(:,'Unviable'));
imdsTest = imageDatastore(testDataTbl{:,'imageFilename'});
bldsTest = boxLabelDatastore(testDataTbl(:,'Unviable'));
trainingData = combine(imdsTrain,bldsTrain);
validationData = combine(imdsValidation,bldsValidation);
testData = combine(imdsTest,bldsTest);
data = read(trainingData);
inputSize = [224 224 3];
preprocessedTrainingData = transform(trainingData, @(data) preprocessData(data,inputSize));
net = resnet50;
lgraph = layerGraph(net);
% Remove the the last 3 layers from ResNet-50.
layersToRemove = {
'fc1000'
'fc1000_softmax'
'ClassificationLayer_fc1000'
};
lgraph = removeLayers(lgraph, layersToRemove);
% Specify the number of classes the network should classify.
numClasses = 1;
numClassesPlusBackground = numClasses + 1;
% Define new classification layers.
newLayers = [
fullyConnectedLayer(numClassesPlusBackground, 'Name', 'rcnnFC')
softmaxLayer('Name', 'rcnnSoftmax')
classificationLayer('Name', 'rcnnClassification')
];
% Add new layers.
lgraph = addLayers(lgraph, newLayers);
% Connect the new layers to the network.
lgraph = connectLayers(lgraph, 'avg_pool', 'rcnnFC');
% Define the number of outputs of the fully connected layer.
numOutputs = 4 * numClasses;
% Create the box regression layers.
boxRegressionLayers = [
fullyConnectedLayer(numOutputs,'Name','rcnnBoxFC')
rcnnBoxRegressionLayer('Name','rcnnBoxDeltas')
];
% Add the layers to the network
lgraph = addLayers(lgraph, boxRegressionLayers);
lgraph = connectLayers(lgraph,'avg_pool','rcnnBoxFC');
lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch2a');
lgraph = disconnectLayers(lgraph, featureExtractionLayer,'res5a_branch1');
% Add ROI max pooling layer.
outputSize = [14 14]
roiPool = roiMaxPooling2dLayer(outputSize,'Name','roiPool');
lgraph = addLayers(lgraph, roiPool);
% Connect feature extraction layer to ROI max pooling layer.
lgraph = connectLayers(lgraph, 'activation_40_relu','roiPool/in');
% Connect the output of ROI max pool to the disconnected layers from above.
lgraph = connectLayers(lgraph, 'roiPool','res5a_branch2a');
lgraph = connectLayers(lgraph, 'roiPool','res5a_branch1');
roiInput = roiInputLayer('Name','roiInput');
lgraph = addLayers(lgraph, roiInput);
% Connect ROI input layer to the 'roi' input of the ROI max pooling layer.
lgraph = connectLayers(lgraph, 'roiInput','roiPool/roi');
netOptions = trainingOptions('sgdm', ...
'MaxEpochs',10,...
'InitialLearnRate',1e-3,...
'MaxEpochs',50, ...
'CheckpointPath',tempdir,...
'ValidationData',validationData,...
'MiniBatchSize',64, ...
'Plots','training-progress')
detector = trainFasterRCNNObjectDetector(trainingData,lgraph,netOptions, ...
'NegativeOverlapRange',[0 0.3], ...
'PositiveOverlapRange',[0.6 1]);
Error using vision.internal.cnn.validation.checkGroundTruthDatastore (line 32)
The read method of the training input datastore must return an M-by-3 cell or table.
Error in trainFasterRCNNObjectDetector>iParseInputs (line 1037)
params.ClassNames = vision.internal.cnn.validation.checkGroundTruthDatastore(trainingDs);
Error in trainFasterRCNNObjectDetector (line 408)
[trainingData, options, params] = iParseInputs(...
Caused by:
Error using checkGroundTruthDatastore
Expected input number 1, Read output of training datastore input, to be an array with number of
columns equal to 3.
0 个评论
回答(1 个)
Shiva Kalyan Diwakaruni
2021-5-3
Hi,
For the issue 'The read method of the training input datastore must return an M-by-3 cell or table.'
Please refer to following documentation example to understand complete workflow of training object detection networks.
Hope it helps,
thanks.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!