matlab 仿照案例-目标检测
生活随笔
收集整理的這篇文章主要介紹了
matlab 仿照案例-目标检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Faster R-CNN深度學習進行目標檢測
版本2019a
% https://ww2.mathworks.cn/help/deeplearning/ug/object-detection-using-faster-r-cnn-deep-learning.html %此示例說明如何訓練 Faster R-CNN(區域卷積神經網絡)目標檢測器 %-------------下載預訓練的檢測器 doTrainingAndEval = true; %要訓練檢測器則改為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%------------加載數據集---已標記好目標 %unzip('D:\2ma\examples\deeplearning_shared\vehicleDatasetImages.zip') %---------unzip vehicleDatasetImages.zip data = load('vehicleDatasetGroundTruth.mat');%數據集 vehicleDataset = data.vehicleDataset;%% Add the fullpath to the local vehicle data folder.添加完整路徑 vehicleDataset.imageFilename = fullfile(pwd, vehicleDataset.imageFilename);%pwa表示當前路徑%------車輛數據存儲在一個包含兩列的表中,其中第一列包含圖像文件路徑,第二列包含車輛邊界框。 % Display first few rows of the data set.顯示前幾行數據形式 vehicleDataset(1:4,:); %-------Read one of the images.讀取其中一張圖并顯示 I = imread(vehicleDataset.imageFilename{10}); figure subplot(211) imshow(I) title('原始圖片') %----------加入邊界框標簽。 I2 = insertShape(I, 'Rectangle', vehicleDataset.vehicle{10});%在圖像中插入形狀 subplot(212) imshow(I2) title('原始圖片加入邊框數據') % % Resize and display image.重新定義圖片大小 % I3 = imresize(I,3);%大小調整因子,指定為正數。如果 scale 小于 1,則輸出圖像小于輸入圖像。如果 scale 大于 1,則輸出圖像大于輸入圖像 % figure % imshow(I3) % title('調整原始圖片大小')%將數據集拆分為用于訓練檢測器的訓練集%和用于評估檢測器的測試集。選擇 60% 的數據進行訓練。其余用于評估。% Set random seed to ensure example training reproducibility. rng(0);% 隨機進行兩種數據集分類 shuffledIdx = randperm(height(vehicleDataset));%數據集的序號隨機排列 idx = floor(0.6 * height(vehicleDataset));%0.6的數據集有多少 trainingData = vehicleDataset(shuffledIdx(1:idx),:);%訓練集 testData = vehicleDataset(shuffledIdx(idx+1:end),:);%測試集%-----------訓練網絡-網絡已訓練好的 %--trainFasterRCNNObjectDetector 分四步訓練檢測器。 %前兩步訓練 Faster R-CNN 中使用的區域提議和檢測網絡。 %最后兩個步驟結合了前兩個步驟中的網絡,從而創建了一個用于檢測的網絡。 %使用 trainingOptions 為所有步驟指定網絡訓練選項。% Options for step 1. options = trainingOptions('sgdm', ...'MaxEpochs', 5, ... % 用于訓練的最大時期數'MiniBatchSize', 1, ...% 用于每次訓練迭代的小批量大小'InitialLearnRate', 1e-3, ...'CheckpointPath', tempdir); %MiniBatchSize' 屬性設置為 1,因為車輛數據集具有不同大小的圖像。 %這可以防止它們被批處理在一起進行處理。 %如果訓練圖像大小相同,則選擇大于 1 的 MiniBatchSize 以減少訓練時間。 %'CheckpointPath' 屬性設置為所有訓練選項的臨時位置,保護防止停電等% 訓練網絡-不訓練,已經訓練好了 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% 使用預訓練的 ResNet-50 進行特征提取% Load pretrained detector for the example.pretrained = load('fasterRCNNResNet50VehicleExample.mat');%下載已訓練號的網絡模型detector = pretrained.detector;%參數 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.% % 隨機測試集圖片原來的圖片 % I4 = imread(testData.imageFilename{7});%讀入圖片2 % figure % imshow(I4) % title('測試集的圖片') % I5 = insertShape(I4, 'Rectangle', testData.vehicle{7});%載入測試集圖片的邊界數據 % figure % imshow(I5) % title('測試集的圖片及邊框') %----運行探測器-測試集的圖片 I6 = imread(testData.imageFilename{10});%讀入圖片 figure subplot(221) imshow(I6) title('輸入探測器的原始圖片') [bboxes,scores] = detect(detector,I6);%得到測試圖片的邊框數據 %bboxes為邊框數據,scores為得分 % Annotate detections in the image. I7 = insertObjectAnnotation(I6,'rectangle',bboxes,scores);%探測器的結果 subplot(222) imshow(I7) title('模型結果')I8 = imread(testData.imageFilename{10});%讀入圖片2 I8 = insertShape(I8, 'Rectangle', testData.vehicle{10});%載入測試集圖片的邊界數據 subplot(223) imshow(I8) title('人工標簽結果')問題:
resnet50 requires the Deep Learning Toolbox Model for ResNet-50 Network support package. To install this
support package, use the Add-On Explorer.
輸入 應與以下值之一匹配:
‘alexnet’, ‘vgg16’, ‘vgg19’, ‘resnet18’, ‘resnet50’, ‘resnet101’, ‘googlenet’, ‘inceptionv3’,
‘inceptionresnetv2’, ‘squeezenet’, ‘mobilenetv2’
解決
還在學習深度學習中
總結
以上是生活随笔為你收集整理的matlab 仿照案例-目标检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 做词云 -jupyter跟
- 下一篇: vue3-network 无效