%% Code Generation for Object Detection by Using Single Shot Multibox Detector
% This example shows how to generate CUDA® code for an SSD network (ssdObjectDetector object) and take advantage of the NVIDIA® cuDNN and TensorRT libraries. An SSD network is based on a feed-forward convolutional neural network that detect multiple objects within the image in a single shot. SSD network can be thought of as having two sub-networks. A feature extraction network, followed by a detection network.
% This example generates code for the network trained in the  Object Detection Using SSD Deep Learning example from Computer Vision Toolbox™. For more information, see Object Detection Using SSD Deep Learning. The Object Detection Using SSD Deep Learning example uses ResNet-50 for feature extraction. 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 to SSD.
%% Third-Party Prerequisites
% Required
% This example generates CUDA MEX and has the following third-party requirements.
% CUDA enabled NVIDIA GPU and compatible driver.
% Optional
% For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.
% NVIDIA toolkit.
% NVIDIA cuDNN library.
% Environment variables for the compilers and libraries. For more information, see Third-party Products and Environment Variables.
%% Verify GPU Environment
% Use the coder.checkGpuInstall function to verify that the compilers and libraries necessary for running this example are set up correctly.

envCfg = coder.gpuEnvConfig('host');
envCfg.DeepLibTarget = 'cudnn';
envCfg.DeepCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

%
%% Get Pretrained DAGNetwork

net = getSSDNW();

%
% The DAG network contains 180 layers including convolution, ReLU, and batch normalization layers, anchor box, SSD merge, focal loss, and other layers. To display an interactive visualization of the deep learning network architecture, use the  analyzeNetwork function.
% analyzeNetwork(net);
%% The ssdObj_detect Entry-Point Function
% The ssdObj_detect.m entry-point function takes an image input and runs the detector on the image using the deep learning network saved in the  ssdResNet50VehicleExample_20a.mat file. The function loads the network object from the ssdResNet50VehicleExample_20a.mat file into a persistent variable ssdObj and reuses the persistent object on subsequent detection calls.

type('ssdObj_detect.m')

%
%% Run MEX Code Generation
% To generate CUDA code for the  ssdObj_detect.m entry-point function, create a GPU code configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a CuDNN deep learning configuration object and assign it to the DeepLearningConfig property of the GPU code configuration object. Run the codegen command specifying an input size of [300,300,3]. This value corresponds to the input layer size of SSD Network.

cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn');
codegen -config cfg ssdObj_detect -args {ones(300,300,3,'uint8')} -report

%
%% Run Generated MEX
% Set up the video file reader and read the input video. Create a video player to display the video and the output detections.

videoFile = 'highway_lanechange.mp4';
videoFreader = vision.VideoFileReader(videoFile,'VideoOutputDataType','uint8');
depVideoPlayer = vision.DeployableVideoPlayer('Size','Custom','CustomSize',[640 480]);

%
% Read the video input frame-by-frame and detect the vehicles in the video using the detector.

cont = ~isDone(videoFreader);
while cont
    I = step(videoFreader);
    in = imresize(I,[300,300]);
    out = ssdObj_detect_mex(in);
    step(depVideoPlayer, out);
    % Exit the loop if the video player figure window is closed
    cont = ~isDone(videoFreader) && isOpen(depVideoPlayer); 
end

%
%
%% References
% [1] Liu, Wei, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng Yang Fu, and Alexander C. Berg. "SSD: Single shot multibox detector." In 14th European Conference on Computer Vision, ECCV 2016. Springer Verlag, 2016.
% Copyright 2019-2021 The MathWorks, Inc.