Add your own model

Your model, as a function.

A PyTorch network becomes a function an omxcode script calls. It runs inside the app, on the GPU, with no server beside it. One command converts it and checks it against PyTorch. You write no Mojo.

Step 0

Set up. One command after the clone.

scripts/setup does everything a newcomer used to do by hand:

  • env: Mojo, MAX and the tools, through pixi. A half-installed environment is cleaned and installed again on its own.
  • build: omxplorer, the showcase app. The first build takes a few minutes.
  • data: a slice of DROID, real robot episodes (CC-BY-4.0), fetched from the Hugging Face Hub and cut into clips with their telemetry.
  • packs: the three models that come with omxcode, each built on your machine from its maker's weights.

Run it again any time: a step that's done is skipped, so a second run takes seconds.

a terminal
git clone https://github.com/abhisheksreesaila/omxcode
cd omxcode
scripts/setup                # env, build, data, packs
scripts/setup --no-models    # everything but the packs

You need Linux on x86-64 with GTK 4 and libadwaita 1.7+, pixi, and an NVIDIA GPU for the packs. On a Mac, setup does the rest and says why it skips the packs.

Step 1

Convert it. Checked against PyTorch before it's written.

scripts/packs/from_torch.py reads your network's layers, rebuilds them as a MAX graph, compiles it for your GPU, and runs both on the test pictures. If MAX's answers differ from PyTorch's, nothing is written.

A torchvision model is one word: torchvision:resnet18 takes ResNet-18 with its default weights. ResNet-18 is how scene(), the third model that comes with omxcode, was made.

Your own network is a Python file and a function that returns it: my_net.py:build. The converter traces it with torch.fx, so ordinary forward code works: residual adds, joined channels, activations as modules or as calls.

--function is what scripts will call it. The labels come from torchvision, from a LABELS list in your file, or from --labels file.txt.

a terminal
pixi run python scripts/packs/from_torch.py \
    torchvision:resnet18 build/packs/scene --function scene
traced torchvision:resnet18: 51 steps, 42 weight arrays, 5 reference pictures
   scene: MAX matches torch on 5/5 pictures (top label), logits within 0.0351; 1000 labels
tests/fixtures/models/tiny_cnn.py · a network written from scratch
def forward(self, x):
    x = self.pool(self.stem(x))
    x = x + self.a(x)                                    # a residual add
    x = torch.cat([self.b(x), self.c(x)], dim=1)         # channels joined
    x = torch.sigmoid(x) * x                             # mul, a function call
    x = self.squash(x)                                   # 4 × 4 left: flatten's order matters
    return self.head(self.drop(torch.flatten(x, 1)))

Converted with from_torch.py tests/fixtures/models/tiny_cnn.py:tiny …: MAX matches PyTorch on all 5 test pictures, its outputs within 0.00000004.

Step 2

The pack describes itself. That is the whole registration.

A pack is a folder: the compiled graph (scene.mef), its weights (scene.safetensors), labels.txt, and plugin.json (with the older model.json beside it), which the converter writes.

The framework reads every plugin.json in the plugins folders ($OMX_PLUGINS, else ~/.local/share/omxcode/plugins) and the packs folder (build/packs, or $OMX_PACKS): mojoui/plugins.mojo. A folder there is a function. Take the folder away and the function is gone. No list to edit, no Mojo to rebuild.

For an image-labels pack, the framework itself crops each frame to its centre square, resizes it to the pack's size, and reads back the best label, the same way the converter's check did. Mojo and PyTorch pick the same label on every test picture.

