trainYOLOv​4ObjectDet​ector>iPar​seInputsYO​LOv4 The class names specified in the detector must match the class names in training data.

11 views (last 30 days)
Hi.When I try to train a YOLOv4,he said that Error using trainYOLOv4ObjectDetector>iParseInputsYOLOv4
The class names specified in the detector must match the class names in training data.
But I checked the content of the label and it's the same as the detector.And I didn't have this problem when training FasterrCNN.Here is my code:
load('training_data.mat')
>> trainingData = objectDetectorTrainingData(gTruth);
% Create a partition of the data into a 70/30 train/test split
cvp = cvpartition(size(trainingData, 1), 'HoldOut', 0.2);
% Get the indices of the training and testing sets
trainIdx = cvp.training;
testIdx = cvp.test;
% Extract the training and testing data using the indices
trainData = trainingData(trainIdx, :);
testData = trainingData(testIdx, :);
imdsTrain = imageDatastore(trainData.imageFilename);
imdsTest = imageDatastore(testData.imageFilename);
bldsTrain = boxLabelDatastore(trainData(:, 2:end));
bldsTest = boxLabelDatastore(testData(:, 2:end));
%imdsValidationData = imageDatastore(validationData.imageFilename);
%bldsValidationData = boxLabelDatastore(validationData(:, 2:end));
ds = combine(imdsTrain,bldsTrain);
inputSize = [2880 2880 3];
trainingDataForEstimation = transform(ds,@(data)preprocessData(data,inputSize));
numAnchors = 6;
[anchors, meanIoU] = estimateAnchorBoxes(trainingDataForEstimation,numAnchors);
area = anchors(:,1).*anchors(:,2);
[~,idx] = sort(area,"descend");
anchors = anchors(idx,:);
anchorBoxes = {anchors(1:3,:);anchors(4:6,:)};
classes =gTruth.LabelDefinitions.Name';
detector = yolov4ObjectDetector("tiny-yolov4-coco",classes,anchorBoxes,InputSize=inputSize);
options = trainingOptions("sgdm", ...
InitialLearnRate=0.001, ...
MiniBatchSize=16,...
MaxEpochs=40, ...
BatchNormalizationStatistics="moving",...
ResetInputNormalization=false,...
VerboseFrequency=30);
trainedDetector = trainYOLOv4ObjectDetector(ds,detector,options);
Error using trainYOLOv4ObjectDetector>iParseInputsYOLOv4
The class names specified in the detector must match the class names in training data.
Error in trainYOLOv4ObjectDetector (line 119)
[trainingData, params] = iParseInputsYOLOv4(trainingData,detector,options,mfilename,varargin{:});
detector.ClassNames
ans =
12×1 cell array
{'lozenge' }
{'hexagon' }
{'trapezium' }
{'heart' }
{'semicircle'}
{'pentagon' }
{'star' }
{'elliptical'}
{'rectangle' }
{'square' }
{'triangle' }
{'circle' }
gTruth.LabelDefinitions.Name
ans =
12×1 cell array
{'lozenge' }
{'hexagon' }
{'trapezium' }
{'heart' }
{'semicircle'}
{'pentagon' }
{'star' }
{'elliptical'}
{'rectangle' }
{'square' }
{'triangle' }
{'circle' }

Answers (3)

Himanshu
Himanshu on 24 May 2023
Hello,
I understand that you are facing an error while training YOLOv4. The error states that the class names specified in the detector must match with the class names in the training data.
As per the provided information, the class names seem to be the same in both detector and training data. To address this error, you can try the following steps:
  1. Check for any leading or trailing whitespaces in the class names of the "gTruth.LabelDefinitions.Name" variable.
  2. Verify the case sensitivity and examine for any additional characters or invisible characters.
  3. Validate the variable types of class names. Ensure that "gTruth.LabelDefinitions.Name" and the class names used in the training data are of type "cell array of strings".
You can refer to the below documentation to understand more about "yolov4ObjectDetector", "trainYOLOv4ObjectDetector" and "groundTruth" object in MATLAB.

hongliang
hongliang on 20 Apr 2024
Edited: hongliang on 13 Jul 2024
Hi,
To resolve the issue you are encountering while training YOLOv4, you need to swap the column names in bboxData:
classNames = {'circle','diamond','heart','hexagon','oval','pentagon','rectangle','semicircle','square','star','trapzoid','triangle'};
bboxData = bboxData(:, classNames);
The order should be exactly the same.
Additionally, I am surprised that we are working on the same task, identifying the same twelve shapes of blocks.
So, I am curious if you are a member of a project team at Southeast University?
I used building block photos (including grayscale/black and white) to train YOLOv4 with poor results. I then used computer-generated standard geometric shapes to train YOLOv4 for block identification.
If you are interested, we can communicate further. My email is zhaohongliang@msn.com.

Ron Manto
Ron Manto on 18 May 2024
Edited: Ron Manto on 19 May 2024
The problem probably was the same as mine.
I had done the same test as you have above, and after some digging in the trainYOLOv4ObjectDetector.m file (lines 624-625), and following the Workspace variables - they were simply not in order. Basically, the same as hongliang said.
The solution for me was:
% -- %
classNamesCells = flip(data.gTruth.LabelDefinitions.Name);
detector = yolov4ObjectDetector("tiny-yolov4-coco",classNamesCells,anchorBoxes,InputSize=inputSize);
% -- %
The rest of the code is pretty similar to what the MATLAB team did on their site:

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!