Code Generation for People Detection Using Deep Learning
This example shows how to generate CUDA® executable code to perform people detection using a pretrained deep learning network. In this example, you use the peopleDetector
object and its detect
method to identify people in images and configure it for code generation. The peopleDetector
object supports only library-free code generation. Therefore, the generated code is plain CUDA code that does not depend on the NVIDIA® cuDNN or TensorRT deep learning libraries.
This example requires a CUDA enabled NVIDIA GPU and a compatible driver to generate the CUDA MEX code. To build non-MEX applications, such as static libraries, dynamically linked libraries, or executables, you must have the NVIDIA CUDA toolkit installed and properly configure the environment variables of your system to locate the necessary compilers and libraries. For more information on the installation and setup requirements, see Third-Party Hardware (GPU Coder) and Setting Up the Prerequisite Products (GPU Coder), respectively.
To perform people detection, this example uses a pretrained people detection network trained on the COCO data set. To use the pretrained network, you must download and install the Computer Vision Toolbox Model For RTMDet Object Detection from Add-On Explorer. The rest of the example explains the steps involved.
Verify GPU Environment
Verify that the compilers and libraries required for this example are set up correctly by using the coder.checkGpuInstall
(GPU Coder) function.
envCfg = coder.gpuEnvConfig('host'); envCfg.DeepLibTarget = 'none'; envCfg.DeepCodegen = 1; envCfg.Quiet = 1; coder.checkGpuInstall(envCfg);
Load Pretrained Network
Load a pretrained people detector model by using the peopleDetector
object. This example uses a small network trained on the COCO data set. You can also use a small or large network model for detection. For information about different model names that you can specify, see peopleDetector
.
detector = peopleDetector("small-network-coco");
disp(detector)
peopleDetector with properties: ModelName: "small-network-coco" ClassNames: {'person'} InputSize: [640 640]
Save the people detector object to a MAT file.
save('pretrainedDetector.mat','detector');
Preallocate Array
Preallocate an array to represent the dimensions of the test image. The size of the test image used for inference must match the preallocated array size. For color images, you must specify the third dimension as 3 to represent the three color channels.
I = zeros(600,600,3);
Create Entry-Point Function
Create an entry-point function and save it as an M file. The entry-point function serves as the main function for code generation. MATLAB® Coder™ analyzes the entry-point function and any functions it calls, generating an equivalent C/C+ code. The entry-point function for this example is saved as smallNetworkDetect.m
. The smallNetworkDetect.m
entry-point function takes the pretrainedDetector.mat
and the inference image as inputs.
The function loads the peopleDetector
object from the pretrainedDetector.mat
file into a persistent variable detectorObj
. Load the peopleDetector
object for code generation by using the coder.load
function.
type smallNetworkDetect
function [bboxes,scores,labels] = smallNetworkDetect(I) %#codegen % Copyright 2024 The MathWorks, Inc. persistent detectorObj; if isempty(detectorObj) S = coder.load('pretrainedDetector.mat'); detectorObj = S.detector; end [bboxes,scores,labels] = detectorObj.detect(I); end
Generate MEX Code
Create a GPU code configuration object that can generate MEX files that are optimized for GPU execution. By default, the target language is set to C++.
cfg = coder.gpuConfig('mex');
cfg.TargetLang
Create a deep learning configuration object using the coder.DeepLearningConfig
(GPU Coder) function. Assign this object to the DeepLearningConfig
property of the GPU code configuration object. To generate plain CUDA code without dependencies on NVIDIA deep learning libraries, set the TargetLibrary
to 'none'
.
cfg.DeepLearningConfig = coder.DeepLearningConfig(TargetLibrary='none');
Run the codegen
command. The codegen
command uses the GPU code generation configuration to generate the MEX file smallNetworkDetect_mex
for the smallNetworkDetect
function. The input argument I
is the preallocated array that represents the expected size of the input test image.
codegen -config cfg smallNetworkDetect -args {I} -report
Code generation successful: View report
Detect People Using Generated MEX Code
Read an image into the MATLAB workspace. Resize the image to match the preallocated array size expected by the smallNetworkDetect_mex
function.
testImg = imread("visionteam.jpg");
testImg = imresize(testImg,[600 600]);
Run the generated mex code to detect people in the input image. The function returns the bounding boxes, corresponding scores, and labels for the detections.
[bboxes,scores,labels] = smallNetworkDetect_mex(testImg);
Annotate the test image with the detection results and display the annotated image.
dispImg = insertObjectAnnotation(testImg,"rectangle",bboxes,scores);
figure
imshow(dispImg)
Further Exploration
You can extend this example to perform code generation for face detection. Simply replace the peopleDetector
object with the faceDetector
object in the code. This substitution allows you to generate code tailored for detecting faces instead of people.
detector = faceDetector("small-network");