error in faster rcnn matlab code with the minibatch size

As observed from the documentation available for Object Detection Using Faster R-CNN Deep Learning, it has been mentioned that the mini-batch size must be 1 for Faster R-CNN training, which processes multiple image regions from one training image every iteration. However, when we try to execute this, we encounter the error: The minibatchsize should be atleast '4'.
optionsStage1 =
trainingOptions('sgdm', ...
'MaxEpochs', 10, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
when we change the minibatchsize to '4' it works. Does that mean the Faster R-CNN processes multiple image regions from four training images every iteration? It this right?

7 commentaires

Can you share the screenshot of the exact error that you are facing? Also, what data are you using for training? Are there multiple objects in one training image?
I tried training Faster RCNN with similar training options, I got an error saying, "The 'MiniBatchSize' training option must be set to 1 for training Fast R-CNN or Faster R-CNN object detectors."
PFA the screenshot of the error. The error occurs when trying to use a minibatch size of 1 while training with SGDM optimizer.
Can you share your code with us?
Here is the code:
% Load training data for the object detector.
load('train_data.mat');
train_data.Properties.VariableNames{1} = 'imageFilename';
train_data.Properties.VariableNames{2} = 'abnormal';
train_data.Properties.VariableNames{3} = 'normal';
train_data(1:4,:)
% Read one of the training images.
I = imread(train_data.imageFilename{100});
% Insert the ROI labels.
I = insertShape(I, 'Rectangle', train_data.abnormal{100});
% Resize and display image.
I = imresize(I,2);
figure
imshow(I)
%load test data
load('test_data.mat');
test_data.Properties.VariableNames{1} = 'imageFilename';
test_data.Properties.VariableNames{2} = 'abnormal';
test_data.Properties.VariableNames{3} = 'normal';
test_data(1:4,:)
% Read one of the test images.
I = imread(test_data.imageFilename{100});
% Insert the ROI labels.
I = insertShape(I, 'Rectangle', test_data.abnormal{100});
% Resize and display image.
I = imresize(I,2);
figure
imshow(I)
objectClasses = {'abnormal','normal'};
numClassesPlusBackground = numel(objectClasses) + 1;
%The final fully connected layer of a network defines the number of classes
%that the network can classify. Set the final fully connected layer to have
%an output size equal to the number of classes plus a background class.
%load data to train the custom model
layers = [ ...
imageInputLayer([224 224 1],'Normalization', 'zerocenter') %grayscale image
convolution2dLayer(3,16,'Stride',1, 'Name','conv1')
reluLayer
convolution2dLayer(3,32,'Stride',1, 'Name','conv2')
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,64,'Stride',1, 'Name','conv3')
reluLayer
convolution2dLayer(3,128,'Stride',1, 'Name','conv4')
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(numClassesPlusBackground);
softmaxLayer
classificationLayer()];
Add fullpath to image files.
abnormal.imageFilename = fullfile('train_abnormal');
normal.imageFilename = fullfile('train_normal');
Set network training options: Set the CheckpointPath to save detector checkpoints to a temporary directory. Change this to another location if required.
% Options for step 1.
optionsStage1 = trainingOptions('sgdm', ...
'MaxEpochs', 60, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
% Options for step 2.
optionsStage2 = trainingOptions('sgdm', ...
'MaxEpochs', 30, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-3, ...
'CheckpointPath', tempdir);
% Options for step 3.
optionsStage3 = trainingOptions('sgdm', ...
'MaxEpochs', 30, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-5, ...
'CheckpointPath', tempdir);
% Options for step 4.
optionsStage4 = trainingOptions('sgdm', ...
'MaxEpochs', 30, ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-5, ...
'CheckpointPath', tempdir);
options = [
optionsStage1
optionsStage2
optionsStage3
optionsStage4
];
Train the Faster R-CNN detector.
doTrainingAndEval = true; %set to true or false depending on whether or not using the custom model
if doTrainingAndEval
% Set random seed to ensure example training reproducibility.
rng(0);
% Train Faster R-CNN detector. Select a BoxPyramidScale of 1.2 to allow
% for finer resolution for multiscale object detection.
detector = trainFasterRCNNObjectDetector(train_data, layers, options, ...
'NegativeOverlapRange', [0 0.3], ...
'PositiveOverlapRange', [0.7 1], ...
'BoxPyramidScale', 1.2);
else
% Load pretrained detector for the example.
detector = trainFasterRCNNObjectDetector(trainingData, vgg16, options, ...
'NegativeOverlapRange', [0 0.3], ...
'PositiveOverlapRange', [0.7 1], ...
'BoxPyramidScale', 1.2);
end
%To quickly verify the training, run the detector on a test image.
I = imread(test_data.imageFilename{1});
% Run the detector.
[bbox,score, label] = detect(detector,I);
%Display strongest detection result.
[score, idx] = max(score);
bbox = bbox(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);
% Annotate detections in the image.
detectedImg = insertObjectAnnotation(I,'rectangle',bbox,annotation);
figure
imshow(detectedImg)
I also had a question about the input dimensions to the custom model at (https://www.mathworks.com/matlabcentral/answers/420854-error-using-trainfasterr-cnnobjectd-etector) where I keep getting the error:
Error using vision.internal.cnn.fastrcnn.RegionReader (line 146) Unable to find any region proposals to use as positive or negative training samples.
I have also send messages to Matlab staff including Birju Patel but none of them responded. It would be great if you or someone responds to that question as well. Many thanks,
Can i have a response for this? Thanks.
I cannot say exactly the reason, but in my experience the error is coming because the number of samples are too small/the feature information they gathered are not adeqaute enough to satisfy the condition [0.7] and therefore they couldn't propose such regions. Try to reduce 'PositiveOverlapRange'and try again.
Hi, I’ve seen this error (the one requring a minimum minibach of 4 instead of only 1) before when training faster RCNNs across different versions of MATLAB (MATLAB 2018a and MATLAB 2018b). I‘m going to guess that the reason you‘re getting this error saying the minibatch size must be at least 4 is because you‘re running the function on MATLAB 2018a or older. MATLAB changed the mandatory minibatch size between MATLAB 2018a and 2018b and the example online assumes a 2018b MATLAB version (for reference, you can find the change explained here under the “functionality being removed or changed” section of 2018b. For what its worth, in my brief experience trying to train a faster rcnn on Matlab 2018a with a minibatch size of 4, I saw much worse performance on my validation set and a more unstable training than with MATLAB 2018b and a batch size of one.

Connectez-vous pour commenter.

Réponses (2)

numi khnax
numi khnax le 25 Oct 2018
I was trying yesterday and got the same error. I hope someone replies/address this issue.
It is actually an example shared by Matlab (https://www.mathworks.com/help/vision/examples/object-detection-using-faster-r-cnn-deep-learning.html)
ziwei li
ziwei li le 17 Jan 2019
I try the same example ,and got the same error of minibatchsize. But faster-rcnn require a batchsize of 1 .
Have you find the solution? Can you share with us?Thank you!

Community Treasure Hunt

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

Start Hunting!

Translated by