本文内容:
一.在MATLAB2018b使用自己的数据训练faster-RCNN(MATLAB集成了faster-rcnn,所以并不需要配置caffe和Visual Studio)
二.报错和解决方法
环境说明:MATLAB2018b+win10 64x
一、训练faster-RCNN
1.准备自己的数据集
按照https://blog.csdn.net/Le0virg0/article/details/86712196博客进行数据集准备,当然也可以替换为自己的数据。(该博客将图像都resize了,目的是避免图像太大导致训练时显存不足;此外,标注框并不需要长宽都大于30个像素)
注意:标注完所有框后,save----->session;export当前数据。过程如图:
save session后,下一次打开Image Label时load 该session就可以进入之前的标注界面。
将标注数据保存在桌面后,save('stopsighdata.mat');(我上面那张图写错成“stopresighdata”,应该是“stopsighdata”)
2.训练网络
可以用alexnet或者resnet101进行训练 .注意:“Minibatchsize”必须为1
%载入标定结果
load('stopsigndata.mat');
%参数
options = trainingOptions('sgdm', ...
'MiniBatchSize', 1, ...
'InitialLearnRate', 1e-4, ...
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 100, ...
'MaxEpochs', 20, ...
'CheckpointPath', tempdir, ...
'Verbose', true, ...
'ExecutionEnvironment', 'gpu');
%训练
FD_Detector = trainFasterRCNNObjectDetector(stopsigndata,alexnet,options);
%FD_Detector = trainFasterRCNNObjectDetector(stopsigndata,'resnet101',options);
%测试
img = imread('2.jpg');
[bbox,score,label] = detect(FD_Detector,img);
index = find(score>0.55);
bbox = bbox(index,:);
score = score(index,:);
label = label(index,:);
img = insertObjectAnnotation(img,'Rectangle',bbox,score);
img = insertShape(img,'Rectangle',bbox);
%显示
figure;
imshow(img);
hold on
x=bbox(1)+60;
y=bbox(2)-14;
text(x,y,'stopsign','Color','k','FontSize',12);
%多物体检测显示
object = char(sort(unique(label)));
number = size(object);
text = 'Defects:';
text = strcat(text,{32},object(1,:));
if(number(1)>1)
for j = 2:number(1)
text = strcat(text,{32},{43},{32},object(j,:));
end
end
text = strrep(text,'_',' ');
imshow(img);
xlabel(text,'Color','r','FontSize',14)
二、报错及解决
1.报错
An unexpected error occurred trying to retrieve CUDA device properties. The CUDA error was:
CUDA_ERROR_UNKNOWN
解决方法:
我gpu驱动是10.0以上且训练其他网络没出错过,但把代码放实验室工作站跑就没报错
根据MATLAB社区提示,可能是驱动版本造成报错,我将驱动版本由430升级到436.48(2019.10.1发布),成功解决!!!
2.按照博客https://blog.csdn.net/qq_42263796/article/details/80453438的制作数据集和训练方式,频频报错
出错 faterrcnn (line 42)
detector = trainFasterRCNNObjectDetector(stopsigndata,layer,options);
原因:
Layer 'classoutput': The input size must be 1×1×2. The classification layer expects the third input
dimension to be the number of object classes the network should to detect (1 classes) plus 1. The
additional class is required for the "background" class. See the documentation for more details about
creating Fast or Faster R-CNN networks.
解决方法:是类别数出错了
根据博主代码,前面训练的Alexnet是个分类网络,因此在D:\MATLAB代码\faster-Rcnn\stopsign_227\文件夹下设立两个文件夹:stopsign(放置博主根据标注坐标裁剪的停止牌),background(背景,注意resize图像227*227)。
整体训练代码如下:
clc;clear;close all
% 使用MATLAB2017a自带的神经网络Alexnet,选择训练深度为20
net=alexnet;
image = imageDatastore(fullfile('D:\MATLAB代码\faster-Rcnn\stopsign_227\'),...
'IncludeSubfolders',true,'LabelSource','foldernames');
layersTransfer=net.Layers(1:end-3);
numClasses = numel(categories(image.Labels));
layers = [...
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm',...
'MiniBatchSize', 1, ... %Faster-RCNN中的minibatch只能设置成1
'InitialLearnRate', 1e-4, ... %学习率,设置大的话训练速度快但效果比较差,甚至会发散,设置小了训练速度会较慢
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 100, ...
'MaxEpochs', 20, ...
'CheckpointPath', tempdir, ...
'Verbose', true);
netTransfer = trainNetwork(image,layers,options);
%使用faster-RCNN训练
options = trainingOptions('sgdm',...
'MiniBatchSize', 1, ... %Faster-RCNN中的minibatch只能设置成1
'InitialLearnRate', 1e-4, ... %学习率,设置大的话训练速度快但效果比较差,甚至会发散,设置小了训练速度会较
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ...
'LearnRateDropPeriod', 100, ...
'MaxEpochs', 20, ...
'CheckpointPath', tempdir, ...
'Verbose', true);
layer = netTransfer.Layers;
%载入标定数据
load('stopsigndata.mat');
detector = trainFasterRCNNObjectDetector(stopsigndata,layer,options);
%检测部分
img=imread('1.jpg');
[bbox,score,label]=detect(detector,img);
img=insertObjectAnnotation(img,'Rectangle',bbox,score);%输出所有预测的box
detectedImg=insertShape(img,'Rectangle',bbox);
figure;
imshow(detectedImg);
hold on
x=bbox(1)+60;
y=bbox(2)-14;
text(x,y,'stopsign','Color','k','FontSize',12);