平台:matlab2016b
matlab自帶一個cifar10Net工具可用於深度學習。
圖片標注
這里使用的是matlab自帶的工具trainingImageLabeler對圖像進行roi的標注。
選擇AddImages將要訓練的圖片放進去(可以放入多張圖片),在ROI Label區域右鍵可以選擇改變label 的color和name,如果要訓練多個類,也可以點擊Add ROI Label來添加label。
所有圖像標注完成后點擊Export ROIs后會得到一個table(或stuct)變量,使用
save(‘file’,‘variable’);
命令來保存
因為cifar10Net使用的是table,如果你的數據集使用的是stuct,
這里使用
data=struct2table(file);
來將stuct轉化為table
imageFilename代表了圖片所存儲的位置;
tire代表了圖片中標注的輪胎,用矩陣存儲,分別為roi左上的坐標(x,y)和roi的大小(width,height);
RCNN訓練
我們來查看下網絡結構
load('rcnnStopSigns.mat','cifar10Net');
cifar10Net.Layers
會得到以下輸出
ans =
15x1 Layer array with layers:
1 'imageinput' Image Input 32x32x3 images with 'zerocenter' normalization
2 'conv' Convolution 32 5x5x3 convolutions with stride [1 1] and padding [2 2]
3 'relu' ReLU ReLU
4 'maxpool' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0]
5 'conv_1' Convolution 32 5x5x32 convolutions with stride [1 1] and padding [2 2]
6 'relu_1' ReLU ReLU
7 'maxpool_1' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0]
8 'conv_2' Convolution 64 5x5x32 convolutions with stride [1 1] and padding [2 2]
9 'relu_2' ReLU ReLU
10 'maxpool_2' Max Pooling 3x3 max pooling with stride [2 2] and padding [0 0]
11 'fc' Fully Connected 64 fully connected layer
12 'relu_3' ReLU ReLU
13 'fc_1' Fully Connected 10 fully connected layer
14 'softmax' Softmax softmax
15 'classoutput' Classification Output cross-entropy with 'airplane', 'automobile', and 8 other classes
通過觀察可以看出,一共只有三個卷積層
我們要對這個網絡進行微調,因為我這里只訓練了一個車輪,提供的數據中還包含有無標注的圖片,所以全連接層的輸出要改成2。后面再接上一個softmax層和一個classificationLayer,並且定義訓練方式:
x=cifar10Net.Layers(1:end-3);
lastlayers = [
fullyConnectedLayer(2,'Name','fc8','WeightLearnRateFactor',1, 'BiasLearnRateFactor',1)
softmaxLayer('Name','softmax')
classificationLayer('Name','classification')
];
options = trainingOptions('sgdm', ...
'MiniBatchSize', 32, ...
'InitialLearnRate', 1e-6, ...
'MaxEpochs', 100);
RCNN的訓練主要使用trainRCNNObjectDetector.m函數
detector = trainRCNNObjectDetector(groundTruth,network,options)
groundTruth - 具有2個或更多列的表。 第一列必須包含圖像文件名。 圖像可以是灰度或真彩色,可以是IMREAD支持的任何格式。 其余列必須包含指定每個圖像內對象位置的[x,y,width,height]邊框的M×4矩陣。 每列表示單個對象類,例如。 人,車,狗。 其實就是之前使用trainingImageLabeler做標注得到的數據。
network - 即為CNN的網絡結構
options - 即為網絡訓練的參數。包括初始化學習率、迭代次數、BatchSize等等。
除了以上三個參數外,還有
‘PositiveOverlapRange’ - 一個雙元素向量,指定0和1之間的邊界框重疊比例范圍。與指定范圍內(即之前做圖片標注畫出的框)的邊界框重疊的區域提案被用作正訓練樣本。Default: [0.5 1]
‘NegativeOverlapRange’ - 一個雙元素向量,指定0和1之間的邊界框重疊比例范圍。與指定范圍內(即之前做圖片標注畫出的框)的邊界框重疊的區域提案被用作負訓練樣本。Default: [0.1 0.5]
在訓練之前,RCNN會從訓練圖片中得到很多候選框,其中滿足正樣本要求的會被當做訓練正樣本,而滿足負樣本要求的會被當做訓練負樣本。
‘NumStrongestRegions’ - 用於生成訓練樣本的最強區域建議的最大數量(即最后得到的候選框數量)。 降低該值以加快處理時間,以訓練准確性為代價。 將此設置為inf以使用所有區域提案。Default: 2000
之后對訓練完成的結果進行檢測
clear;
tic;
load myRCNN.mat;
detectedImg = imread('cars_train_croped(227_227)\08031.jpg');
[bbox, score, label] = detect(myRCNN, detectedImg, 'MiniBatchSize', 20);
imshow(detectedImg);
idx=find(score>0.1);
bbox = bbox(idx, :);
n=size(idx,1);
for i=1:n
annotation = sprintf('%s: (Confidence = %f)', label(idx(i)), score(idx(i)));
de = insertObjectAnnotation(detectedImg, 'rectangle', bbox(i,:), annotation);
end
figure
imshow(de);
toc;
參考博客:https://blog.csdn.net/qq_33801763/article/details/77185457
https://blog.csdn.net/mr_curry/article/details/53160914
https://blog.csdn.net/u014096352/article/details/72854077