Matlab Import External AI Model
MathWorksOfficialCreated by MathWorks
- Skill
- Tool integration
- Software development
- BSD-2-Clause
- Low risk
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a comprehensive guide for importing trained deep learning models from PyTorch, ONNX, and Keras 3/TensorFlow 2.16+ into MATLAB. It covers the necessary workflows to convert these models into MATLAB dlnetwork objects, ensuring that layer structures and learnable parameters are preserved. The skill includes specific procedures for handling different file formats, such as .pt2, .pt, .onnx, and .keras, and provides guidance on validating the imported models against their source frameworks to ensure numerical consistency.
What it does
- Imports PyTorch models (.pt2 or .pt) using importNetworkFromPyTorch.
- Imports ONNX models using importNetworkFromONNX with support for InputDataFormats.
- Imports Keras 3 models using matlabsaver or tf_keras downgrade paths.
- Imports TensorFlow SavedModels using importNetworkFromTensorFlow.
- Provides workflows for validating imported networks against source framework outputs.
- Offers troubleshooting steps for common import issues like uninitialized networks or missing learnables.
When to use it
- When you need to bring a trained deep learning model from PyTorch, ONNX, or Keras into the MATLAB environment.
- When you encounter import errors, unsupported operator warnings, or uninitialized networks during model migration.
- When you need to verify that an imported model's inference results match the original framework's output.
- When you have a Keras 3 model and need to determine the correct import path based on your MATLAB version.
Advantages
- Enables seamless integration of external deep learning workflows into MATLAB.
- Supports modern model formats like PyTorch exported programs (.pt2).
- Provides structured paths for handling Keras 3 compatibility across different MATLAB releases.
- Includes built-in validation techniques to ensure numerical accuracy after import.
Usage examples
- “How do I import a PyTorch .pt2 file into MATLAB?”
- Use the importNetworkFromPyTorch function: net = importNetworkFromPyTorch('model.pt2');. No input size argument is required as shape information is embedded.
- “My ONNX model import results in an uninitialized network. What should I do?”
- Check the input layer dimensions and re-import using the InputDataFormats argument, for example: net = importNetworkFromONNX('model.onnx', InputDataFormats='BCSS');.
- “How can I import a Keras 3 model into MATLAB R2026a?”
- Use the matlabsaver utility in Python to save the model, then import it in MATLAB using: net = importNetworkFromKeras('exportedModelFolder');.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/matlab/matlab-import-external-ai-model/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip matlab-import-external-ai-model.zip -d .agents/skills
Documentation by MathWorks
Import Deep Learning Models into MATLAB
Import trained PyTorch, ONNX, or Keras 3 models into MATLAB as dlnetwork
objects and verify numerical correctness.
When to Use
User wants to import a deep learning model from PyTorch, ONNX, or Keras/TensorFlow
User has
.pt2,.pt,.onnx, or.kerasfiles to bring into MATLABUser mentions
importNetworkFromPyTorch,importNetworkFromONNX,importNetworkFromKeras, orimportNetworkFromTensorFlowUser mentions
torch.export.export,torch.jit.trace,PyTorchInputSizes,InputDataFormats, ormatlabsaverUser encounters import errors, unsupported operator warnings, uninitialized networks, or 0 learnables after import
User wants to validate that an imported model matches the source framework's outputs
When NOT to Use
Exporting MATLAB networks to ONNX/PyTorch (use
exportONNXNetwork/exportNetworkToPyTorch)Training or fine-tuning after import — use
/matlab-train-networkDeploying to embedded hardware — use
/matlab-deploy-embedded-aiSimulink integration after import (agent handles this well without guidance)
Router: Which Framework?
Q: What format is the source model?
|
+-- .pt2 (PyTorch exported program) ──────────> PYTORCH IMPORT below
+-- .pt (PyTorch traced model) ───────────────> PYTORCH IMPORT below
+-- .onnx ────────────────────────────────────> ONNX IMPORT below
+-- .keras / TensorFlow 2.16+ / matlabsaver ──> KERAS IMPORT below
+-- Unknown ("import my model") ──────────────> Ask: framework? file extension?PyTorch Import
Full pipeline: export from PyTorch → import into MATLAB → validate numerics.
Determine Starting Point
| User has | Action |
|---|---|
| PyTorch model (code or saved) | Export as .pt2 first → see references/pytorch-export-guidance.md |
.pt2 file (exported program) | Import directly (below) |
.pt file (traced model) | Import with input sizes (below) |
Always prefer .pt2 over .pt. If user has a traced model, recommend re-exporting
with torch.export.export first. Only use traced path if re-export is not feasible.
Import .pt2 (Exported Program)
net = importNetworkFromPyTorch("model.pt2");No input size argument needed — shape info is embedded in the .pt2 file.
Import .pt (Traced Model)
net = importNetworkFromPyTorch("model.pt", ...
PyTorchInputSizes=[1 3 224 224]);PyTorchInputSizes is mandatory for traced models. Specify sizes in PyTorch
dimension ordering. For multiple inputs use a cell array: {[1 3 256 256], [1 10]}.
Name-Value Arguments
| Argument | When to use |
|---|---|
PyTorchInputSizes | Required for traced models (.pt). Not needed for .pt2 |
Namespace | Control where auto-generated custom layer files are stored |
PreferredNestingType | Choose "networklayer" (default) or "customlayer" |
PyTorch Critical Mistakes
| Mistake | Correct Approach |
|---|---|
Using InputShape NV argument | Does not exist — use PyTorchInputSizes for .pt, nothing for .pt2 |
Using PackageName NV argument | Deprecated — use Namespace |
Not calling model.to("cpu") before export | Always model.to("cpu") before export |
| Not checking PyTorch version before export | Assert torch.__version__ starts with "2.8" |
Passing PyTorchInputSizes for .pt2 | Unnecessary — .pt2 embeds shape info, omit it |
| Guessing input size for unknown models | Always ask the user for exact input dimensions |
Assuming net.InputNames matches forward() order | Importer may reorder — always check net.InputNames |
PyTorch Conventions
Always
model.to("cpu")andmodel.eval()before exportAlways verify PyTorch version is 2.8 before exporting as .pt2
Never guess input sizes — ask the user or inspect the model
Use
NamespacenotPackageNamefor custom layer storagePrefer .pt2 over .pt — recommend
torch.export.exportovertorch.jit.trace
PyTorch References
references/pytorch-export-guidance.md— Full Python-side export procedurereferences/pytorch-import-guidance.md— Detailed MATLAB import for both formatsreferences/pytorch-numeric-validation.md— Dimension conversion and tolerance comparisonreferences/pytorch-placeholder-guidance.md— Implementing unsupported ops in custom layersscripts/validateImportedNetwork.m— Helper function for numeric validation against .npy reference data
ONNX Import
Import ONNX models using importNetworkFromONNX, diagnose issues, verify numerics.
Workflow
1. IMPORT → importNetworkFromONNX with appropriate NVPs
2. DIAGNOSE → Check initialization, custom layers, warnings
3. RESOLVE → Fix issues (InputDataFormats, placeholder functions)
4. VERIFY → Compare outputs against ONNX Runtime (if installed)CRITICAL: Do NOT re-import after step 3. Re-importing regenerates +ops/ and overwrites all custom implementations.
Import
net = importNetworkFromONNX("model.onnx");If you know the input format:
net = importNetworkFromONNX("model.onnx", InputDataFormats="BCSS");Diagnose and Resolve
If net.Initialized is false, read the input shape and re-import with InputDataFormats:
net = importNetworkFromONNX("model.onnx");
if ~net.Initialized
inputLayer = net.Layers(1);
fprintf("NumDims: %d\n", inputLayer.NumDims);
endInputDataFormats Reference
Characters: B (batch), C (channel), S (spatial), T (time), U (unspecified).
| ONNX Input Shape | InputDataFormats |
|---|---|
| [N, C, H, W] | "BCSS" |
| [N, C] | "BC" |
| [N, T, C] | "BTC" |
| [N, C, T] | "BCT" |
Verify Against ONNX Runtime
If onnxruntime is installed in the user's Python environment, compare outputs. If not installed, skip — do not ask the user to install it.
try
ort = py.importlib.import_module("onnxruntime");
ortAvailable = true;
catch
ortAvailable = false;
endSee references/onnx-validation-workflow.md for the full comparison procedure.
ONNX Critical Mistakes
| Mistake | Correct Approach |
|---|---|
Use importONNXNetwork or importONNXLayers | Legacy — always use importNetworkFromONNX |
| Re-import after implementing placeholders | Import once, then modify. Never re-import. |
| Guess InputDataFormats randomly | Read input shape from uninitialized network first |
| Skip numeric verification when ORT is available | Compare against ONNX Runtime if installed |
ONNX Conventions
Always use
importNetworkFromONNX— never legacy APIsVerify numerically against ONNX Runtime after import (if installed)
Never re-import after modifying network or implementing placeholders
Use
dlarraywith explicit format strings:dlarray(data, "SSCB")Report max absolute difference and assert tolerance < 1e-4 for float32
ONNX Reference
references/onnx-validation-workflow.md— Full ORT comparison including multi-output models
Keras Import
Import Keras 3 / TensorFlow 2.16+ models with full layer structure and learnables.
Decision Tree
Q1: What MATLAB release is available?
+-- R2026a or newer ──> PATH 1 (matlabsaver + importNetworkFromKeras)
+-- R2025b or older ──> Q2
Q2: Does the model use Keras 3-specific features? (keras.ops, multi-backend)
+-- No (standard layers) ──> PATH 2 (tf_keras downgrade)
+-- Yes ────────────────────> PATH 3 (ONNX export fallback)Path 1: matlabsaver + importNetworkFromKeras (R2026a+)
Python:
import matlabsaver
matlabsaver.save_for_matlab(model, "exportedModelFolder")Apply the config.json patch for Keras 3.10+ compatibility (see references/keras-matlabsaver-workflow.md).
MATLAB:
net = importNetworkFromKeras("exportedModelFolder");
assert(numel(net.Learnables.Value) > 0, "Import failed: 0 learnables")Path 2: tf_keras Downgrade (Pre-R2026a, Standard Layers Only)
Python:
import os
os.environ["TF_USE_LEGACY_KERAS"] = "1" # MUST be before importing TensorFlow
import tf_keras as keras
model.save("savedModelFolder")MATLAB:
net = importNetworkFromTensorFlow("savedModelFolder");Path 3: ONNX Export (Fallback)
Requires tf2onnx in the Python environment: pip install tf2onnx
Python:
model.export("exportedModel.onnx", format="onnx")MATLAB:
net = importNetworkFromONNX("exportedModel.onnx");Keras Critical Mistakes
| Mistake | Correct Approach |
|---|---|
importNetworkFromKeras fails with "Brace indexing..." | Keras 3.10+ changed config.json — apply the patch (see reference) |
model.export("folder") then importNetworkFromTensorFlow | No keras_metadata.pb → 0 learnables. Use matlabsaver instead |
TF_USE_LEGACY_KERAS=1 set after import tensorflow | Must be set before any TF import |
Using deprecated importKerasNetwork | Use importNetworkFromKeras (R2026a+) or Path 2/3 |
Keras Conventions
Always verify imported network has non-zero learnables
Always check MATLAB release before choosing import path
Prefer Path 1 > Path 2 > Path 3 (ordered by fidelity)
Report number of layers and learnables after import
Keras References
references/keras-matlabsaver-workflow.md— Full matlabsaver procedure for R2026a+references/keras-tf-keras-downgrade.md— tf_keras setup for pre-R2026a
Key Functions
| Function | Framework | Purpose |
|---|---|---|
importNetworkFromPyTorch | PyTorch | Import .pt2 or .pt as dlnetwork |
importNetworkFromONNX | ONNX | Import .onnx as dlnetwork |
importNetworkFromKeras | Keras | Import Keras 3 folder as dlnetwork (R2026a+) |
importNetworkFromTensorFlow | TF/Keras | Import TF SavedModel as dlnetwork |
torch.export.export | PyTorch | Export model as .pt2 (Python) |
matlabsaver.save_for_matlab | Keras | Export Keras 3 for MATLAB (Python) |
predict | All | Run inference on imported dlnetwork |
dlarray | All | Labeled multi-dimensional array for deep learning |
Copyright 2026 The MathWorks, Inc.
Files
- ATTRIBUTION.mdAdded by AutoGineer1.1 KiB
- LICENSELicence file added by AutoGineer1.4 KiB
- SKILL.md11.1 KiB
- manifest.yaml626 B
- references/keras-matlabsaver-workflow.md4.0 KiB
- references/keras-tf-keras-downgrade.md3.8 KiB
- references/onnx-validation-workflow.md6.4 KiB
- references/pytorch-export-guidance.md3.6 KiB
- references/pytorch-import-guidance.md3.9 KiB
- references/pytorch-numeric-validation.md4.3 KiB
- references/pytorch-placeholder-guidance.md4.3 KiB
- scripts/validateImportedNetwork.m4.3 KiB
references/onnx-validation-workflow.md
Validation Workflow
Mandatory verification that an imported ONNX model produces numerically correct outputs. Every import must pass this check before declaring success.
Prerequisites
Imported
dlnetworkobject in MATLABPython available in MATLAB (
pyenvreturns a valid environment)onnxruntimealready installed in the user's Python environment
Important: Only run this workflow if onnxruntime is already present. Do not ask the user to install it or suggest installation commands.
Full Procedure
1. Generate Deterministic Test Input
Use a fixed random seed so results are reproducible:
rng(42);Determine the input shape and format by inspecting the network's input layer. The layer class tells you which format to use:
| Input Layer Class | InputSize | dlarray Format | Example |
|---|---|---|---|
ImageInputLayer | [H W C] | "SSCB" | randn([H W C 1], "single") |
SequenceInputLayer | numFeatures (scalar) | "CBT" | randn([C T 1], "single") where T is an arbitrary sequence length |
FeatureInputLayer | numFeatures (scalar) | "CB" | randn([C 1], "single") |
inputLayer = net.Layers(1);
inputSize = inputLayer.InputSize;
if isa(inputLayer, "nnet.cnn.layer.ImageInputLayer")
inputData = randn([inputSize 1], "single");
X = dlarray(inputData, "SSCB");
elseif isa(inputLayer, "nnet.cnn.layer.SequenceInputLayer")
seqLen = 10; % Arbitrary sequence length for testing
inputData = randn([inputSize seqLen 1], "single");
X = dlarray(inputData, "CBT");
elseif isa(inputLayer, "nnet.cnn.layer.FeatureInputLayer")
inputData = randn([inputSize 1], "single");
X = dlarray(inputData, "CB");
endFor uninitialized networks (input layer is CustomInputLayerMultiOutput),
read the InputInformation property which prints the expected ONNX shape and
required format. Use NumDims to determine rank. You will need to initialize
the network with InputDataFormats before running inference.
2. Run MATLAB Inference
Y = predict(net, X);
matlabOutput = extractdata(Y);3. Run ONNX Runtime Inference
ort = py.importlib.import_module("onnxruntime");
np = py.importlib.import_module("numpy");
sess = ort.InferenceSession("model.onnx");
inputs = sess.get_inputs();
inputInfo = inputs{1};
inputName = char(inputInfo.name);4. Permute MATLAB Data to ONNX Dimension Order
MATLAB and ONNX use reversed dimension ordering. MATLAB's dlarray stores data in DLT order (reverse of ONNX convention). You must permute the input data before passing it to ONNX Runtime, and permute ORT outputs back for comparison.
Why permutation is required: MATLAB uses column-major storage with dimensions ordered as spatial→channel→batch (SSCB), while ONNX uses row-major convention with batch→channel→spatial (NCHW). Without permutation, the comparison is meaningless — you'd be comparing different data arrangements.
Input permutation (MATLAB → ONNX):
The general rule is to reverse the MATLAB dimension order. Use permutation vectors based on the dlarray format:
% Image SSCB (H,W,C,B) → ONNX NCHW (B,C,H,W): reverse 4D
inputForOnnx = permute(inputData, [4, 3, 1, 2]);
% Sequence CBT (C,B,T) → ONNX (B,T,C): swap to batch-first
inputForOnnx = permute(inputData, [2, 3, 1]);
% Feature CB (C,B) → ONNX BC (B,C): transpose
inputForOnnx = permute(inputData, [2, 1]);Output permutation (ONNX → MATLAB):
ORT returns outputs in ONNX dimension order. Permute back to MATLAB order before comparing:
% Image output: ORT NCHW (B,C,H,W) → MATLAB SSCB (H,W,C,B)
ortOutputPermuted = permute(ortOutput, [3, 4, 2, 1]);
% Sequence output: ORT (B,T,C) → MATLAB CBT (C,B,T)
ortOutputPermuted = permute(ortOutput, [3, 1, 2]);
% Feature/classification output: ORT BC (B,C) → MATLAB CB (C,B)
ortOutputPermuted = permute(ortOutput, [2, 1]);Tip: If the ORT output is 1D or 2D with a batch size of 1, you can often
skip output permutation and compare with matlabOutput(:) vs ortOutput(:)
since flattening makes order irrelevant for scalar or vector outputs.
Handling numpy shape issues
When passing MATLAB arrays to numpy, small arrays (1D or 2D with a singleton
dimension) may lose dimensions. Use numpy.reshape to enforce the expected
shape from the ONNX session metadata:
% If numpy flattens the input, reshape to match expected ONNX shape:
onnxShape = inputInfo.shape;
pyInput = np.array(inputForOnnx(:)', pyargs("dtype", np.float32));
expectedShape = int32(cell2mat(cell(onnxShape)));
pyInput = pyInput.reshape(expectedShape);5. Execute and Compare
pyInput = np.array(inputForOnnx, pyargs("dtype", np.float32));
inputDict = py.dict(pyargs(inputName, pyInput));
result = sess.run(py.list(), inputDict);
ortOutput = single(result{1});
% Permute ORT output back to MATLAB order if needed (see Step 4)
% For scalar/vector outputs, direct comparison usually works:
maxDiff = max(abs(matlabOutput(:) - ortOutput(:)));
meanDiff = mean(abs(matlabOutput(:) - ortOutput(:)));
fprintf("Max absolute difference: %e\n", maxDiff);
fprintf("Mean absolute difference: %e\n", meanDiff);6. Assert Tolerance
assert(maxDiff < 1e-4, ...
"FAIL: Max difference %e exceeds float32 tolerance 1e-4", maxDiff);
fprintf("PASS: MATLAB output matches ONNX Runtime (max diff: %e)\n", maxDiff);Tolerance guidelines:
float32 models:
< 1e-4(typical accumulation error)float16 models:
< 1e-2If difference is
> 1e-3for float32, investigate which layer diverges
When ONNX Runtime Is Not Installed
If onnxruntime is not available, skip numeric verification. Do not:
Ask the user to install
onnxruntimeSuggest
pip installcommandsUse alternative verification methods (e.g.,
onnx.reference.ReferenceEvaluator)
Simply inform the user that numeric verification was skipped because ONNX Runtime is not present in their environment.
Multi-Output Models
For models with multiple outputs, compare each output separately:
outputs = sess.get_outputs();
for i = 1:int32(py.len(outputs))
outputName = char(outputs{i}.name);
ortOut = single(result{i});
% Get corresponding MATLAB output
matlabOut = extractdata(predict(net, X, Outputs=outputName));
maxDiff = max(abs(matlabOut(:) - ortOut(:)));
fprintf("Output '%s': max diff = %e\n", outputName, maxDiff);
endCopyright 2026 The MathWorks, Inc.
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Python
Security
What our scanner noticed· Low risk
- Instructions that use the network
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- MathWorks
- Publisher
- MathWorks (Official)
- Source
- matlab/matlab-agentic-toolkit · skills-catalog/ai-and-statistics/matlab-import-external-ai-model · commit 2a7f215
- Licence
- BSD 2-Clause License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE.md)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by MathWorks or MathWorks.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-import-external-ai-model.zip