matlab/matlab-agentic-toolkit / skills-catalog/ai-and-statistics/matlab-deploy-embedded-ai
matlab-deploy-embedded-ai
VerifiedDeploy AI models to embedded hardware using MathWorks tools (MATLAB, Simulink, Embedded Coder). Covers two workflow patterns: (1) MathWorks-native or imported models rebuilt as dlnetwork for lean hardware, (2) direct C/C++ code generation from PyTorch and LiteRT models. Both patterns support all targets (Cortex-M/A/R, x86, GPU). Trigger when: user wants to deploy AI to embedded targets; generate C/CUDA from neural networks; compress AI models for MCU; integrate AI in Simulink for system-level simulation; import PyTorch/ONNX/TensorFlow models for embedded deployment; optimize AI for resource-constrained hardware; or use loadPyTorchExportedProgram, loadLiteRTModel, importNetworkFromPyTorch, importNetworkFromONNX, importNetworkFromTensorFlow, importNetworkFromKeras, dlquantizer, exportNetworkToSimulink, or Embedded Coder with AI models.
references/shared/ai-verification.md
View on GitHub# AI Verification
Verify AI model behavior before system integration. This phase applies to all
Pattern 1 workflows (both native and import paths) after training or import.
## Prerequisites
Check for AI Verification Library (support package, NOT toolbox):
```matlab
pkgs = matlabshared.supportpkg.getInstalled;
hasAIVerif = false;
if ~isempty(pkgs)
hasAIVerif = any(contains({pkgs.Name}, "AI Verification"));
end
```
If missing, direct user to Add-On Explorer. Do NOT install on their behalf.
## Converting Stats/ML Models for Verification
If the model was trained with `fitcnet` or `fitrnet`, convert for verification:
```matlab
dlNet = dlnetwork(mdl); % Convert fitcnet/fitrnet to dlnetwork
```
## Out-of-Distribution (OOD) Detection -- ALWAYS RECOMMEND
**CRITICAL:** Use the official `networkDistributionDiscriminator` function.
Do NOT implement custom OOD detection.
### Create OOD Discriminator
```matlab
% For tabular / image data
discriminator = networkDistributionDiscriminator(net, XTrain, [], "hbos");
% With tuning
discriminator = networkDistributionDiscriminator(net, XTrain, [], "hbos", ...
TruePositiveGoal=0.95, ...
FalsePositiveGoal=0.05);
% For sequence models: convert cell array to dlarray first
dlXTrain = dlarray(cat(3, XTrain{:}), "TCB");
discriminator = networkDistributionDiscriminator(net, dlXTrain, [], "hbos");
```
### OOD Detection Methods
| Method | Name-Value | Best For |
|--------|-----------|----------|
| Baseline | `"baseline"` | Simple softmax-based |
| ODIN | `"odin"` | Temperature-scaled softmax |
| Energy | `"energy"` | General purpose, robust |
| **HBOS** | `"hbos"` | **Recommended default** -- histogram-based, works for classification and regression |
### Evaluate OOD
**CRITICAL:** Do NOT use `predict(discriminator, ...)`. Use dedicated functions:
```matlab
% Check if samples are in-distribution
tf = isInNetworkDistribution(discriminator, XTest);
% Get confidence scores
scores = distributionScores(discriminator, XTest);
% Summarize
numOOD = sum(~tf);
fprintf("OOD samples: %d / %d (%.1f%%)\n", numOOD, numel(tf), 100*numOOD/numel(tf));
```
### Deploy OOD to Simulink / Embedded
```matlab
% Save discriminator for deployment
save("oodDiscriminator.mat", "discriminator");
% In Simulink MATLAB Function block or codegen entry-point:
function [prediction, isOOD] = predictWithOOD(inputData)
persistent net disc
if isempty(net)
net = coder.loadDeepLearningNetwork("trainedNet.mat");
disc = coder.loadNetworkDistributionDiscriminator("oodDiscriminator.mat");
end
prediction = predict(net, inputData);
tf = isInNetworkDistribution(disc, inputData);
isOOD = ~tf;
end
```
## Formal Verification (if AI Verification Library Available)
```matlab
% Robustness verification to Linf perturbations
epsilon = 0.01;
results = verifyNetworkRobustness(net, XTest(1:100,:), YTest(1:100), ...
PerturbationRadius=epsilon);
% Analyze results: "verified", "counterexample", or "unknown"
numVerified = sum(results.VerificationStatus == "verified");
fprintf("Verified: %d / %d\n", numVerified, height(results));
```
Notes:
- Computationally expensive; may not scale to large networks
- Check documentation for supported layer types
- Results depend on network architecture and perturbation radius
## Empirical Verification (No Additional Toolbox Required)
### Adversarial Robustness Testing
Apply small perturbations (FGSM-style) and check prediction stability:
```matlab
epsilon = 0.01;
numChanged = 0;
for i = 1:size(XTest, 1)
x = dlarray(single(XTest(i,:)), 'CB');
[y, grad] = dlfeval(@(x) predict(net, x), x);
xPerturbed = x + epsilon * sign(grad);
yPerturbed = predict(net, xPerturbed);
if argmax(extractdata(y)) ~= argmax(extractdata(yPerturbed))
numChanged = numChanged + 1;
end
end
fprintf("Predictions changed under perturbation: %d / %d\n", numChanged, size(XTest,1));
```
### Distribution Shift Analysis
Compare training vs. test feature distributions using histograms or
statistical tests to identify potential deployment-time distribution drift.
## Re-Verify After Compression
**CRITICAL:** After any compression (Phase 5 — Model Compression), re-run OOD detection on the
compressed model to ensure the discriminator still works correctly. Compression
can shift internal activations, affecting OOD boundaries.
Copyright 2026 The MathWorks, Inc.
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |