Skip to main content
ClaudeWave
Skill996 repo starsupdated 9d ago

matlab-integrate-pytorch-vision

>-

Install in Claude Code
Copy
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-vision
Then start a new Claude Code session; the skill loads automatically.

SKILL.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
matlab-train-networkSkill

>

matlab-driving-data-importerSkill

Import recorded driving sensor data (GPS, camera, lidar, actor tracks, lanes) into scenariobuilder.* objects (GPSData, CameraData, LidarData, ActorTrackData, Trajectory, laneData) and run preprocessing — synchronize, offset correction, crop, normalizeTimestamps, convertTimestamps. Also: compute actor tracks from lidar when no annotations exist, attach camera/lidar mounting + intrinsics, export to MAT/workspace/timetable/script. Use for raw driving dataset files (KITTI, nuScenes, Waymo, Pandaset, ROS/ROS2 bags, .mat, .csv, .mp4) or driving/vehicle/sensor logs that need wrapping. drivingLogAnalyzer (DLA) is OPT-IN ONLY — invoke only on explicit user request ('DLA', 'open in DLA', 'inspect/explore/analyze the recording') or reported sensor problem (sync drift, timestamp mismatch, overlay misalignment). NEVER auto-launch DLA after wrapping (Rule 0). For 'build scenario / export to RoadRunner / drivingScenario / OpenSCENARIO / Unreal / simulate', hand off to matlab-scenario-builder.

matlab-scenario-builderSkill

Generate driving scenes, scenarios, road surfaces, and 3D content from already-wrapped scenariobuilder.* sensor data (GPS, camera, lidar, actor tracks) using Scenario Builder for Automated Driving Toolbox. Use to BUILD, EXPORT, or AUGMENT a virtual scenario/scene/map: ego or actor trajectories, trajectory smoothing, OpenCRG road-surface extraction, 3D asset generation, static-object placement, point-cloud georeferencing + elevation, lane-based ego localization, sensor-fusion tracking, scenario-event extraction (cut-ins, hard brakes, near-misses, ADAS disengagements), or export to RoadRunner, drivingScenario, OpenDRIVE, OpenCRG, OpenSCENARIO, or Unreal Engine. Also: log-to-scenario, scenario harvesting, accident/near-miss reconstruction, SOTIF (ISO 21448) and ISO 26262 scenario coverage, USGS-aerial-lidar scene augmentation, traffic-sign placement from camera+lidar logs. NOT for raw-data import or multi-sensor sync/crop/offset/timestamp normalization — route those to matlab-driving-data-importer.

roadrunner-asset-mappingSkill

>

roadrunner-convert-lanelet2-to-rrhdSkill

>

roadrunner-import-sceneSkill

>

roadrunner-rrhd-authoringSkill

>

matlab-build-simbiology-modelSkill

Build, modify, and diagram SimBiology models — API reference, helper functions, and layout patterns. Use when constructing or editing models programmatically or visually.