how load a pre-trained yolov2 net in matlab

Here is an example code to load a pre-trained YOLOv2 network in MATLAB for object detection:

main.m
% Load the pre-trained network
net = load('yolov2ResNet50VehicleExample.mat');
lgraph = net.lgraph;

% Specify the input size of the network
inputSize = [224 224 3];
inputLayer = imageInputLayer(inputSize);

% Replace the input layer of the network with the new input layer
lgraph = replaceLayer(lgraph,'data',inputLayer);

% Set the network training to false
lgraph = setTrainingState(lgraph,'output_1', 'bn_2d_37', false);
lgraph = setTrainingState(lgraph,'output_2', 'bn_2d_44', false);

% Analyze the network layers
analyzeNetwork(lgraph);

% Classify a test image
I = imread('car2.jpg');
I = imresize(I,inputSize(1:2));
[bboxes,scores] = detect(net.detector,I);

% Display the object detection results
if ~isempty(bboxes)
   I = insertObjectAnnotation(I,'rectangle',bboxes,scores);
end  
imshow(I);
804 chars
29 lines

You can replace the inputSize, inputPath, and image name with your own. You can download the pre-trained yolov2ResNet50VehicleExample.mat and use it to load and detect objects in a test image.

gistlibby LogSnag