Main Content

Object Detection Using SSD Deep Learning

This example shows how to train a Single Shot Detector (SSD).

概述

深度学习是一种强大的机器学习技术,它会自动学习检测任务所需的图像功能。使用深度学习(例如更快的R-CNN)有几种用于对象检测的技术,您只能看一次(Yolo V2)和SSD。此示例使用trainssdobjectDetector功能。有关更多信息,请参阅Object Detection(Computer Vision Toolbox).

下载Pretrained Detector

下载预验证的检测器,以免等待培训完成。如果要训练检测器,请设置dotraining变量为true。

dotraining= false;如果~ doTraining & & ~存在('SSDRESNET50VEHICLEEXAMPLE_20A.MAT','file')disp('Downloading pretrained detector (44 MB)...');预处理='//www.tianjin-qmedu.com/supportfiles/vision/data/ssdResNet50VehicleExample_20a.mat'; websave('SSDRESNET50VEHICLEEXAMPLE_20A.MAT',pretrainedURL);结尾
下载预审预测器(44 MB)...

加载数据集

This example uses a small vehicle data set that contains 295 images. Many of these images come from the Caltech Cars 1999 and 2001 data sets, created by Pietro Perona and used with permission. Each image contains one or two labeled instances of a vehicle. A small data set is useful for exploring the SSD training procedure, but in practice, more labeled images are needed to train a robust detector.

解压缩车辆数据=负载('vehicleDatasetGroundTruth.mat');VERICLEDATASET = DATA.VEHICLEDATASET;

培训数据存储在表中。第一列包含图像文件的路径。其余的列包含车辆的ROI标签。显示数据的前几行。

vehicleDataset(1:4,:)
ans =4×2 tableimageFilename vehicle _________________________________ _________________ {'vehicleImages/image_00001.jpg'} {[220 136 35 28]} {'vehicleImages/image_00002.jpg'} {[175 126 61 45]} {'vehicleImages/image_00003.jpg'} {[108 120 45 33]} {'vehicleImages/image_00004.jpg'} {[124 112 38 36]}

将数据集拆分为用于训练检测器的训练集和用于评估检测器的测试集。选择60%的数据进行培训。使用其余的进行评估。

rng(0); shuffledIndices = randperm(height(vehicleDataset)); idx = floor(0.6 * length(shuffledIndices) ); trainingData = vehicleDataset(shuffledIndices(1:idx),:); testData = vehicleDataset(shuffledIndices(idx+1:end),:);

利用imageDatastoreandboxLabelDatastoreto load the image and label data during training and evaluation.

