matlab 仿照案例-目标检测
生活随笔
收集整理的這篇文章主要介紹了
matlab 仿照案例-目标检测
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Faster R-CNN深度學(xué)習(xí)進(jìn)行目標(biāo)檢測
版本2019a
% https://ww2.mathworks.cn/help/deeplearning/ug/object-detection-using-faster-r-cnn-deep-learning.html %此示例說明如何訓(xùn)練 Faster R-CNN(區(qū)域卷積神經(jīng)網(wǎng)絡(luò))目標(biāo)檢測器 %-------------下載預(yù)訓(xùn)練的檢測器 doTrainingAndEval = true; %要訓(xùn)練檢測器則改為true if ~doTrainingAndEval && ~exist('fasterRCNNResNet50EndToEndVehicleExample.mat','file')disp('Downloading pretrained detector (118 MB)...');pretrainedURL = 'https://www.mathworks.com/supportfiles/vision/data/fasterRCNNResNet50EndToEndVehicleExample.mat';websave('fasterRCNNResNet50EndToEndVehicleExample.mat',pretrainedURL); end%------------加載數(shù)據(jù)集---已標(biāo)記好目標(biāo) %unzip('D:\2ma\examples\deeplearning_shared\vehicleDatasetImages.zip') %---------unzip vehicleDatasetImages.zip data = load('vehicleDatasetGroundTruth.mat');%數(shù)據(jù)集 vehicleDataset = data.vehicleDataset;%% Add the fullpath to the local vehicle data folder.添加完整路徑 vehicleDataset.imageFilename = fullfile(pwd, vehicleDataset.imageFilename);%pwa表示當(dāng)前路徑%------車輛數(shù)據(jù)存儲在一個包含兩列的表中,其中第一列包含圖像文件路徑,第二列包含車輛邊界框。 % Display first few rows of the data set.顯示前幾行數(shù)據(jù)形式 vehicleDataset(1:4,:); %-------Read one of the images.讀取其中一張圖并顯示 I = imread(vehicleDataset.imageFilename{10}); figure subplot(211) imshow(I) title('原始圖片') %----------加入邊界框標(biāo)簽。 I2 = insertShape(I, 'Rectangle', vehicleDataset.vehicle{10});%在圖像中插入形狀 subplot(212) imshow(I2) title('原始圖片加入邊框數(shù)據(jù)') % % Resize and display image.重新定義圖片大小 % I3 = imresize(I,3);%大小調(diào)整因子,指定為正數(shù)。如果 scale 小于 1,則輸出圖像小于輸入圖像。如果 scale 大于 1,則輸出圖像大于輸入圖像 % figure % imshow(I3) % title('調(diào)整原始圖片大小')%將數(shù)據(jù)集拆分為用于訓(xùn)練檢測器的訓(xùn)練集%和用于評估檢測器的測試集。選擇 60% 的數(shù)據(jù)進(jìn)行訓(xùn)練。其余用于評估。% Set random seed to ensure example training reproducibility. rng(0);% 隨機(jī)進(jìn)行兩種數(shù)據(jù)集分類 shuffledIdx = randperm(height(vehicleDataset));%數(shù)據(jù)集的序號隨機(jī)排列 idx = floor(0.6 * height(vehicleDataset));%0.6的數(shù)據(jù)集有多少 trainingData = vehicleDataset(shuffledIdx(1:idx),:);%訓(xùn)練集 testData = vehicleDataset(shuffledIdx(idx+1:end),:);%測試集%-----------訓(xùn)練網(wǎng)絡(luò)-網(wǎng)絡(luò)已訓(xùn)練好的 %--trainFasterRCNNObjectDetector 分四步訓(xùn)練檢測器。 %前兩步訓(xùn)練 Faster R-CNN 中使用的區(qū)域提議和檢測網(wǎng)絡(luò)。 %最后兩個步驟結(jié)合了前兩個步驟中的網(wǎng)絡(luò),從而創(chuàng)建了一個用于檢測的網(wǎng)絡(luò)。 %使用 trainingOptions 為所有步驟指定網(wǎng)絡(luò)訓(xùn)練選項(xiàng)。% Options for step 1. options = trainingOptions('sgdm', ...'MaxEpochs', 5, ... % 用于訓(xùn)練的最大時期數(shù)'MiniBatchSize', 1, ...% 用于每次訓(xùn)練迭代的小批量大小'InitialLearnRate', 1e-3, ...'CheckpointPath', tempdir); %MiniBatchSize' 屬性設(shè)置為 1,因?yàn)檐囕v數(shù)據(jù)集具有不同大小的圖像。 %這可以防止它們被批處理在一起進(jìn)行處理。 %如果訓(xùn)練圖像大小相同,則選擇大于 1 的 MiniBatchSize 以減少訓(xùn)練時間。 %'CheckpointPath' 屬性設(shè)置為所有訓(xùn)練選項(xiàng)的臨時位置,保護(hù)防止停電等% 訓(xùn)練網(wǎng)絡(luò)-不訓(xùn)練,已經(jīng)訓(xùn)練好了 if doTrainingAndEval % Train Faster R-CNN detector.% * Use 'resnet50' as the feature extraction network. % * Adjust the NegativeOverlapRange and PositiveOverlapRange to ensure% training samples tightly overlap with ground truth.[detector, info] = trainFasterRCNNObjectDetector(trainingData, 'resnet50', options, ...'NegativeOverlapRange', [0 0.3], ...'PositiveOverlapRange', [0.6 1]); else% 使用預(yù)訓(xùn)練的 ResNet-50 進(jìn)行特征提取% Load pretrained detector for the example.pretrained = load('fasterRCNNResNet50VehicleExample.mat');%下載已訓(xùn)練號的網(wǎng)絡(luò)模型detector = pretrained.detector;%參數(shù) end% Note: This example verified on an Nvidia(TM) Titan X with 12 GB of GPU % memory. Training this network took approximately 10 minutes using this setup. % Training time varies depending on the hardware you use.% % 隨機(jī)測試集圖片原來的圖片 % I4 = imread(testData.imageFilename{7});%讀入圖片2 % figure % imshow(I4) % title('測試集的圖片') % I5 = insertShape(I4, 'Rectangle', testData.vehicle{7});%載入測試集圖片的邊界數(shù)據(jù) % figure % imshow(I5) % title('測試集的圖片及邊框') %----運(yùn)行探測器-測試集的圖片 I6 = imread(testData.imageFilename{10});%讀入圖片 figure subplot(221) imshow(I6) title('輸入探測器的原始圖片') [bboxes,scores] = detect(detector,I6);%得到測試圖片的邊框數(shù)據(jù) %bboxes為邊框數(shù)據(jù),scores為得分 % Annotate detections in the image. I7 = insertObjectAnnotation(I6,'rectangle',bboxes,scores);%探測器的結(jié)果 subplot(222) imshow(I7) title('模型結(jié)果')I8 = imread(testData.imageFilename{10});%讀入圖片2 I8 = insertShape(I8, 'Rectangle', testData.vehicle{10});%載入測試集圖片的邊界數(shù)據(jù) subplot(223) imshow(I8) title('人工標(biāo)簽結(jié)果')問題:
resnet50 requires the Deep Learning Toolbox Model for ResNet-50 Network support package. To install this
support package, use the Add-On Explorer.
輸入 應(yīng)與以下值之一匹配:
‘a(chǎn)lexnet’, ‘vgg16’, ‘vgg19’, ‘resnet18’, ‘resnet50’, ‘resnet101’, ‘googlenet’, ‘inceptionv3’,
‘inceptionresnetv2’, ‘squeezenet’, ‘mobilenetv2’
解決
還在學(xué)習(xí)深度學(xué)習(xí)中
總結(jié)
以上是生活随笔為你收集整理的matlab 仿照案例-目标检测的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 做词云 -jupyter跟
- 下一篇: matlab人脸追踪,求大神帮助我这个菜