下载预训练的检测器
下载预训练的检测器,避免在训练上花费时间。如果要训练检测器,请将 doTraining 变量设置为 true。
doTraining = false;
if ~doTraining && ~exist('yolov2ResNet50VehicleExample_19b.mat','file')
disp('Downloading pretrained detector (98 MB)...');
pretrainedURL = 'https://www.mathworks.com/supportfiles/vision/data/yolov2ResNet50VehicleExample_19b.mat';
websave('yolov2ResNet50VehicleExample_19b.mat',pretrainedURL);
end
加载数据集
此示例使用包含 295 个图像的小型车辆数据集。每个图像包含一个或两个带标签的车辆实例。小型数据集适用于探查 YOLO v2 训练过程,但在实践中,需要更多带标签的图像来训练稳健的检测器。解压缩车辆图像并加载车辆真实值数据。
unzip vehicleDatasetImages.zip
data = load('vehicleDatasetGroundTruth.mat');
vehicleDataset = data.vehicleDataset;
车辆数据存储在一个包含两列的表中,其中第一列包含图像文件路径,第二列包含车辆边界框。
% Display first few rows of the data set.
vehicleDataset(1:4,:)
ans=4×2 table
imageFilename vehicle
_________________________________ ____________
{'vehicleImages/image_00001.jpg'} {1×4 double}
{'vehicleImages/image_00002.jpg'} {1×4 double}
{'vehicleImages/image_00003.jpg'} {1×4 double}
{'vehicleImages/image_00004.jpg'} {1×4 double}
% Add the fullpath to the local vehicle data folder.
vehicleDataset.imageFilename = fullfile(pwd,vehicleDataset.imageFilename);
将数据集分成训练集、验证集和测试集。选择 60% 的数据用于训练,10% 用于验证,其余用于测试经过训练的检测器。
rng(0);
shuffledIndices = randperm(height(vehicleDataset));
idx = floor(0.6 * length(shuffledIndices) );
trainingIdx = 1:idx;
trainingDataTbl = vehicleDataset(shuffledIndices(trainingIdx),:);
validationIdx = idx+1 : idx + 1 + floor(0.1 * length(shuffledIndices) );
validationDataTbl = vehicleDataset(shuffledIndices(validationIdx),:);
testIdx = validationIdx(end)+1 : length(shuffledIndices);
testDataTbl = vehicleDataset(shuffledIndices(testIdx),:);
使用 imageDatastore 和 boxLabelDatastore 创建数据存储,以便在训练和评估期间加载图像和标签数据。
imdsTrain = imageDatastore(trainingDataTbl{:,'imageFilename'});
bldsTrain = boxLabelDatastore(trainingDataTbl(:,'vehicle'));<