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 = 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

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)
  • 7
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值