$npx -y skills add matlab/matlab-agentic-toolkit --skill matlab-import-external-ai-modelImport PyTorch, ONNX, or Keras 3 / TensorFlow 2.16+ deep learning models into MATLAB as dlnetwork objects. Use when importing .pt2 exported programs, traced .pt files, .onnx models, or Keras 3 models via matlabsaver. Covers importNetworkFromPyTorch, importNetworkFromONNX, importN
| 1 | # Import Deep Learning Models into MATLAB |
| 2 | |
| 3 | Import trained PyTorch, ONNX, or Keras 3 models into MATLAB as `dlnetwork` |
| 4 | objects and verify numerical correctness. |
| 5 | |
| 6 | ## When to Use |
| 7 | |
| 8 | - User wants to import a deep learning model from PyTorch, ONNX, or Keras/TensorFlow |
| 9 | - User has `.pt2`, `.pt`, `.onnx`, or `.keras` files to bring into MATLAB |
| 10 | - User mentions `importNetworkFromPyTorch`, `importNetworkFromONNX`, `importNetworkFromKeras`, or `importNetworkFromTensorFlow` |
| 11 | - User mentions `torch.export.export`, `torch.jit.trace`, `PyTorchInputSizes`, `InputDataFormats`, or `matlabsaver` |
| 12 | - User encounters import errors, unsupported operator warnings, uninitialized networks, or 0 learnables after import |
| 13 | - User wants to validate that an imported model matches the source framework's outputs |
| 14 | |
| 15 | ## When NOT to Use |
| 16 | |
| 17 | - Exporting MATLAB networks to ONNX/PyTorch (use `exportONNXNetwork` / `exportNetworkToPyTorch`) |
| 18 | - Training or fine-tuning after import — use `/matlab-train-network` |
| 19 | - Deploying to embedded hardware — use `/matlab-deploy-embedded-ai` |
| 20 | - Simulink integration after import (agent handles this well without guidance) |
| 21 | |
| 22 | ## Router: Which Framework? |
| 23 | |
| 24 | ``` |
| 25 | Q: What format is the source model? |
| 26 | | |
| 27 | +-- .pt2 (PyTorch exported program) ──────────> PYTORCH IMPORT below |
| 28 | +-- .pt (PyTorch traced model) ───────────────> PYTORCH IMPORT below |
| 29 | +-- .onnx ────────────────────────────────────> ONNX IMPORT below |
| 30 | +-- .keras / TensorFlow 2.16+ / matlabsaver ──> KERAS IMPORT below |
| 31 | +-- Unknown ("import my model") ──────────────> Ask: framework? file extension? |
| 32 | ``` |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## PyTorch Import |
| 37 | |
| 38 | Full pipeline: export from PyTorch → import into MATLAB → validate numerics. |
| 39 | |
| 40 | ### Determine Starting Point |
| 41 | |
| 42 | | User has | Action | |
| 43 | |----------|--------| |
| 44 | | PyTorch model (code or saved) | Export as .pt2 first → see `references/pytorch-export-guidance.md` | |
| 45 | | `.pt2` file (exported program) | Import directly (below) | |
| 46 | | `.pt` file (traced model) | Import with input sizes (below) | |
| 47 | |
| 48 | **Always prefer .pt2 over .pt.** If user has a traced model, recommend re-exporting |
| 49 | with `torch.export.export` first. Only use traced path if re-export is not feasible. |
| 50 | |
| 51 | ### Import .pt2 (Exported Program) |
| 52 | |
| 53 | ```matlab |
| 54 | net = importNetworkFromPyTorch("model.pt2"); |
| 55 | ``` |
| 56 | |
| 57 | No input size argument needed — shape info is embedded in the .pt2 file. |
| 58 | |
| 59 | ### Import .pt (Traced Model) |
| 60 | |
| 61 | ```matlab |
| 62 | net = importNetworkFromPyTorch("model.pt", ... |
| 63 | PyTorchInputSizes=[1 3 224 224]); |
| 64 | ``` |
| 65 | |
| 66 | `PyTorchInputSizes` is **mandatory** for traced models. Specify sizes in PyTorch |
| 67 | dimension ordering. For multiple inputs use a cell array: `{[1 3 256 256], [1 10]}`. |
| 68 | |
| 69 | ### Name-Value Arguments |
| 70 | |
| 71 | | Argument | When to use | |
| 72 | |----------|-------------| |
| 73 | | `PyTorchInputSizes` | **Required** for traced models (.pt). Not needed for .pt2 | |
| 74 | | `Namespace` | Control where auto-generated custom layer files are stored | |
| 75 | | `PreferredNestingType` | Choose `"networklayer"` (default) or `"customlayer"` | |
| 76 | |
| 77 | ### PyTorch Critical Mistakes |
| 78 | |
| 79 | | Mistake | Correct Approach | |
| 80 | |---------|-----------------| |
| 81 | | Using `InputShape` NV argument | Does not exist — use `PyTorchInputSizes` for .pt, nothing for .pt2 | |
| 82 | | Using `PackageName` NV argument | Deprecated — use `Namespace` | |
| 83 | | Not calling `model.to("cpu")` before export | Always `model.to("cpu")` before export | |
| 84 | | Not checking PyTorch version before export | Assert `torch.__version__` starts with "2.8" | |
| 85 | | Passing `PyTorchInputSizes` for .pt2 | Unnecessary — .pt2 embeds shape info, omit it | |
| 86 | | Guessing input size for unknown models | Always ask the user for exact input dimensions | |
| 87 | | Assuming `net.InputNames` matches `forward()` order | Importer may reorder — always check `net.InputNames` | |
| 88 | |
| 89 | ### PyTorch Conventions |
| 90 | |
| 91 | - Always `model.to("cpu")` and `model.eval()` before export |
| 92 | - Always verify PyTorch version is 2.8 before exporting as .pt2 |
| 93 | - Never guess input sizes — ask the user or inspect the model |
| 94 | - Use `Namespace` not `PackageName` for custom layer storage |
| 95 | - Prefer .pt2 over .pt — recommend `torch.export.export` over `torch.jit.trace` |
| 96 | |
| 97 | ### PyTorch References |
| 98 | |
| 99 | - `references/pytorch-export-guidance.md` — Full Python-side export procedure |
| 100 | - `references/pytorch-import-guidance.md` — Deta |