Main Content


Load YOLOX object detector model for code generation

Since R2023b



net = vision.loadYOLOXObjectDetector(filename) loads a pretrained YOLOX object detector model net from the MAT file filename. filename must be a compile-time constant.


This functionality requires Deep Learning Toolbox™ and the Computer Vision Toolbox™ Automated Visual Inspection Library. You can install the Computer Vision Toolbox Automated Visual Inspection Library from Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.


collapse all

Use the vision.loadYOLOXObjectDetector function to load a YOLOX object detector and generate C code for the network. You must have a pretrained yoloxObjectDetector object saved as a variable named detector in the file trainedYoloxObjectDetector.mat. For an example of training a YOLOX object detector, see trainYOLOXObjectDetector.

Load the pretrained yoloxObjectDetector object from the MAT file.


Create an entry-point function myYoloxObject.m that accepts an image as input. The entry-point function performs these operations:

  • Defines a persistent variable called myDetector. The persistent variable prevents reconstructing and reloading the network object during subsequent calls to the myYoloxObject function.

  • Loads the detector from the file trainedYoloxObjectDetector.mat into the myDetector variable using the vision.loadYOLOXObjectDetector function.

  • Detects object bounding boxes, scores, and labels on the input test image imTest using the detector.

function [bboxes,scores,labels] = myYoloxObject(imTest)
  persistent myDetector;
    if isempty(myDetector)
        myDetector = vision.loadYOLOXObjectDetector("trainedYoloxObjectDetector.mat");
  [bboxes,scores,labels] = detect(myDetector,imTest);

Read a test image from a graphics file.

imTest = imread("highway.png");

Create a coder.config (MATLAB Coder) configuration object for MEX code generation and set the target language to C++. On the configuration object, set the DeepLearningConfig property to the target library "mkldnn", for MKL-DNN targets. For more information about the supported target libraries for C++ code generation in deep learning networks, see Generate Generic C/C++ Code for Deep Learning Networks (MATLAB Coder).

The codegen (MATLAB Coder) function must determine the size, class, and complexity of MATLAB® function inputs. Use the -args option to specify the input test image imTest to the entry-point function. Use the -config option to pass the code configuration object myYoloxObject to codegen.

cfg = coder.config("mex");
cfg.TargetLang = "C++";
cfg.DeepLearningConfig = coder.DeepLearningConfig(TargetLibrary = "mkldnn");
codegen -args {imTest} -config cfg myYoloxObject -report;

The codegen command places all the generated files in the codegen folder. The folder contains the C code for the entry-point function myYoloxObject.c, as well as header and source files containing the C class definitions for the network, weight, and bias files.


The YOLOX object detector deep learning network architecture contains the spaceToDepthLayer. Code generation for spaceToDepthLayer is not supported for the target library "none".

Predict the bounding boxes, detection confidence scores, and object labels in the test image by calling the code generated mex file myYoloxObject_mex.

[predBboxes,predScores,predLabels] = myYoloxObject_mex(imTest)

predBboxes =

  3×4 single matrix

   97.9102  104.5984   39.9582   32.5561
   97.6418  104.9113   41.7848   32.5751
  146.5345   88.3480   82.4816   73.4598


predScores =

  3×1 single column vector



predLabels = 

  3×1 categorical array


Input Arguments

collapse all

Filename of the MAT file containing the pretrained YOLOX object detector, specified as a character vector or string scalar. The MAT file must exist on the MATLAB path and contain only the network to be loaded.

This input argument must be a compile-time constant.

Data Types: char | string

Output Arguments

collapse all

YOLOX object detector network, returned as a yoloxObjectDetector object.

Version History

Introduced in R2023b