fixing an error about the number of channels in Yolov2

15 views (last 30 days)
I got this code working with one class from imageLabeler. But, I got errors when I tried 2 or 3 classes. It says " Network: The input to the YOLO v2 transform layer must have 28 channels to support 4 anchor boxes and 2 classes."
vehicleDetector = load('yolov2VehicleDetector.mat');
lgraph = vehicleDetector.lgraph;
[imds,bxds] = objectDetectorTrainingData(gTruth);
cds = combine(imds,bxds);
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.001, ...
'Verbose',true, ...
'MiniBatchSize',2, ...
'MaxEpochs',30, ...
'Shuffle','every-epoch', ...
'VerboseFrequency',10);
[detector,info] = trainYOLOv2ObjectDetector(cds,lgraph,options);
Error using trainYOLOv2ObjectDetector>iParseInputsYolov2
Invalid network.
Error in trainYOLOv2ObjectDetector (line 209)
[trainingData, lgraph, params, options] = iParseInputsYolov2(...
Caused by:
Network: The input to the YOLO v2 transform layer must have 28 channels to support
4 anchor boxes and 2 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.
Could you let me know how to fix the error? I appreciate your help.

Answers (1)

Vivek Akkala
Vivek Akkala on 9 Jun 2022
Hi,
The error is due to a mismatch between expected inputs and actual inputs to the yolov2TransformLayer. Based on the error message, I assume you want to train a YOLO v2 network with 4 anchor boxes for 2 classes.
To do so, the last convolutional layer before yolov2TransformLayer in the "lgraph" must have 28 output filters.
The issue can be resolved by updating the output filters of the last convolutional layer. You can try the following code to resolve the issue:
numClasses= 2;
numAnchorBoxes = 4;
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);

Community Treasure Hunt

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

Start Hunting!