Cutie for the browser (ONNX)
The released small Cutie model (cutie-small-mega.pth, ResNet-18 encoders; CVPR 2024), converted to ONNX so it runs in a web page with ONNX Runtime Web. It powers the live demo in the blog post Cutie: object queries that clean up pixel matching.
The pixel memory, the object memory, and the frame loop are not in these graphs; they live in model.js, a line-by-line port of cutie/inference/inference_core.py and memory_manager.py for one object, with the evaluation settings (memory every 5 frames, at most 5 memory frames, top-30 matching, no long-term memory).
Files
| file | contents |
|---|---|
image_encoder.onnx |
the query encoder (ResNet-18 to stride 16) and key projection: features at strides 16, 8, and 4, key, shrinkage, selection |
mask_encoder.onnx |
the mask encoder with its sensory update, and the object summarizer |
pixel_readout.onnx |
the anisotropic L2 affinity to the pixel memory and the top-30 readout; no weights |
object_transformer.onnx |
the pixel fuser (which gives the pixel readout R0), then the three object transformer blocks; also outputs the four auxiliary masks and each block's attention, averaged over heads |
decoder.onnx |
the mask decoder with its sensory update; outputs stride-4 logits |
*.f16.onnx |
the same graphs in half precision, for GPUs with shader-f16; inputs and outputs stay fp32 (pixel_readout has no half-precision version) |
weights.bin |
all weights in one file: large tensors as symmetric per-output-channel int8 with fp32 scales, the rest raw |
manifest.json |
where each tensor of weights.bin goes in each graph's external-data file |
The graphs keep their initializers in external data (<graph>.data, <graph>.f16.data), which are not shipped: the page rebuilds them from weights.bin and passes them to InferenceSession.create as externalData. That way the fp32 and fp16 graphs share one 28 MB download.
Changes from the released model
- The network is split into five graphs that share no weights, one object at a time. Feature maps pass between them on the GPU.
- Each forward is rewritten with exact equivalents: area interpolation by an integer factor becomes average pooling,
nn.MultiheadAttentionis written out, and the top-30 readout gathers the 30 memory values instead of multiplying by a sparse affinity. The top 30 are taken in two steps (within each memory frame, then overall), which selects the same entries and is four times faster on ONNX Runtime Web's WebGPU TopK. - Three steps moved out of the graphs into JS, in fp32, because half precision cannot hold them: the object memory's running sums (the summarizer outputs a weighted mean and the area instead of the sum), their division by the area, and the end of
CUTIE.segment(whose clamp to 1 β 1e-7 rounds to 1 in fp16). pixel_readoutalso outputs, for display, the memory frames' masks read with the same top-30 affinity: what pixel matching alone says.- Weights are int8. With them and the fp16 graphs on WebGPU, every frame's mask keeps an IoU above 0.97 with the full-precision PyTorch model's on the two demo clips and the judo example from the Cutie repository, and above 0.99 on most frames.
Use ONNX Runtime Web 1.30 or later: 1.22 has no WebGPU TopK, and its WebGPU ReduceMax, the obvious substitute, is wrong for long rows.
Citation
@inproceedings{cheng2024putting,
title={Putting the Object Back into Video Object Segmentation},
author={Cheng, Ho Kei and Oh, Seoung Wug and Price, Brian and Lee, Joon-Young and Schwing, Alexander},
booktitle={CVPR},
year={2024}
}
Reproducing
export/export.py (with export/load.py) splits the released checkpoint (cutie-small-mega.pth from the Cutie v1.0 GitHub release, placed in weights/ next to a clone of the Cutie repository in src/Cutie) into the five fp32 graphs, and export/package.py int8 <out> writes the files in this repository. export/loop.py runs the frame loop as model.js does, on the rewritten PyTorch modules or on the ONNX graphs, and compares the masks with the released InferenceCore. They need torch, onnx, onnxruntime, onnxconverter-common, hydra-core, and the Cutie repository.