How to use yolov3 in matlab

 I try to use yolo in matlab ,and follow the example, although the detetor of yolo was built, the accuracy is low. I used the image from website ,few picture with car could be recognized.

 

Object Detection Using YOLO v2 Deep Learning
April 8,2019
Jun zhang
matlab:2019a
download data

doTraining = false;
if ~doTraining && ~exist('yolov2ResNet50VehicleExample.mat','file')
    % Download pretrained detector.
    disp('Downloading pretrained detector (98 MB)...');
    pretrainedURL = 'https://www.mathworks.com/supportfiles/vision/data/yolov2ResNet50VehicleExample.mat';
    websave('yolov2ResNet50VehicleExample.mat',pretrainedURL);
end


load dataset
when you unzip the file, the dataset will be added to the same directory as this .m or .mlx file
% Unzip vehicle dataset images.
unzip vehicleDatasetImages.zip

% Load vehicle dataset ground truth.
data = load('vehicleDatasetGroundTruth.mat');
vehicleDataset = data.vehicleDataset;

%% The training data is stored in a table. The first column contains the path to the image files. The remaining columns contain the ROI labels for vehicles.

% Display first few rows of the data set.
vehicleDataset(1:4,:)


%% Display one of the images from the data set to understand the type of images it contains.
% Add the fullpath to the local vehicle data folder.
vehicleDataset.imageFilename = fullfile(pwd,vehicleDataset.imageFilename);

% % 

%% read the  specific file

% Read one of the images.
num=11;
I = imread(vehicleDataset.imageFilename{num});

% Insert the ROI labels.
I = insertShape(I,'Rectangle',vehicleDataset.vehicle{num});

% Resize and display image.
I = imresize(I,3);
imshow(I)


%% THIS method was used to construct the train dataset and test dataset in randomly order
%% Split the data set into a training set for training the detector, and a test set for evaluating the detector. Select 60% of the data for training. Use the rest for evaluation.% Set random seed to ensure example training reproducibility.

rng(0);

% Randomly split data into a training and test set.
shuffledIndices = randperm(height(vehicleDataset))
% p = randperm(n) returns a row vector containing a random permutation of the integers from 1 to n inclusive.


idx = floor(0.6 * length(shuffledIndices) )
VtrainingData = vehicleDataset(shuffledIndices(1:idx),:);
VtestData = vehicleDataset(shuffledIndices(idx+1:end),:);

% Create a YOLO v2 Object Detection Network
% The YOLO v2 object detection network can be thought of as having two sub-networks. A feature extraction network, followed by a detection network.

% The feature extraction network is typically a pretrained CNN (see Pretrained Deep Neural Networks for more details). This example uses ResNet-50 for feature extraction. Other pretrained networks such as MobileNet v2 or ResNet-18 can also be used depending on application requirements. The detection sub-network is a small CNN compared to the feature extraction network and is composed of a few convolutional layers and layers specific for YOLO v2.

% Use the yolov2Layers function to automatically modify a pretrained ResNet-50 network into a YOLO v2 object detection network. yolov2Layers requires you to specify several inputs that parameterize a YOLO v2 network.

% First, specify the image input size and the number of classes. The image input size should be at least as big as the images in the training image set. In this example, the images are 224-by-224 RGB images.

% Define the image input size.
imageSize = [224 224 3];

% Define the number of object classes to detect.
numClasses = width(vehicleDataset)-1; % first column is file path.

anchorBoxes = [
    43 59
    18 22
    23 29
    84 109
];

Finally, specify the network and feature extraction layer within that network to use as the basis of YOLO v2.
% Load a pretrained ResNet-50.
baseNetwork = resnet50;
% Analyze deep learning network architecture

analyzeNetwork(baseNetwork)

%% Select 'activation_40_relu' as the feature extraction layer. The layers after 'activation_40_relu' are discarded and the detection sub-network is attached to 'activation_40_relu'. This feature extraction layer outputs feature maps that are downsampled by a factor of 16. This amount of downsampling is a good trade-off between spatial resolution and the strength of the extracted features (features extracted further down the network encode stronger image features at the cost of spatial resolution). Choosing the optimal feature extraction layer requires empirical analysis and is another hyperparameter to tune.

% Specify the feature extraction layer.
featureLayer = 'activation_40_relu' %% this is the 141th layer in the basenetwork

% Create the YOLO v2 object detection network. 
% Create the YOLO v2 object detection network. 
lgraph = yolov2Layers(imageSize,numClasses,anchorBoxes,baseNetwork,featureLayer);

analyzeNetwork(lgraph)


%% Train YOLO v2 Object Detector
% To use the trainYOLOv2ObjectDetector function, set doTraining to true. Otherwise, load a pretrained detector.
doTraining=true;
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', 16, ....
        'InitialLearnRate',1e-3, ...
        'MaxEpochs',10,...
        'CheckpointPath', tempdir, ...
        'Shuffle','every-epoch');    
    
    % Train YOLO v2 detector.
    [detector,info] = trainYOLOv2ObjectDetector(VtrainingData,lgraph,options);
else
    % Load pretrained detector for the example.
    pretrained = load('yolov2ResNet50VehicleExample.mat');
    detector = pretrained.detector;
end

Training on single CPU.
|========================================================================================|
|  Epoch  |  Iteration  |  Time Elapsed  |  Mini-batch  |  Mini-batch  |  Base Learning  |
|         |             |   (hh:mm:ss)   |     RMSE     |     Loss     |      Rate       |
|========================================================================================|
|       1 |           1 |       00:00:06 |         6.69 |         44.7 |          0.0010 |
|       5 |          50 |       00:05:39 |         2.40 |          5.7 |          0.0010 |
|      10 |         100 |       00:11:17 |         2.33 |          5.4 |          0.0010 |
|      10 |         110 |       00:12:21 |         2.12 |          4.5 |          0.0010 |
|========================================================================================|

 

 

test dataset
% Read a test image.
num=1
VtestData.imageFilename{num}
I = imread(VtestData.imageFilename{num});

% Run the detector.
[bboxes,scores] = detect(detector,I)

% Annotate detections in the image.
I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
imshow(I)


%% Evaluate Detector Using Test Set

% Create a table to hold the bounding boxes, scores, and labels output by
% the detector. 
numImages = height(VtestData);
results = table('Size',[numImages 3],...
    'VariableTypes',{'cell','cell','cell'},...
    'VariableNames',{'Boxes','Scores','Labels'});

% Run detector on each image in the test set and collect results.
for i = 1:numImages
    
    % Read the image.
    I = imread(VtestData.imageFilename{i});
    
    % Run the detector.
    [bboxes,scores,labels] = detect(detector,I);
   
    % Collect the results.
    results.Boxes{i} = bboxes;
    results.Scores{i} = scores;
    results.Labels{i} = labels;
end

% Extract expected bounding box locations from test data.
expectedResults = VtestData(:, 2:end);

% Evaluate the object detector using average precision metric.
[ap, recall, precision] = evaluateDetectionPrecision(results, expectedResults);

% Plot precision/recall curve
plot(recall,precision)
xlabel('Recall')
ylabel('Precision')
grid on
title(sprintf('Average Precision = %.2f', ap))


test image from website and downloadit

filename='C:\Users\cra\Downloads\image_00063.jpg';
% Read a test image.
I = imread(filename);
%I=imresize(I,[224 224]);
imshow(I)
% Run the detector.
  [bboxes,scores,labels] = detect(detector,I);

% Annotate detections in the image.
I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
imshow(I)

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一个码农都是奢望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值