在深度學習的物體檢測方面,相比其他平臺,MATLAB打包好了很多模塊方法和網絡,對於簡單的應用,已經足夠應付!大家跟着調用,稍微修改一下適應的參數就可以啦!!下面我手把手教學!!!
1.需要讀取提前製作的csv文件(裏面有training data的bounding box的座標),csv 代碼會在另外一篇文章介紹
然後對csv裏的數據進行讀取,並轉化數據
A = cell2mat© 將元胞數組轉換爲普通數組。元胞數組的元素必須全都包括相同的數據類型,並且生成的數組也是該數據類型。
close all;clear;clc
gpuDevice(1);
GDSDataset = readtable('./train_cell2_matlab_0525.csv','Delimiter',',');
%將表格裏的座標數據轉化爲double類型,原來的csv文件在
for i=1:length(GDSDataset{:,1})
GDSDataset{i,2} = {str2double(reshape(strsplit(cell2mat(GDSDataset{i,2})),4,[])')};
end
2.這一步會抓取其中一張圖像文件根據csv的座標製作boundingbox並展示
%%%%%%展示其中一張圖片%%%%%%%
% Add the fullpath to the local vehicle data folder.
% % Read one of the images.
%讀取CSV文件裏的路徑列fn的第三張圖片
I = imread(GDSDataset.fn{3});
% Insert the ROI labels.
I = insertShape(I, 'Rectangle', GDSDataset.cell2{3});
% Resize and display image.
% I = imresize(I,2);
figure
imshow(I)
3.最重要的一步訓練步驟
% %%%%%%%%%%%%%Set Training and Validation Split%%%%%%%%%%%%%%%
% % Set random seed to ensure example training reproducibility.
% rng(0);
% % 隨機的將數據集分爲訓練集和驗證集
% shuffledIdx = randperm(height(windowDataset));
90%的測試集和10%的驗證集
idx = floor(0.9 * height(windowDataset));
trainingData = windowDataset(shuffledIdx(1:idx),:);
testData = windowDataset(shuffledIdx(idx+1:end),:);
epoch,batchsize,iteration大家很容易弄混淆,我來舉個例子
epoch是指遍歷一次所有樣本的行爲
batchsize是指針對一個小子集做一次梯度下降
比如總共1000個樣本,batchsize是50,則有20個iterations,20個iterations完成一個epoch.
%%%%%%%%%%%%%Set Training Options%%%%%%%%%%%%%%%%%%%%%%%
% Options for step 1.
imageSize = [1536 2048 3];
numClasses = width(GDSDataset)-1;
%需要生成boudingbox的anchorboxes
anchorBoxes = [93,172;100,180;344,628];
baseNetwork = resnet50;
% Specify the feature extraction layer.
featureLayer = 'activation_40_relu';
% Create the YOLO v2 object detection network.
lgraph = yolov2Layers(imageSize,numClasses,anchorBoxes,baseNetwork,featureLayer);
%%%%%%%%%%%%%Train YOLO v2 Object Detector%%%%%%%%%%
doTraining = true;
model_name = 'standard_cell2_detector_yolov2_0611_epoch50_changeanchor_testr';
if doTraining
% Configure the training options.
% * Lower the learning rate to 1e-3 to stabilize training.
% * Set CheckpointPath to save detector checkpoints to a temporary
% location. If training is interrupted due to a system failure or
% power outage, you can resume training from the saved checkpoint.
options = trainingOptions('sgdm', ...
'MiniBatchSize', 2, ....
'InitialLearnRate',1e-5, ...
'MaxEpochs',10,...
'CheckpointPath', tempdir, ...
'Shuffle','every-epoch');
tic;
% Train YOLO v2 detector.
[detector,info] = trainYOLOv2ObjectDetector(trainingData,lgraph,options);
trainingTime = toc;
save (model_name, 'detector','-v7.3');
else
% Load pretrained detector for the example.
pretrained = load('standard_cell2_detector_yolov2_0610_epoch50_2.mat');
detector = pretrained.detector;
end
4.將生成的模型做一個快速測試
%%%%%%%%%%%%%%%%%%%%As a quick test, run the detector on one test image.
% Read a test image.
I = imread('/home/testdata_crop_800_800/2.png');
% Run the detector.
[bboxes,scores] = detect(detector,I)
% Annotate detections in the image.
I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
imshow(I)