Adding CoreML Export Support - Looking for Feedback
Before I make the pr, just want to make sure that my process makes sense:
Started by exploring how to handle EdgeTAM's dynamic prompts (points, boxes, masks) in CoreML. The main challenge was that CoreML prefers static input shapes, but EdgeTAM needs to handle variable numbers of points and different prompt types.
Initially tried exporting the entire model as one piece, but quickly realized that wouldn't work well for mobile - the prompt handling was too dynamic and CoreML's tracing couldn't handle the conditional logic properly.
Ended up splitting the model into 3 parts:
- Image encoder (~9.6MB) - handles the heavy lifting of processing the 1024x1024 input image
- Prompt encoder (~2MB) - processes user inputs with fixed slots (up to 4 points, 1 box, 1 mask)
- Mask decoder (~8MB) - generates the final masks
The tricky part was getting the prompt encoder right. Had to dig into the SAM2 code to understand how prompts are embedded, then create wrapper classes that make the dynamic parts static enough for CoreML.
Let me know if this makes sense! happy to create the pr soon :)
@DjKesu have you shared your coreml export anywhere, or have a working implementation wired up? Curious to try it!
@vade - feel free to try it out: https://github.com/facebookresearch/EdgeTAM/pull/18 or https://github.com/DjKesu/EdgeTAM
let me know if you encounter any issues!
Can you add coreml_models/inference_example.py to better demonstrate how to perform video tracking?
@Lightning-Lion just added an example!
@DjKesu Does the current version not support the memory bank mechanism?
@vade - feel free to try it out: #18 or https://github.com/DjKesu/EdgeTAM
let me know if you encounter any issues!
@DjKesu Love the work thanks. I have gotten a bit of an issue when trying to install it first I installed the packages with this pip install -e ".[coreml]" than I tried to run this python coreml/export_to_coreml.py \
--sam2_cfg sam2/configs/edgetam.yaml \
--sam2_checkpoint checkpoints/edgetam.pt
and got the following error:
Loading EdgeTAM model...
Exporting to ./coreml_models... Exporting Image Encoder... WARNING:coremltools:Tuple detected at graph output. This will be flattened in the converted model. Converting PyTorch Frontend ==> MIL Ops: 0%| | 0/987 [00:00<?, ? ops/s]ERROR:coremltools:
ERROR - converting 'int' op (located at: '2005'):
Converting PyTorch Frontend ==> MIL Ops: 97%|████████████████████████████████████████████████████▏ | 954/987 [00:00<00:00, 10150.68 ops/s] Export failed: only 0-dimensional arrays can be converted to Python scalars
Thanks in advance
@vade - feel free to try it out: #18 or https://github.com/DjKesu/EdgeTAM let me know if you encounter any issues!
@DjKesu Love the work thanks. I have gotten a bit of an issue when trying to install it first I installed the packages with this pip install -e ".[coreml]" than I tried to run this python coreml/export_to_coreml.py \ --sam2_cfg sam2/configs/edgetam.yaml \ --sam2_checkpoint checkpoints/edgetam.pt
and got the following error:
Loading EdgeTAM model...
Exporting to ./coreml_models... Exporting Image Encoder... WARNING:coremltools:Tuple detected at graph output. This will be flattened in the converted model. Converting PyTorch Frontend ==> MIL Ops: 0%| | 0/987 [00:00<?, ? ops/s]ERROR:coremltools:
ERROR - converting 'int' op (located at: '2005'):
Converting PyTorch Frontend ==> MIL Ops: 97%|████████████████████████████████████████████████████▏ | 954/987 [00:00<00:00, 10150.68 ops/s] Export failed: only 0-dimensional arrays can be converted to Python scalars
Thanks in advance
I am also facing same issue
@DjKesu Does the current version not support the memory bank mechanism?
Following up on this question. It seems that the current version only handles the prompting and segmentation but not the tracking using the memory bank.
I tried to reexport by passing the mask_input instead of None but it doesn't seem to work.
@AlexMorgand @vade @DjKesu @Lightning-Lion @yinnengzhong I've been working on this problem for a while — specifically getting the full video tracking pipeline (including memory attention) running on iOS via CoreML + Metal.
The core issue, as @AlexMorgand and @yinnengzhong pointed out, is that the memory attention module has dynamic shapes and cross-attention patterns that coremltools can't export cleanly. My approach was to keep 5 modules as CoreML models on ANE (image encoder, prompt encoder, mask decoder, memory encoder, spatial perceiver) and implement the memory attention as custom Metal shaders — RoPE encoding, layer norm, cross-attention via MPS matrix ops, with a ring-buffer memory bank (7 frames, matching edgetam.yaml's num_maskmem=7).
I've packaged this into an iOS framework called SegmentKit:
- GitHub: https://github.com/chenweisomebody126/SegmentKit
- Docs: https://segmentkit.dev
The trickiest parts were: (1) the RoPE frequency repetition for cross-attention keys across variable frame counts, (2) getting the obj_ptr from mask decoder v3 to feed back into the memory bank correctly, and (3) zero-copy buffer management between CoreML and Metal to avoid per-frame allocations.
Happy to discuss the technical details if anyone's working on something similar.
Awesome! As a temporary fix on my app I was computing the gravity center from contours and rerunning the segmentation as a click.
Let me give a try to your work @chenweisomebody126 and thank you for your contribution!