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|
|
|
| 1 |
+
# MixVPR β ONNX & CoreML Export
|
| 2 |
+
|
| 3 |
+
Exported and quantized models for [MixVPR](https://github.com/Vincentqyw/MixVPR): Feature Mixing for Visual Place Recognition (WACV 2023).
|
| 4 |
+
|
| 5 |
+
## Models
|
| 6 |
+
|
| 7 |
+
| Format | File | Size | Latency (M-series) | CosSim | Status |
|
| 8 |
+
|---|---|---|---|---|---|
|
| 9 |
+
| ONNX FP32 | `onnx/mixvpr_fp32.onnx` | 41.7 MB | 32.4 ms | 1.0000 | β |
|
| 10 |
+
| ONNX FP16 | `onnx/mixvpr_fp16.onnx` | 21.0 MB | 38.2 ms | 0.9999 | β Recommended |
|
| 11 |
+
| CoreML FP16 | `coreml/mixvpr_fp16.mlpackage/` | 20.8 MB | **3.1 ms** | 0.9999 | β Recommended |
|
| 12 |
+
| CoreML INT8 | `coreml/mixvpr_int8.mlpackage/` | 10.5 MB | **3.3 ms** | 0.9983 | β |
|
| 13 |
+
|
| 14 |
+
## Benchmark
|
| 15 |
+
|
| 16 |
+
All models were benchmarked against the original PyTorch FP32 model on a MacBook with Apple Silicon (M-series). The primary accuracy metric is **cosine similarity** between the exported model's 4096-dim descriptor and the PyTorch reference.
|
| 17 |
+
|
| 18 |
+
### Latency (batch=1, averaged over 4 images)
|
| 19 |
+
|
| 20 |
+
| Model | Latency | Speedup |
|
| 21 |
+
|---|---|---|
|
| 22 |
+
| PyTorch FP32 (reference) | 44.5 ms | 1.0Γ |
|
| 23 |
+
| ONNX FP32 | 32.4 ms | 1.4Γ |
|
| 24 |
+
| ONNX FP16 | 38.2 ms | 1.2Γ |
|
| 25 |
+
| **CoreML FP16** | **3.1 ms** | **14.6Γ** |
|
| 26 |
+
| **CoreML INT8** | **3.3 ms** | **13.5Γ** |
|
| 27 |
+
|
| 28 |
+
### Accuracy (cosine similarity vs PyTorch)
|
| 29 |
+
|
| 30 |
+
| Model | CosSim | Max Abs Error | Verdict |
|
| 31 |
+
|---|---|---|---|
|
| 32 |
+
| ONNX FP32 | 1.0000 | 1.9Γ10β»β· | No drop |
|
| 33 |
+
| ONNX FP16 | 0.9999 | 1.7Γ10β»β΄ | No drop |
|
| 34 |
+
| CoreML FP16 | 0.9999 | 3.2Γ10β»β΄ | No drop |
|
| 35 |
+
| CoreML INT8 | 0.9983 | 3.5Γ10β»Β³ | Negligible |
|
| 36 |
+
|
| 37 |
+
- CosSim > 0.9999 means retrieval results are **identical** to PyTorch.
|
| 38 |
+
- CosSim 0.9983 means top-1 may shift for borderline cases; top-10 remains stable.
|
| 39 |
+
|
| 40 |
+
### File Size
|
| 41 |
+
|
| 42 |
+
| Model | Size | vs PyTorch (.pth) |
|
| 43 |
+
|---|---|---|
|
| 44 |
+
| PyTorch .pth | ~98 MB | β |
|
| 45 |
+
| ONNX FP32 | 41.7 MB | -57% |
|
| 46 |
+
| ONNX FP16 | 21.0 MB | -79% |
|
| 47 |
+
| CoreML FP16 | 20.8 MB | -79% |
|
| 48 |
+
| CoreML INT8 | 10.5 MB | -89% |
|
| 49 |
+
|
| 50 |
+
## Model Architecture
|
| 51 |
+
|
| 52 |
+
```
|
| 53 |
+
Input: (1, 3, 320, 320) normalized RGB image
|
| 54 |
+
β
|
| 55 |
+
βΌ
|
| 56 |
+
ResNet50 backbone (layer4 cropped) β (1, 1024, 20, 20)
|
| 57 |
+
β
|
| 58 |
+
βΌ
|
| 59 |
+
MixVPR aggregator:
|
| 60 |
+
- 4Γ FeatureMixerLayer (LayerNorm β Linear β ReLU β Linear, residual)
|
| 61 |
+
- Channel projection (Linear: 1024 β 1024)
|
| 62 |
+
- Row projection (Linear: 400 β 4)
|
| 63 |
+
- Flatten + L2 normalize β (1, 4096)
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
Parameters: **10.88 M**
|
| 67 |
+
|
| 68 |
+
## Usage
|
| 69 |
+
|
| 70 |
+
### Download
|
| 71 |
+
|
| 72 |
+
```bash
|
| 73 |
+
# All MixVPR models
|
| 74 |
+
huggingface-cli download Realcat/image_retrieval_checkpoints mixvpr/ --local-dir .
|
| 75 |
+
|
| 76 |
+
# Single file
|
| 77 |
+
huggingface-cli download Realcat/image_retrieval_checkpoints mixvpr/onnx/mixvpr_fp16.onnx
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### Install Dependencies
|
| 81 |
+
|
| 82 |
+
```bash
|
| 83 |
+
pip install onnxruntime # for ONNX
|
| 84 |
+
pip install coremltools # for CoreML (macOS only)
|
| 85 |
+
pip install torch torchvision Pillow numpy # for preprocessing
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
### ONNX Inference
|
| 89 |
+
|
| 90 |
+
```python
|
| 91 |
+
import onnxruntime as ort
|
| 92 |
+
import numpy as np
|
| 93 |
+
from PIL import Image
|
| 94 |
+
import torchvision.transforms as tvf
|
| 95 |
+
|
| 96 |
+
sess = ort.InferenceSession("mixvpr/onnx/mixvpr_fp16.onnx",
|
| 97 |
+
providers=['CPUExecutionProvider'])
|
| 98 |
+
|
| 99 |
+
# Images must be resized to 320Γ320
|
| 100 |
+
preprocess = tvf.Compose([
|
| 101 |
+
tvf.Resize((320, 320), interpolation=tvf.InterpolationMode.BICUBIC),
|
| 102 |
+
tvf.ToTensor(),
|
| 103 |
+
tvf.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 104 |
+
])
|
| 105 |
+
|
| 106 |
+
def extract_descriptor(image_path):
|
| 107 |
+
img = Image.open(image_path).convert("RGB")
|
| 108 |
+
tensor = preprocess(img).unsqueeze(0).numpy().astype(np.float32)
|
| 109 |
+
desc = sess.run(None, {'images': tensor})[0]
|
| 110 |
+
return desc # (1, 4096), L2-normalized
|
| 111 |
+
|
| 112 |
+
# Compare two images via cosine similarity
|
| 113 |
+
d1 = extract_descriptor("query.jpg")
|
| 114 |
+
d2 = extract_descriptor("reference.jpg")
|
| 115 |
+
similarity = np.dot(d1.flatten(), d2.flatten())
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
### CoreML Inference (Apple Silicon, ~15Γ faster)
|
| 119 |
+
|
| 120 |
+
```python
|
| 121 |
+
import coremltools as ct
|
| 122 |
+
import numpy as np
|
| 123 |
+
from PIL import Image
|
| 124 |
+
import torchvision.transforms as tvf
|
| 125 |
+
|
| 126 |
+
mlmodel = ct.models.MLModel("mixvpr/coreml/mixvpr_fp16.mlpackage")
|
| 127 |
+
|
| 128 |
+
preprocess = tvf.Compose([
|
| 129 |
+
tvf.Resize((320, 320), interpolation=tvf.InterpolationMode.BICUBIC),
|
| 130 |
+
tvf.ToTensor(),
|
| 131 |
+
tvf.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 132 |
+
])
|
| 133 |
+
|
| 134 |
+
def extract_descriptor(image_path):
|
| 135 |
+
img = Image.open(image_path).convert("RGB")
|
| 136 |
+
tensor = preprocess(img).unsqueeze(0).numpy().astype(np.float32)
|
| 137 |
+
desc = mlmodel.predict({'images': tensor})['descriptor']
|
| 138 |
+
return desc # (1, 4096), L2-normalized, runs on ANE + GPU
|
| 139 |
+
```
|
| 140 |
+
|
| 141 |
+
## Notes
|
| 142 |
+
|
| 143 |
+
- **Input**: 320Γ320 RGB images, normalized with ImageNet stats.
|
| 144 |
+
- **Output**: 4096-dim L2-normalized global descriptor. Use cosine similarity for retrieval.
|
| 145 |
+
- **ONNX INT8/INT4**: Quantized models exist but the ONNX Runtime CPU EP lacks `ConvInteger` kernels for this architecture. Use a GPU EP (CUDA/TensorRT) or switch to CoreML for quantized inference.
|
| 146 |
+
- **Re-export**: Scripts available in the [source repo](https://github.com/Vincentqyw/MixVPR) (`export_quant_onnx.py`, `export_coreml.py`).
|
| 147 |
+
|
| 148 |
+
## Reference
|
| 149 |
+
|
| 150 |
+
```bibtex
|
| 151 |
+
@inproceedings{ali2023mixvpr,
|
| 152 |
+
title={{MixVPR}: Feature Mixing for Visual Place Recognition},
|
| 153 |
+
author={Ali-bey, Amar and Chaib-draa, Brahim and Gigu{\`e}re, Philippe},
|
| 154 |
+
booktitle={Proceedings of the IEEE/CVF Winter Conference on Applications of Computer Vision},
|
| 155 |
+
pages={2998--3007},
|
| 156 |
+
year={2023}
|
| 157 |
+
}
|
| 158 |
+
```
|