Problem in YOLOv2 training

7 views (last 30 days)
Guilherme Franklin
Guilherme Franklin on 31 May 2022
Answered: Vivek Akkala on 6 Jun 2022
I'm doing a YOLOv2 training and I came across the following error:
Error using
trainYOLOv2ObjectDetector>iParseInputsYolov2
(line 240)
Invalid network.
Error in
trainYOLOv2ObjectDetector
(line 174)
[trainingData, lgraph,
params, options] =
iParseInputsYolov2(...
Error in criando (line 13)
[detector,info] =
trainYOLOv2ObjectDetector(ds,lgraph,options);
Caused by:
Network: The input to
the YOLO v2 transform
layer must have 12
channels to support 2
anchor boxes and 1
classes. The number of
channels must equal
numAnchors * (5 +
numClasses). Update the
training data, the
number of anchor boxes
specified in the
yolov2Transform layer,
or the layers preceding
the transform layer.
My code:
clear, clc, close all;
vagem = load('RotulosVagem.mat');
lgraph = load('lgraph.mat');
lgraph = lgraph.lgraph;
gTruth = vagem.gTruth;
[imds, blds] = objectDetectorTrainingData(gTruth);
ds = combine(imds, blds);
options = trainingOptions('sgdm');
[detector,info] = trainYOLOv2ObjectDetector(ds,lgraph,options);
I will attach the files. Thanks in advance.

Answers (1)

Vivek Akkala
Vivek Akkala on 6 Jun 2022
Hi,
There seems to be a mismatch between expected inputs and actual inputs to the yolov2TransformLayer. Based on the "RotulosVagem.mat" and "lgraph" provided by you, I assume you want to train a YOLO v2 network with 2 anchor boxes for 1 class.
For this, the last convolutional layer before yolov2TransformLayer in the "lgraph" must have 12 output filters but the current network is having 20 filters.
The issue can be resolved by updating the output filters of the last convolutional layer. You can try the following code:
lgraph = lgraph.lgraph;
[imds, blds] = objectDetectorTrainingData(gTruth);
ds = combine(imds, blds);
options = trainingOptions('sgdm');
% % Start of the code to be added %%
numClasses= size(vagem.gTruth.LabelData,2);
numAnchorBoxes = size(lgraph.Layers(end,1).AnchorBoxes,1);
outFilters = (5+numClasses).*numAnchorBoxes;
yolov2ConvLayer = convolution2dLayer(3,outFilters,'Name','yolov2ConvUpdated',...
'Padding', 'same',...
'WeightsInitializer',@(sz)randn(sz)*0.01);
yolov2ConvLayer.Bias = zeros(1,1,outFilters);
lgraph = replaceLayer(lgraph,'yolov2ClassConv',yolov2ConvLayer);
% % End of the code to be added %%
[detector,info] = trainYOLOv2ObjectDetector(ds,lgraph,options);

Community Treasure Hunt

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

Start Hunting!