www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I5_06_2_3_myTrainRCNNObjectDetector.m

    %% Train R-CNN (regions with convolutional neural networks) Stop Sign Detector
% 揮堏妛廗傪巊梡
% Image Processing Toolbox, Computer Vision System Toolbox
% Neural Network Toolbox, Statistics and Machine Learning Toolbox, Parallel Computing Toolbox偺儔僀僙儞僗偑昁梫丅乮暘椶偺幚峴偺傒偱傕摨條乯
clc;clear;close all;imtool close all;

%% 僨乕僞偺撉崬傒
% 'stopSigns'丗夋憸僼傽僀儖柤偲ROI丄'layers'丗妛廗嵪傒 Network Layers.
load('rcnnStopSigns.mat', 'stopSigns', 'layers')
layers         % 15憌丗妛廗嵪傒 Network Layers偺妋擣
% 夋憸傊偺僷僗傪愝掕
imDir = [matlabroot, '\toolbox\vision\visiondata\stopSignImages'];
addpath(imDir);

%% 撉崬傫偩ROI偺妋擣
imageLabeler     % stopSigns傪儚乕僋僗儁乕僗偐傜撉崬傒

%% 僩儗乕僯儞僌僆僾僔儑儞偺愝掕
options = trainingOptions('sgdm', ...
  'MiniBatchSize', 32, ...            % 僨僼僅儖僩偺128偐傜壓偘傞乮GPU儊儌儕巊梡検掅尭乯
  'InitialLearnRate', 1e-6, ...       % 僨僼僅儖僩丗0.01偐傜偝偘傞乮PreTraining儌僨儖偐傜FineTuning偡傞偨傔乯
  'MaxEpochs', 10);

%% R-CNN偺僩儗乕僯儞僌偺幚峴乮儔儀儕儞僌嵪傒夋憸僨乕僞偲丄妛廗嵪傒僱僢僩儚乕僋傪擖椡乯
%rcnn = trainRCNNObjectDetector(stopSigns, layers, options, 'NegativeOverlapRange', [0 0.3]);
load('I5_06_2_3_myRcnn.mat');      % 偁傜偐偠傔妛廗偟偨僱僢僩儚乕僋傪撉崬傒


%% 僥僗僩梡夋憸偺撉崬傒丒昞帵
I = imread('stopSignTest.jpg');
figure; imshow(I)

%% 専弌偟寢壥傪夋憸忋偵昞帵乮CPU偱偼幚峴帪娫昁梫乯
[bbox, score, label] = detect(rcnn, I, 'MiniBatchSize', 32);    % RCNN偱専弌

I1 = insertObjectAnnotation(I, 'rectangle', bbox, char(label), 'FontSize',18);
figure; imshow(I1)        % 昞帵

%% 夋憸傊偺僷僗傪徚嫀
rmpath(imDir); 

%%










%% R-CNN偺僩儗乕僯儞僌偺幚峴乮儔儀儕儞僌嵪傒夋憸僨乕僞偲丄妛廗嵪傒僱僢僩儚乕僋傪擖椡乯
% When the network is a SeriesNetwork, the network layers are automatically adjusted to support
% the number of object classes defined within the groundTruth training data.
% The background is added as an additional class.

% When the network is an array of Layer objects, the network must have a classification layer
% that supports the number of object classes, plus a background class. Use this input type to
% customize the learning rates of each layer. You can also use this input type to resume training
% from a previous session. Resuming the training is useful when the network requires additional
% rounds of fine-tuning, and when you want to train with additional training data.

% 幚峴偟偨嵺偺儊僢僙乕僕
% *******************************************************************
% Training an R-CNN Object Detector for the following object classes:
% 
% * stopSign
% 
% Step 1 of 3: Extracting region proposals from 27 training images...done.
% 
% Step 2 of 3: Training a neural network to classify objects in training data...
% 
% |=========================================================================================|
% |     Epoch    |   Iteration  | Time Elapsed |  Mini-batch  |  Mini-batch  | Base Learning|
% |              |              |  (seconds)   |     Loss     |   Accuracy   |     Rate     |
% |=========================================================================================|
% |            3 |           50 |         6.13 |       0.2895 |       96.88% |     0.000001 |
% |            5 |          100 |        10.23 |       0.2443 |       93.75% |     0.000001 |
% |            8 |          150 |        14.50 |       0.0013 |      100.00% |     0.000001 |
% |           10 |          200 |        18.64 |       0.1524 |       96.88% |     0.000001 |
% |=========================================================================================|
% 
% Network training complete.
% 
% Step 3 of 3: Training bounding box regression models for each object class...100.00%...done.
% 
% R-CNN training complete.
% *******************************************************************
% Copyright 2018 The MathWorks, Inc.