Matlab|Matlab yolov2 深度学习物体检测 超级简单代码
在深度学习的物体检测方面,相比其他平台,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.
% % Set random seed to ensure example training reproducibility.
rng(0);
% % Randomly split data into a training and test set.
% shuffledIdx = randperm(height(GDSDataset));
% idx = floor(0.9 * height(GDSDataset));
% trainingData = https://www.it610.com/article/GDSDataset(shuffledIdx(1:idx),:);
% testData = GDSDataset(shuffledIdx(idx+1:end),:);
trainingData = GDSDataset
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
【Matlab|Matlab yolov2 深度学习物体检测 超级简单代码】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)
推荐阅读
- 《深度倾听》第5天──「RIA学习力」便签输出第16期
- 深度解读(《秘密》(八)—健康的秘密)
- 影响深度思考的9种思维定式
- hough变换检测的matlab程序
- 为Google|为Google Cloud配置深度学习环境(CUDA、cuDNN、Tensorflow2、VScode远程ssh等)
- 深度学习-入门
- 养好一塘鱼——《深度思维》(三)
- OpenCV|OpenCV-Python实战(18)——深度学习简介与入门示例
- 讲给资深产品人跳槽用的21道深度好问题
- C语言学习(bit)|16.C语言进阶——深度剖析数据在内存中的存储