主要内容

Deploy Visual Inspection Code, Models, and Applications

R2026b

You can deploy trained visual inspection models and algorithms to production environments through these pathways: exporting to the Open Neural Network Exchange (ONNX) format for integration with external inference frameworks, generating optimized C or C++ code for desktop and embedded targets, generating CUDA® code for GPU targets, or packaging complete inspection workflows as standalone applications.

Deployment pathways from a MATLAB application and Visual Inspection Toolbox models to standalone executables via MATLAB Compiler, C or C++ via MATLAB Coder and Embedded Coder, CUDA code via GPU Coder, and ONNX via ONNX Export.

Export Models to ONNX

Export trained visual inspection models to ONNX format for integration with external inference frameworks on edge hardware, cloud platforms, or third-party runtimes such as ONNX Runtime. To export and deploy an ONNX model, follow this general workflow:

  1. Train or load a visual inspection model, such as a studentTeacherAnomalyDetector or yoloxObjectDetector.

  2. Export the model to an ONNX file by using the exportONNXNetwork function.

    exportONNXNetwork(detector,"myModel.onnx")
  3. Transfer the ONNX file to your target hardware or cloud environment.

  4. Run inference on the target by using an ONNX-compatible runtime (such as ONNX Runtime or TensorRT™).

  5. Postprocess the inference results. For YOLOX detectors, use visualinspection.detection.yolox.postprocess to convert raw network outputs into bounding boxes, labels, and scores. Student-Teacher anomaly detectors export as end-to-end models that return anomaly scores directly.

Note

For YOLOX detectors, preprocess input images by using visualinspection.detection.yolox.resizeLetterbox before inference. Both the preprocessing and postprocessing functions support C or C++ code generation for deployment on edge hardware.

For an example showing how to deploy an exported ONNX YOLOX model on an NVIDIA® Jetson™ Nano, see Deploy ONNX YOLOX Object Detector on NVIDIA Jetson Orin Nano for PCB Defect Detection.

Generate C/C++ and CUDA Code

Generate optimized C or C++ code for desktop and CPU-based deployment by using MATLAB Coder (MATLAB Coder), target embedded processors by using Embedded Coder® with a coder.EmbeddedCodeConfig configuration, or generate CUDA code for high-performance execution on NVIDIA GPUs by using GPU Coder (GPU Coder). These tools enable you to package trained networks, along with preprocessing and postprocessing logic, into deployable libraries and executables.

To generate code from a visual inspection workflow:

  1. Write a MATLAB® function that accepts inputs, such as images, and returns detection or classification results.

  2. Verify that all functions in the workflow support code generation. Use the codegen (MATLAB Coder) function to generate a MEX file as a first validation step.

  3. Generate standalone C or C++ code or a shared library by configuring codegen with a coder.config("lib") or coder.config("exe") configuration object. To target embedded processors, use a coder.EmbeddedCodeConfig (MATLAB Coder) object instead.

For an example showing how to generate code for a YOLOX object detector that detects, localizes, and classifies PCB defects, see Code Generation for Detect Defects on Printed Circuit Boards Using YOLOX Network.

Create Standalone Applications with MATLAB Compiler

Package visual inspection pipelines as standalone applications by using MATLAB Compiler™. Standalone applications run on machines that do not have MATLAB installed, enabling you to distribute anomaly detection, object detection, or other inspection workflows to end users in production environments.

To create a standalone application, write a MATLAB function that encapsulates your workflow. If the function requires workspace variables (such as a trained model), save them to a MAT file (for example, save("yourAnomalyDetector.mat","detector")) and load them at runtime. The following example function loads a saved anomaly detector, reads a test image, classifies the image, and saves an anomaly map overlay to disk. Adapt this template to your own workflow by replacing the processing logic.

function detectAnomaly(imagePath,matFile)

s = load(matFile);
detector = s.detector;

image = imread(imagePath);

[isAnomaly,anomalyScore,anomalyMap] = detect(detector,image);

fprintf("Image: %s\n",imagePath);
fprintf("isAnomaly: %d\n",isAnomaly);
fprintf("anomalyScore: %f\n",anomalyScore);

overlay = anomalyMapOverlay(image,anomalyMap);
imwrite(overlay,"anomalyOverlay.png");
fprintf("Overlay saved to anomalyOverlay.png\n");

end

Compile the function into a standalone executable by using the mcc (MATLAB Compiler) function. Include the MAT file containing the trained detector as an additional file.

mcc -m detectAnomaly.m -a yourAnomalyDetector.mat

Run the compiled executable from the system command prompt. Pass the path to the image you want to inspect, testImage.png, and the MAT file containing your saved variables as command-line arguments. On Windows®:

detectAnomaly.exe C:\path\to\testImage.png yourAnomalyDetector.mat

On Linux:

./detectAnomaly /path/to/testImage.png yourAnomalyDetector.mat

For more information about creating standalone executables, see Create Standalone Application from MATLAB (MATLAB Compiler).

List of Unsupported Functions and Apps

These functions are currently not supported for compilation by MATLAB Compiler:

  • trainFastFlowAnomalyDetector

  • trainFCDDAnomalyDetector

  • trainStudentTeacherAnomalyDetector

  • trainPatchCoreAnomalyDetector

  • trainYOLOXObjectDetector

  • shapemodel

  • visualAnomalyDetector

See Also

Topics