我有一个自定义数据集有4类和我使用MATLAB中的例子文档YOLov4对象检测。我不能够获得精确的情节。

17日视图(30天)
data =负载(“train.mat”);
defectDataset = data.gTruth;
rng (“默认”);
shuffledIndices = randperm(高度(defectDataset));
地板idx =(0.6 *长度(shuffledIndices));
trainingIdx = 1: idx;
trainingDataTbl = defectDataset (shuffledIndices (trainingIdx):);
validationIdx = idx + 1: idx + 1 +地板(0.1 *长度(shuffledIndices));
validationDataTbl = defectDataset (shuffledIndices (validationIdx):);
testIdx = validationIdx(结束)+ 1:长度(shuffledIndices);
testDataTbl = defectDataset (shuffledIndices (testIdx):);
defectDataset。defectDataset.imageFilename imageFilename = fullfile (pwd);
imdsTrain = imageDatastore (trainingDataTbl {:,“imageFilename”});
bldsTrain = boxLabelDatastore (trainingDataTbl (:,“洞”,“竹节”,“污点”,“结”)));
imdsValidation = imageDatastore (validationDataTbl {:,“imageFilename”});
bldsValidation = boxLabelDatastore (validationDataTbl (:,“洞”,“竹节”,“污点”,“结”)));
imdsTest = imageDatastore (testDataTbl {:,“imageFilename”});
bldsTest = boxLabelDatastore (testDataTbl (:,“洞”,“竹节”,“污点”,“结”)));
trainingData =结合(imdsTrain bldsTrain);
validationData =结合(imdsValidation bldsValidation);
testData =结合(imdsTest bldsTest);
data =阅读(trainingData);
我={1}数据;
bbox ={2}数据;
标签={3}数据;
annotatedImage = insertObjectAnnotation(我“矩形”bbox,标签);
annotatedImage = imresize (annotatedImage 2);
imshow (annotatedImage)
inputSize = (608 608 3);
className = [“洞”,“竹节”,“污点”,“结”];
rng (“默认”)
trainingDataForEstimation =变换(trainingData @(数据)preprocessData(数据、inputSize));
numAnchors = 9;
[锚,meanIoU] = estimateAnchorBoxes (trainingDataForEstimation numAnchors);
面积=锚(:1)。*锚(:,2);
[~,idx] =排序(区域,“下”);
锚=锚(idx:);
:anchorBoxes ={锚(1:3)
主持人(4:6 -:)
主持人(七章,:)
};
探测器= yolov4ObjectDetector (“csp-darknet53-coco”、名称、anchorBoxes InputSize = InputSize);
augmentedTrainingData =变换(trainingData @augmentData);
augmentedData =细胞(4,1);
k = 1:4
data =阅读(augmentedTrainingData);
augmentedData {k} = insertShape(数据{1},“矩形”、数据{2});
重置(augmentedTrainingData);
结束
蒙太奇(augmentedData BorderSize = 10)
选择= trainingOptions (“亚当”,
GradientDecayFactor = 0.9,
SquaredGradientDecayFactor = 0.999,
InitialLearnRate = 0.001,
LearnRateSchedule =“没有”,
MiniBatchSize = 8,
L2Regularization = 0.0005,
MaxEpochs = 20,
BatchNormalizationStatistics =“移动”,
DispatchInBackground = true,
ResetInputNormalization = false,
洗牌=“every-epoch”,
VerboseFrequency = 20,
ValidationFrequency = 1000,
CheckpointPath = tempdir,
ValidationData = ValidationData);
doTraining = false;
如果doTraining
%训练YOLO v4意思探测器。
[探测器,信息]= trainYOLOv4ObjectDetector (augmentedTrainingData、探测器、期权);
其他的
%负载pretrained检测器的例子。
探测器= downloadPretrainedYOLOv4Detector ();
结束
%检测对象在测试数据使用训练或pretrained探测器。
testData detectionResults =检测(探测器);
%评估检测性能。
[据美联社、召回、精密]= evaluateDetectionPrecision (detectionResults testData);
%绘制precision-recall曲线。
情节(记得,精度)
包含(“回忆”起)
ylabel (“精度”)
网格
标题(sprintf (“平均精度= % .2f”据美联社)),
%显示检测结果在测试图像
resultsDir = fullfile (pwd,“detection_results”);
mkdir (resultsDir);
i = 1:元素个数(testDataTbl.imageFilename)
我= imread (testDataTbl.imageFilename{我});
我= insertObjectAnnotation (,“矩形”detectionResults (i)。盒子,detectionResults (i) .Scores);
imwrite(我fullfile (resultsDir [num2str(我)“jpg”)));
结束
函数targetSize data = preprocessData(数据)
%改变图像的大小和规模在0和1之间的像素。同时规模
%相应的边界框。
2 = 1:尺寸(数据,1)
我= {ii, 1}数据;
imgSize =大小(I);
bboxes = {ii, 2}数据;
我= im2single (imresize(我targetSize (1:2)));
规模= targetSize (1:2)。/ imgSize (1:2);
bboxes = bboxresize (bboxes、规模);
数据(ii, 1:2) ={我bboxes};
结束
结束
函数探测器= downloadPretrainedYOLOv4Detector ()
%下载pretrained yolov4探测器。
如果~ (“yolov4CSPDarknet53VehicleExample_22a.mat”,“文件”)
如果~ (“yolov4CSPDarknet53VehicleExample_22a.zip”,“文件”)
disp (“下载pretrained探测器……”);
pretrainedURL =“https://ssd.mathworks.com/万博1manbetxsupportfiles/vision/data/yolov4CSPDarknet53VehicleExample_22a.zip”;
websave (“yolov4CSPDarknet53VehicleExample_22a.zip”,pretrainedURL);
结束
解压缩(“yolov4CSPDarknet53VehicleExample_22a.zip”);
结束
pretrained =负载(“yolov4CSPDarknet53VehicleExample_22a.mat”);
探测器= pretrained.detector;
结束
函数data = augmentData (A)
%应用随机水平翻转,和随机的X / Y缩放。盒子,
%比例范围以外剪如果重叠高于0.25。同时,
%抖动图像颜色。
data =细胞(大小(A));
2 = 1:尺寸(1)
我= {ii, 1};
bboxes = {ii, 2};
标签= {ii, 3};
深圳=大小(I);
如果元素个数(深圳)= = 3 & &深圳(3)= = 3
我= jitterColorHSV (,
对比= 0.0,
色调= 0.1,
饱和= 0.2,
亮度= 0.2);
结束
%随机翻转图像。
tform = randomAffine2d (XReflection = true,规模= 1.1 [1]);
溃败= affineOutputView(深圳、tform BoundsStyle =“centerOutput”);
I = imwarp(我tform OutputView =溃败);
%应用相同的转换盒。
[bboxes,指数]= bboxwarp (bboxes、tform溃败,OverlapThreshold = 0.25);
标签=标签(指标);
%返回原始数据只有当所有盒子都被扭曲。
如果isempty(指数)
数据(ii):) = (ii):);
继续;
结束
数据(ii):) ={我bboxes标签};
结束
结束
未被认出的字段名“gTruth”。

答案(1)

Shubham
Shubham 2023年4月3日
嗨Atishay,
获取精确的情节在MATLAB定制YOLOv4对象检测模型,您可以按照以下步骤:
  1. 首先,你需要训练你的YOLOv4模型自定义数据集。您可以使用MATLAB代码示例作为起点和修改它根据你的数据集。
  2. 训练后的模型,您可以使用evaluateDetectionPrecision函数获取精度指标。此函数接受地面实况数据和检测到的数据作为输入,并返回的精度指标。
  3. 获取精密策划,您可以使用plotDetectionPrecision函数。这个函数的输出evaluateDetectionPrecision函数作为输入和情节的精度指标。
这里有一个例子代码片段展示了如何获得精密的情节:
%加载地面实况数据和检测数据
负载(“groundTruthData.mat”);
负载(“detectedData.mat”);
%评估检测精度
指标= evaluateDetectionPrecision (detectedData groundTruthData);
%绘制精度
plotDetectionPrecision(指标);
注意,您需要更换 groundTruthData.mat detectedData.mat 地面实况的文件名和检测数据文件,分别。

社区寻宝

找到宝藏在MATLAB中央,发现社区如何帮助你!

开始狩猎!