主要内容

列车YLO v2车辆检测网络

将车辆检测的训练数据加载到工作区中。

数据=负载(“vehicleTrainingData.mat”);trainingData = data.vehicleTrainingData;

指定存储训练样本的目录。将完整路径添加到培训数据中的文件名。

dataDir = fullfile (toolboxdir (“愿景”),“视觉数据”); trainingData.imageFilename=fullfile(dataDir,trainingData.imageFilename);

随机洗牌数据进行训练。

rng (0);shuffledIdx = randperm(高度(trainingData));trainingData = trainingData (shuffledIdx:);

使用表中的文件创建imageDatastore。

imd = imageDatastore (trainingData.imageFilename);

使用表中的标签列创建boxLabelDatastore。

blds=boxLabelDatastore(培训数据(:,2:end));

结合数据存储。

ds=联合收割机(imds、blds);

加载预初始化的YOLO v2对象检测网络。

净=负荷(“yolov2VehicleDetector.mat”);lgraph = net.lgraph
lgraph = LayerGraph with properties: Layers: [25×1 net.cnn.layer. layer] Connections: [24×2 table] InputNames: {'input'} OutputNames: {'yolov2OutputLayer'}

检查YOLO v2网络中的图层及其属性。您还可以按照中给出的步骤创建YOLO v2网络创建YOLO v2对象检测网络.

ans=25x1层阵列和层:1“输入”图像输入128x128x3图像2“conv_1”卷积16 3x3卷积带跨步[1]和填充[1]3“BN1”批标准化批标准化4“relu_1”relu relu 5“maxpool1”最大池2x2最大池带跨步[2]和填充[0 0 0 0]6'conv_2'卷积32 3x3带跨步[1]和填充的卷积[1 1]7'BN2'批标准化批标准化8'relu_2'relu relu 9'maxpool2'最大池2x2带跨步的最大池[2]和填充[0 0 0 0]10'conv_3'卷积64 3x3带跨步[1]和填充的卷积[1 1 1 1 1]11“BN3”批标准化批标准化12“relu_3”relu relu 13“maxpool3”最大池2x2最大池带跨步[2]和填充[0 0 0 0]14“conv_4”卷积128 3x3卷积带跨步[1]和填充[1 1 1]15“BN4”批次标准化批次标准化16“relu_4”relu relu 17“yolov2Conv1”卷积128 3x3带跨距卷积[1]和填充“相同”18“yolov2Batch1”批次标准化批次标准化19“yolov2Relu1”relu 20“yolov2Conv2”卷积128 3x3带跨距卷积[1]和填充“相同”21“yolov2Batch2”批次标准化批次标准化22“yolov2Relu2”ReLU ReLU 23“yolov2ClassConv”卷积24 1x1卷积,带跨距[1]和填充[0]24“yolov2Transform”Yolov2转换层。Yolov2转换层带有4个锚。25“yolov2OutputLayer”Yolov2输出Yolov2输出带有4个锚。

配置网络培训选项。

选择= trainingOptions (“sgdm”,...“初始学习率”,0.001,...“冗长”符合事实的...“MiniBatchSize”,16,...“MaxEpochs”30岁的...“洗牌”,“从来没有”,...“VerboseFrequency”30岁的...“CheckpointPath”,tempdir);

培训YOLO v2网络。

[检测器,信息]=trainYOLOv2ObjectDetector(ds,lgraph,选项);
************************************************************************* 培训YOLO v2意思对象探测器以下对象类:*车辆培训单CPU。|========================================================================================| | 时代| |迭代时间| Mini-batch | Mini-batch |基地学习  | | | | ( hh: mm: ss) | RMSE | |率损失  | |========================================================================================| | 1 | 1 | 00:00:01 | 7.13 | 50.8 | 0.0010 | | 2 |30 | 00:00:14 | 1.35 | 1.8 | 0.0010 | | 4 | 60 | 00:00:27 | 1.13 | 1.3 | 0.0010 | | 5 | 90 | 00:00:39 | 0.64 | 0.4 | 0.0010 | | 7 | 120 | 00:00:51 | 0.65 | 0.4 | 0.0010 | | 9 | 150 | 00:01:04 | 0.72 | 0.5 | 0.0010 | | 10 | 180 | 00:01:16 | 0.52 | 0.3 | 0.0010 | | 12 | 210 | 00:01:28 | 0.45 | 0.2 | 0.0010 | | 14 | 240 | 00:01:41 | 0.61 | 0.4 | 0.0010 | | 15 | 270 | 00:01:52 | 0.43 | 0.2 | 0.0010 | | 17 | 300 | 00:02:05 | 0.42 | 0.2 | 0.0010 | | 19 | 330 | 00:02:17 | 0.52 | 0.3 | 0.0010 | | 20 | 360 | 00:02:29 | 0.43 | 0.2 | 0.0010 | | 22 | 390 | 00:02:42 | 0.43 | 0.2 | 0.0010 | | 24 | 420 | 00:02:54 | 0.59 | 0.4 | 0.0010 | | 25 | 450 | 00:03:06 | 0.61 | 0.4 | 0.0010 | | 27 | 480 | 00:03:18 | 0.65 | 0.4 | 0.0010 | | 29 | 510 | 00:03:31 | 0.48 | 0.2 | 0.0010 | | 30 | 540 | 00:03:42 | 0.34 | 0.1 | 0.0010 | |========================================================================================| Detector training complete. *************************************************************************

检查检测器的性能。

探测器
检测器=Yolov2Object检测器,具有以下属性:型号名称:“车辆”网络:[1×1 DAG网络]训练图像大小:[128]固定盒:[4×2双]类名:车辆

您可以通过检查每个迭代的训练损失来验证训练的准确性。

图:绘制(信息培训损失)网格包含(的迭代次数) ylabel (“每次迭代的培训损失”)

将测试映像读入工作区。

img = imread (“detectcars.png”);

在测试图像上运行经过培训的YOLO v2目标检测器,以进行车辆检测。

[bboxes,分数]=检测(探测器,img);

显示检测结果。

如果(~isempty(bboxes))img=插入对象注释(img,“矩形”,b框,分数);终止图imshow(img)