matlab-import-external-ai-model

Import PyTorch, ONNX, and Keras deep learning models into MATLAB as dlnetwork objects.

995|122|Updated Apr 3, 2026
One-click install
npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-import-external-ai-model
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: matlab-import-external-ai-model
Source: https://github.com/matlab/matlab-agentic-toolkit/tree/main/skills-catalog/ai-and-statistics/matlab-import-external-ai-model
Command: npx skills add https://github.com/matlab/matlab-agentic-toolkit --skill matlab-import-external-ai-model

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

Bringing trained deep learning models from PyTorch, ONNX, or Keras/TensorFlow into MATLAB is error-prone: wrong export formats, missing input sizes, unsupported operators, and silent numeric mismatches can waste hours. This Skill guides the full import pipeline and verifies numerical correctness against the source framework.

Core Features & Use Cases

  • PyTorch Import: Export models with torch.export.export to .pt2 and import via importNetworkFromPyTorch, with guidance for traced .pt files requiring PyTorchInputSizes.
  • ONNX Import: Import .onnx models with importNetworkFromONNX, resolve uninitialized networks using InputDataFormats, and validate outputs against ONNX Runtime.
  • Keras 3 Import: Choose between matlabsaver + importNetworkFromKeras (R2026a+), tf_keras downgrade, or ONNX fallback based on MATLAB release and model features.
  • Use Case: You have a ResNet model trained in PyTorch. Export it as .pt2, import it into MATLAB as a dlnetwork, implement any placeholder custom layers, and confirm the MATLAB output matches PyTorch within 1e-4 tolerance.

Quick Start

Import my PyTorch model file model.pt2 into MATLAB and verify its outputs match the original PyTorch model.

Frequently Asked Questions about matlab-import-external-ai-model

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I import a PyTorch model into MATLAB?

Export the model in Python using torch.export.export to create a .pt2 file, then call importNetworkFromPyTorch("model.pt2") in MATLAB. For traced .pt files, you must also pass PyTorchInputSizes with dimensions in PyTorch NCHW order.

How do I import an ONNX model into MATLAB?

Use importNetworkFromONNX("model.onnx") to get a dlnetwork. If the network is uninitialized, inspect the input layer's NumDims and re-import with InputDataFormats such as "BCSS" for image data. Avoid legacy importONNXNetwork and importONNXLayers.

How do I import a Keras 3 model into MATLAB?

On R2026a or newer, save the model in Python with matlabsaver.save_for_matlab and import it with importNetworkFromKeras. On older releases with standard layers only, use the tf_keras downgrade path with importNetworkFromTensorFlow, or export to ONNX as a fallback.

Why does my imported network have 0 learnables?

Zero learnables usually means the model was exported with model.export instead of matlabsaver.save_for_matlab, so no keras_metadata.pb was produced. Re-export using matlabsaver and verify numel(net.Learnables.Value) is greater than zero after import.

Which PyTorch version is required for .pt2 export to MATLAB?

The .pt2 export must be created with PyTorch 2.8 exactly, since MATLAB's converter targets that exported program format. Newer or older versions cause incompatible format errors, so create a dedicated environment with torch==2.8.0 if needed.

How do I verify an imported model matches the original framework?

Run predict on the dlnetwork with a deterministic input and compare against reference outputs from PyTorch or ONNX Runtime, permuting dimensions between MATLAB and framework orderings. For float32 models, the maximum absolute difference should be below 1e-4.