matlab-integrate-pytorch-vision
>-
git clone --depth 1 https://github.com/matlab/matlab-agentic-toolkit /tmp/matlab-integrate-pytorch-vision && cp -r /tmp/matlab-integrate-pytorch-vision/skills-catalog/image-processing-and-computer-vision/matlab-integrate-pytorch-vision ~/.claude/skills/matlab-integrate-pytorch-visionSKILL.md
# MPyReq MATLAB Interface Builder
Build a MATLAB interface to a Python/PyTorch model repository using the MPyReq framework.
## When to Use
- User asks to interface MATLAB with a Python image processing or computer vision model (segmentation, depth estimation, object detection, image generation, super-resolution, pose estimation, optical flow, salient object detection, etc.)
- User provides a GitHub repository URL for a vision/image model and wants to call it from MATLAB
- User asks to "create an MPyReq wrapper" or "MPyReq demo" for an image/CV model
- User wants to run a pip-installable vision model library (e.g., Cellpose, SAM2, Depth-Pro, BiRefNet, StarDist) from MATLAB
## When Not to Use
- General-purpose Python-MATLAB interfacing (no vision/image model involved)
- Non-vision models: NLP, audio, tabular, reinforcement learning, time-series
- Model deployment, containerization, or inference servers
- Pure MATLAB image processing workflows with no Python dependency
- Creating Python code (this skill creates MATLAB code that calls Python)
## Prerequisites: MPyReq on the MATLAB Path
Before generating any demo script, verify that MPyReq is available. Run `which MPyReq` via the MATLAB MCP server (if available) or ask the user to confirm.
### If MPyReq is NOT on the MATLAB path:
1. **Download MPyReq** from the MATLAB File Exchange:
https://mathworks.com/matlabcentral/fileexchange/182230-matlab-based-python-requirements-manager
2. **Install it** — either:
- Open the downloaded `.mltbx` file in MATLAB (double-click), which installs it as a MATLAB Add-On automatically, or
- Extract the files and add the folder containing `MPyReq.m` to the MATLAB path:
```matlab
addpath("/path/to/mpyreq");
savepath; % persist across sessions
```
3. **Verify** by running `which MPyReq` in MATLAB — it should return the path to `MPyReq.m`.
Do not proceed with demo generation until MPyReq is confirmed on the path.
## Input
Ask the user for:
1. **GitHub repository URL** — the Python model repository to interface with
2. **What the model does** (optional) — to help identify the right inference example
## Step 1: Analyze the Repository
Fetch and analyze the GitHub repository to determine:
- **Python version requirement** — check `setup.py`, `setup.cfg`, `pyproject.toml`, or README for the required Python version. Default to `"3.12"` if not specified. Use `"3.11"` if the project needs older compatibility.
- **Installation method** — determine how the project is installed:
- If it uses `torch.hub.load()`: only need `torch` and `torchvision` as pip packages (model downloads automatically)
- If it's a pip-installable package: use `MPyReq.pipPackage()`
- If it's a non-packaged git repo: use `MPyReq.gitrepo()` + `MPyReq.requirementTextFile()` if a `requirements.txt` exists
- If it needs `pip install git+<url>`: use `MPyReq.pipPackage("git+<url>", Name="<ProjectName>")`
- **Additional dependencies** — any extra pip packages needed (e.g., `torch`, `torchvision`, etc.)
- **Model weights** — determine how weights are loaded:
- `torch.hub.load()` — weights download automatically, no `MPyReq.weights()` needed
- Direct URL download — use `MPyReq.weights()` with the checkpoint URL
- HuggingFace `.from_pretrained()` — weights download automatically via the library
- **Inference example** — locate the primary inference/prediction code in the README or example scripts
- **Preprocessing requirements** — check if the model requires specific input normalization (e.g., ImageNet mean/std), resizing, or center cropping
## Step 2: Generate the MPyReq Setup Script
Create a MATLAB `.m` file that sets up the Python environment. Follow these patterns from the demo files:
### MANDATORY: Installation folder setup
Every generated script MUST begin with `MPyReq.setInstallFolder()`. This tells MPyReq where to download Python, packages, and model weights. Without this, MPyReq will show a GUI dialog which blocks non-interactive execution. Also include `MPyReq.autoAcceptDownloadPrompts(true)` to avoid interactive confirmation prompts.
```matlab
% Set installation folder (SSD recommended, ~15+ GB free space)
% Change this path to a suitable location on your machine
MPyReq.setInstallFolder(fullfile(tempdir, "MPyReq"));
MPyReq.autoAcceptDownloadPrompts(true);
```
### Pattern A: Simple pip package (like Cellpose)
```matlab
MPyReq.setInstallFolder(fullfile(tempdir, "MPyReq"));
MPyReq.autoAcceptDownloadPrompts(true);
MPyReq.python("3.12");
MPyReq.pipPackage("<package_name>");
```
### Pattern B: Git repo as pip package (like SAM2)
```matlab
MPyReq.setInstallFolder(fullfile(tempdir, "MPyReq"));
MPyReq.autoAcceptDownloadPrompts(true);
MPyReq.python("3.12");
MPyReq.pipPackage("git+https://github.com/<org>/<repo>.git", Name="<RepoName>");
```
### Pattern C: Git repo + requirements.txt (like VGGT, BiRefNet)
```matlab
MPyReq.setInstallFolder(fullfile(tempdir, "MPyReq"));
MPyReq.autoAcceptDownloadPrompts(true);
MPyReq.python("3.11");
MPyReq.gitrepo("https://github.com/<org>/<repo>.git");
reqTxt = MPyReq.pathTo("<repo>") + filesep + "requirements.txt";
MPyReq.requirementTextFile(reqTxt, Name="<repo>Packages");
```
### Pattern D: torch.hub model (like DINOv2, ResNet, etc.)
When the model uses `torch.hub.load()`, no git clone or weights download is needed — just install torch/torchvision:
```matlab
MPyReq.setInstallFolder(fullfile(tempdir, "MPyReq"));
MPyReq.autoAcceptDownloadPrompts(true);
MPyReq.python("3.12");
MPyReq.pipPackage("torch");
MPyReq.pipPackage("torchvision");
% Model loads automatically via torch.hub:
model = py.torch.hub.load('org/repo', 'model_name');
```
### Weights download pattern
Only needed when weights are NOT handled by `torch.hub.load()` or `.from_pretrained()`:
```matlab
MPyReq.weights("<weights_url>", DownloadTo=MPyReq.pathTo("<RepoName>") + filesep + "checkpoints");
```
## Step 3: Create the MATLAB Inference Interface
Translate the Python inference example to MATLAB. Refer t>
Analyze AMS waveform data using Mixed-Signal Blockset utilities: phase noise measurement, clock jitter, anti-aliased resampling, timing measurements, lock time, INL/DNL, ADC/DAC calibration, HSpice import. Use when analyzing time-domain voltage from PLL/VCO/clock simulations, measuring phase noise from variable-step solver output, computing jitter, or resampling non-uniform data.
Analyze data using MATLAB. Use when the task involves tables, timetables, time-series data, numeric arrays, sensor matrices, or gridded data — including but not limited to exploring, filtering, sorting, cleaning, transforming, aggregating, smoothing, padding, trimming, and answering questions about data. MATLAB provides extensive, easy-to-use built-in functions for these workflows with no additional products required.
Analyze the effective toolbox file set to produce a Dependency Manifest — classify all transitive dependencies as included, product, add-on, or external-unresolved, then present resolution options with tradeoffs. Use after matlab-define-toolbox-api when the spec is approved.
S-parameters, insertion loss, fields, currents, mesh control, and solver selection for RF PCB performance validation. TRIGGER: user asks to compute S-parameters, analyze insertion/return loss, extract fields or currents, compare MoM vs FEM, or control mesh for any RF PCB component. Invoke BEFORE writing sparameters() or solver code — API is non-obvious. SKIP: designing or creating components (use the specific matlab-design-pcb-* skill), material/stackup setup only (use matlab-manage-pcb-material), optimization sweeps (use matlab-optimize-pcb-design), PDN/IR-drop analysis (use matlab-analyze-pcb-pdn).
Analyze antennas installed on electrically large conducting platforms using MATLAB Antenna Toolbox. Loads platform geometry from STL/STEP/IGES, installs antenna elements, selects electromagnetic solvers (MoM-PO, FMM, MoM), and computes patterns, impedance, coupling, and efficiency. Use when the user wants to model an antenna on a vehicle, aircraft, ship, satellite, or other large structure.
PDN DC voltage/current analysis, IR drop, design rule checking, and multi-net batch analysis on imported PCB layouts. TRIGGER: user asks about power integrity, PDN analysis, IR drop, voltage distribution, current density, power nets, or design rule checking on a PCB. Invoke BEFORE writing code — the PDN API chain is specialized and non-obvious. SKIP: importing a PCB file (use matlab-read-pcb-layout), EM field/S-parameter extraction (use matlab-analyze-em), material/stackup setup only (use matlab-manage-pcb-material), transmission line design (use matlab-design-pcb-txline).
Calculate and visualize monostatic and bistatic radar cross section (RCS) using MATLAB Antenna Toolbox. Computes RCS of platforms, antennas, and arrays with PO, MoM, and FMM solvers, supporting HH/VV/HV/VH polarization, GPU acceleration, and near-field observation. Use when the user wants to compute, plot, or analyze radar cross section.