imdsTrain = imageDatastore(trainingData{:,'imageFilename'});bldstrain = boxlabeldatastore(triendingdata(:,,,'vehicle');imdstest = imagedatastore(testdata {:,,,'imageFilename'});bldsTest = boxLabelDatastore(testData(:,'vehicle');

Combine image and box label datastores.

trainingData = combine(imdsTrain,bldsTrain); testData = combine(imdsTest, bldsTest);

显示训练图像和盒子标签之一。

data = read(trainingData); I = data{1}; bbox = data{2}; annotatedImage = insertShape(I,'Rectangle',bbox);antotatedImage = imresize(AntotatedImage,2);图IMShow(注释图)

创建一个SSD对象检测网络

The SSD object detection network can be thought of as having two sub-networks. A feature extraction network, followed by a detection network.

The feature extraction network is typically a pretrained CNN (seePretrained Deep Neural Networks为了more details). This example uses ResNet-50 for feature extraction. Other pretrained networks such as MobileNet v2 or ResNet-18 can also be used depending on application requirements. The detection sub-network is a small CNN compared to the feature extraction network and is composed of a few convolutional layers and layers specific to SSD.

利用thessdLayers函数可以自动将预验证的RESNET-50网络修改为SSD对象检测网络。ssdLayers需要你指定多个输入that parameterize the SSD network, including the network input size and the number of classes. When choosing the network input size, consider the size of the training images, and the computational cost incurred by processing data at the selected size. When feasible, choose a network input size that is close to the size of the training image. However, to reduce the computational cost of running this example, the network input size is chosen to be [300 300 3]. During training,trainssdobjectDetectorautomatically resizes the training images to the network input size.

inputsize = [300 300 3];

Define number of object classes to detect.

numClasses = width(VehicleDataset)-1;

创建SSD对象检测网络。

lgraph = ssdLayers(inputSize, numClasses,'resnet50');

You can visualize the network using分析或dEEPNETWORKDESIGNER从深度学习工具箱™。请注意,您还可以逐层创建自定义SSD网络。有关更多信息,请参阅创建SSD对象检测网络(Computer Vision Toolbox).

Data Augmentation

Data augmentation is used to improve network accuracy by randomly transforming the original data during training. By using data augmentation, you can add more variety to the training data without actually having to increase the number of labeled training samples. Usetransform通过增加培训数据

  • Randomly flipping the image and associated box labels horizontally.

  • Randomly scale the image, associated box labels.

  • 抖动图像颜色。

Note that data augmentation is not applied to the test data. Ideally, test data should be representative of the original data and is left unmodified for unbiased evaluation.

augmentedTrainingData = transform(trainingData,@augmentData);

通过多次读取相同的图像来可视化增强训练数据。

augmenteddata =细胞(4,1);为了k = 1:4 data = read(augmentedTrainingData);augmenteddata {k} = insertshape(data {1},'Rectangle',数据{2});重置(AugmentedTrainingData);结尾图蒙太奇(Augmenteddata,'BorderSize',10)

Preprocess Training Data

Preprocess the augmented training data to prepare for training.

preprocessedTrainingData = transform(augmentedTrainingData,@(data)preprocessdata(data,inputsize));

Read the preprocessed training data.

data = read(preprocessedtrainingdata);

Display the image and bounding boxes.

i = data {1};bbox = data {2};antotatedImage = insertshape(i,'Rectangle',bbox);antotatedImage = imresize(AntotatedImage,2);图IMShow(注释图)

Train SSD Object Detector

利用trainingOptionsto specify network training options. Set'CheckpointPath'to a temporary location. This enables the saving of partially trained detectors during the training process. If training is interrupted, such as by a power outage or system failure, you can resume training from the saved checkpoint.

options = trainingOptions('sgdm',...“ MINIBATCHSIZE”,16,....“初始删除”,1e-1,...“学习者”,'piecewise',...'LearnRateDropPeriod',30,...'LearnRatedRopFactor', 0.8,...“MaxEpochs”, 300,...'VerboseFrequency', 50,...'CheckpointPath',tempdir,...“洗牌”,'every-epoch');

利用trainssdobjectDetector(Computer Vision Toolbox)训练SSD对象检测器的功能dotrainingto true. Otherwise, load a pretrained network.

如果dotraining% Train the SSD detector.[detector, info] = trainSSDObjectDetector(preprocessedTrainingData,lgraph,options);别的示例的负载预告量检测器。pretrained = load('SSDRESNET50VEHICLEEXAMPLE_20A.MAT');探测or = pretrained.detector;结尾

This example is verified on an NVIDIA™ Titan X GPU with 12 GB of memory. If your GPU has less memory, you may run out of memory. If this happens, lower the 'MinibatchSize' 使用trainingOptions功能。训练this network took approximately 2 hours using this setup. Training time varies depending on the hardware you use.

作为快速测试,请在一个测试图像上运行检测器。

data = read(testData); I = data{1,1}; I = imresize(I,inputSize(1:2)); [bboxes,scores] = detect(detector,I,'Threshold', 0.4);

Display the results.

I = insertObjectAnnotation(I,'长方形',bbox,分数);图Imshow(i)

Evaluate Detector Using Test Set

Evaluate the trained object detector on a large set of images to measure the performance. Computer Vision Toolbox™ provides object detector evaluation functions to measure common metrics such as average precision (评估估计) and log-average miss rates (evaluateDetectionMissRate)。在此示例中,使用平均精度度量来评估性能。平均精度提供了一个单个数字,该数字结合了检测器进行正确分类的能力(precision) and the ability of the detector to find all relevant objects (记起)。

Apply the same preprocessing transform to the test data as for the training data. Note that data augmentation is not applied to the test data. Test data should be representative of the original data and be left unmodified for unbiased evaluation.

preprocessedTestData = transform(testData,@(data)preprocessData(data,inputSize));

Run the detector on all the test images.

dentectionResults =检测(检测器,预处理testdata,'Threshold', 0.4);

使用平均精度度量评估对象检测器。

[ap,recall,precision] = evaluateDetectionPrecision(detectionResults, preprocessedTestData);

The precision/recall (PR) curve highlights how precise a detector is at varying levels of recall. Ideally, the precision would be 1 at all recall levels. The use of more data can help improve the average precision, but might require more training time Plot the PR curve.

figure plot(recall,precision) xlabel('Recall') ylabel('Precision') 网格on标题(sprintf(sprintf)('Average Precision = %.2f',AP))

Code Generation

Once the detector is trained and evaluated, you can generate code for thessdobjectDetectorusing GPU Coder™. For more details, see使用单镜头多伯克斯检测器,用于对象检测的代码生成(Computer Vision Toolbox)例子。

Supporting Functions

functionb = augmentdata(a)%应用随机水平翻转和随机X/Y缩放。得到的盒子% scaled outside the bounds are clipped if the overlap is above 0.25. Also,%抖动图像颜色。B = cell(size(A)); I = A{1}; sz = size(I);如果numel(sz)== 3 && sz(3)== 3 i = jittercolorhsv(i,...'Contrast',0.2,...'色调',0,...'Saturation',0.1,...'Brightness',0.2);结尾%随机翻转和比例图像。tform = randomAffine2d('XReflection',true,'规模',[1 1.1]); rout = affineOutputView(sz,tform,“界限”,'CenterOutput');b {1} = imwarp(i,tform,'outputview',rout);% Sanitize boxes, if needed.A{2} = helperSanitizeBoxes(A{2}, sz);%对框应用相同的转换。[B{2},indices] = bboxwarp(A{2},tform,rout,'OverlapThreshold',0.25);b {3} = a {3}(indices);% Return original data only when all boxes are removed by warping.如果isempty(indices) B = A;结尾结尾functiondata = preprocessData(data,targetSize)% Resize image and bounding boxes to the targetSize.sz = size(data{1},[1 2]); scale = targetSize(1:2)./sz; data{1} = imresize(data{1},targetSize(1:2));% Sanitize boxes, if needed.data {2} = helperSanitizeboxes(data {2},sz);% Resize boxes.data{2} = bboxresize(data{2},scale);结尾

References

[1] Liu,Wei,Dragomir Anguelov,Dumitru Erhan,Christian Szegedy,Scott Reed,Cheng Yang Fu和Alexander C. Berg。“ SSD:单枪Multibox检测器。”在第14届欧洲计算机视觉会议上,ECCV2016。SpringerVerlag,2016年。

也可以看看

Apps

Functions

对象

Related Topics