Matlab target detection - R-CNN implementation based on Alexnet transfer learning

        Object detection is to find out the objects of interest in the image and determine their type and location. The traditional object detection method takes feature extraction and matching as the core, which has the disadvantages of high time complexity and poor robustness of feature extraction.

        In this paper, on the basis of the Alexnet network model, the R-CNN target detection network is trained based on the principle of transfer learning. And identify the stop sign (stop sign) image data set that comes with Matlab, and the data set has been marked. Its implementation steps are as follows:

        Step 1: Import the Alexnet pre-trained model (need to be downloaded in advance, please refer to https://blog.csdn.net/shitao99/article/details/106536156 ). The implementation code is as follows:

%% 步骤1:载入AlexNet
% net=alexnet;  
net =load('alexnet.mat');
net =net.netTransfer;

        Step 2: Load the training set image, the implementation code is as follows:

data = load('stopSignsAndCars.mat', 'stopSignsAndCars');
stopSignsAndCars = data.stopSignsAndCars;
% 设置图像路径参数
visiondata = fullfile(toolboxdir('vision'),'visiondata');
stopSignsAndCars.imageFilename = fullfile(visiondata, stopSignsAndCars.imageFilename);
% 显示数据
summary(stopSignsAndCars)
% 只保留文件名及其所包含的“stop sign”区域
stopSigns = stopSignsAndCars(:, {'imageFilename','stopSign'});

        Step 3: Set the training parameters, and based on the transfer learning principle, train the R-CNN detector through 41 images including stop sign on the basis of the Alexnet convolutional neural network. The implementation code is as follows:

% 设置训练策略
    options = trainingOptions('sgdm', ...
        'MiniBatchSize', 128, ...
        'InitialLearnRate', 1e-3, ...
        'LearnRateSchedule', 'piecewise', ...
        'LearnRateDropFactor', 0.1, ...
        'LearnRateDropPeriod', 100, ...
        'MaxEpochs', 2, ...
        'Plots','training-progress', ...
        'Verbose', true);    
% 训练网络.    
rcnn = trainRCNNObjectDetector(stopSigns, net, options, ...
    'NegativeOverlapRange', [0 0.3], 'PositiveOverlapRange',[0.5 1])

         Step 4: Use the test image to test the detection effect of the trained target detector on the stop sign image, mark the target area on the original image and display the category and confidence. The implementation code is as follows:

% 载入测试图片
testImage = imread('stopSignTest.jpg');
% 检测“stop sign“标志
[bboxes,score,label] = detect(rcnn,testImage,'MiniBatchSize',128)

% 计算置信度并显示
[score, idx] = max(score);
bbox = bboxes(idx, :);
annotation = sprintf('%s: (Confidence = %f)', label(idx), score);
outputImage = insertObjectAnnotation(testImage, 'rectangle', bbox, annotation);
figure
imshow(outputImage)

         The above is all the code of matlab to realize R-CNN target detection, for your reference and study. If there are friends who don’t understand, welcome to leave a comment or send a private message, and you can also send a private message to the blogger for code customization (Q: 809315756).

Guess you like

Origin blog.csdn.net/qq_37904531/article/details/131490670