build/packs/scene/model.json · written by from_torch.py
{
 "function": "scene",
 "kind": "image-labels",
 "about": "torchvision:resnet18 (from_torch.py)",
 "input": {
  "size": 224,
  "fit": "centre-square",
  "pixels": "RGB 0-255 NHWC float32"
 },
 "labels": "labels.txt",
 "model": "torchvision:resnet18"
}
mojoui/imagemodel.mojo · the same pack from your own app
#   var m = ImageModel("build/packs/scene")        # (loaded once per process: mojoui.maxrt.shared_model)
#   var top = m.top_rgba(frame, w, h)              # a video frame (one UInt32 a pixel, RGBA in memory) → Guess
#   m.labels[top.label], top.score                 # its best label and how sure (softmax, 0–1)
#   var logits = m.logits_rgba(frame, w, h)        # every label's raw output, in labels.txt's order
Step 3

Call it. From a script, a button or an agent.

In omxplorer, open a video and the Script window. The new model is already in the Apply row, beside detect. Apply it to a few seconds, or write the call yourself (a video is a table named after its file, f00118_mp4):

Each frame becomes a row: label, score (how sure, 0 to 1), frame and t (seconds). The result lands as a table card, so the next line of the script can query it, join it to the telemetry or chart it.

Agents get it too: omxplorer's agent host offers an apply-scene tool as soon as the pack is there.

omxplorer · the Script window
labels = scene(f00118_mp4, 3, 8);
per_label = SELECT label, COUNT(*) AS frames FROM labels GROUP BY label;

The robot's camera isn't what ImageNet was trained on, so ResNet-18's labels for it are guesses: it calls these frames "projectile". That's fine: this page is about plugging a model in, and your own model will know your own footage.

The whole flow in 22 seconds, recorded from the app (silent)
omxplorer in dark mode: the robot fleet table, the featured episode's video, and a new table card from scene() with a label and score for every frame between 3 and 8 seconds
omxplorer on Linux: scene() applied to seconds 3–8 of episode F00118, shot by the app's test mode
Or

Add a model from the app. Paste a name, drop a file, or ask the agent.

omxplorer's Add a model takes a Hugging Face name, a model file (XGBoost JSON, LightGBM text, ONNX, skops) or a folder. Before anything downloads it says, in one line, what the model does, whether it fits the GPU, and its licence; a model it can't run says why, and nothing downloads.

It answers on the row or the video frame you were looking at as soon as it lands, then Run it on all fills a column or a table. A table's columns are matched to the model's inputs by name and type, never guessed. A model that runs its own Python code asks first. Pickles are refused. The agent's add-model tool does the same (mojoui/addmodel.mojo).

Learn from labels is the other way in: label about twenty rows of any table and a decision tree, written in Mojo, fills the rest and says its rule in words (mojoui/tree.mojo, mojoui/learn.mojo).

a plugin.json · what Add a model writes for a Hub detector
{
  "format": "omx-plugin/1",
  "name": "yolos_tiny",
  "kind": "detect",
  "runner": "torch",
  "licence": "apache-2.0",
  "source": "hf:hustvl/yolos-tiny@…",
  "inputs": [{"name": "frame", "type": "image"}],
  "outputs": [{"name": "kind", "type": "text"}, …],
  "files": {"model": "model"},
  "trust_remote_code": false
}
Limits

What it can't do yet.

Layers

Convolutional networks

It converts Conv2d (groups 1), BatchNorm2d, Linear, the common activations, max and average pooling, add, mul, cat and flatten. Anything else stops it with the layer's name, for example an Upsample. Then that part's MAX graph is written by hand, as scripts/packs/yolox.py does.

Kinds

Five kinds, by plugin.json

A plugin says its kind: tabular, detect, segment, classify-image or embed-text, and its runner: a MAX pack, a decision tree file read in Mojo, or PyTorch, ONNX or skops in the app's process (mojoui/models.mojo names no model). Segmenters from the Hub don't run yet; a MAX detector decodes YOLOX's outputs only.

Where

Linux with an NVIDIA GPU

Packs are compiled by MAX for the GPU in the machine, on Linux. The converter checks every pack on that GPU before writing it. Macs run omxplorer and its video, but not packs yet.