Problem in YOLOv2 training

8 次查看(过去 30 天)
Guilherme Franklin
Guilherme Franklin 2022-5-31
评论: Abhilasha 2024-5-3
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.

回答(1 个)

Vivek Akkala
Vivek Akkala 2022-6-6
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);
  1 个评论
Abhilasha
Abhilasha 2024-5-3
I am writing this code:
data = load('annotated_img.mat');
trainingData = gTruth1;
dataDir = fullfile("Filtered_Combined_Images");
trainingData.imageFilename = fullfile(trainingData.imageFilename);
rng(0); % Set random seed for reproducibility
shuffledIdx = randperm(height(trainingData));
trainingData = trainingData(shuffledIdx,:);
imds = imageDatastore(trainingData.imageFilename);
blds = boxLabelDatastore(trainingData(:,2:end))
dp = combine(imds, blds);
net = load('lgraph.mat');
lgraph = net.lgraph;
analyzeNetwork(lgraph);
lgraph.Layers
options = trainingOptions('sgdm', ...
'InitialLearnRate', 0.001, ...
'Verbose', true, ...
'MiniBatchSize', 16, ...
'MaxEpochs', 200,...
'Shuffle', 'never', ...
'VerboseFrequency', 20, ...
'CheckpointPath', tempdir);
[detector, info] = trainYOLOv2ObjectDetector(dp, lgraph, options);
and I am getting this error:
Network: The input to the YOLO v2 transform layer must have 168 channels to support 8 anchor boxes and 16 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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Recognition, Object Detection, and Semantic Segmentation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by