Update README.md
Browse files
README.md
CHANGED
|
@@ -1,65 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
license: apple-amlr
|
| 3 |
-
license_name: apple-ascl
|
| 4 |
-
license_link: https://github.com/apple/ml-mobileclip/blob/main/LICENSE_weights_data
|
| 5 |
-
library_name: mobileclip
|
| 6 |
-
---
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
](https://arxiv.org/pdf/2311.17049.pdf) (CVPR 2024), by Pavan Kumar Anasosalu Vasu, Hadi Pouransari, Fartash Faghri, Raviteja Vemulapalli, Oncel Tuzel.
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|:----------------------------------------------------------|:----------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------------:|:----------------------------------:|
|
| 27 |
-
| [MobileCLIP-S0](https://hf.co/pcuenq/MobileCLIP-S0) | 13 | 11.4 + 42.4 | 1.5 + 1.6 | 67.8 | 58.1 |
|
| 28 |
-
| [MobileCLIP-S1](https://hf.co/pcuenq/MobileCLIP-S1) | 13 | 21.5 + 63.4 | 2.5 + 3.3 | 72.6 | 61.3 |
|
| 29 |
-
| [MobileCLIP-S2](https://hf.co/pcuenq/MobileCLIP-S2) | 13 | 35.7 + 63.4 | 3.6 + 3.3 | 74.4 | 63.7 |
|
| 30 |
-
| [MobileCLIP-B](https://hf.co/pcuenq/MobileCLIP-B) | 13 | 86.3 + 63.4 | 10.4 + 3.3 | 76.8 | 65.2 |
|
| 31 |
-
| [MobileCLIP-B (LT)](https://hf.co/pcuenq/MobileCLIP-B-LT) | 36 | 86.3 + 63.4 | 10.4 + 3.3 | 77.2 | 65.8 |
|
| 32 |
|
| 33 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
For programmatic downloading, if you have `huggingface_hub` installed, you can also run:
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
```
|
| 39 |
-
huggingface-cli download pcuenq/MobileCLIP-B
|
| 40 |
-
```
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
import torch
|
| 47 |
-
from PIL import Image
|
| 48 |
-
import mobileclip
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
image
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
```
|
|
|
|
|
|
|
|
|
| 1 |
+
````markdown
|
| 2 |
+
# 📸 MobileCLIP-B Zero-Shot Image Classifier — HF Inference Endpoint
|
| 3 |
+
|
| 4 |
+
This repository packages Apple’s **MobileCLIP-B** model as a production-ready
|
| 5 |
+
Hugging Face Inference Endpoint.
|
| 6 |
+
|
| 7 |
+
* **One-shot image → class probabilities**
|
| 8 |
+
⚡ < 30 ms on an A10G / T4 once the image arrives.
|
| 9 |
+
* **Branch-fused / FP16** MobileCLIP for fast GPU inference.
|
| 10 |
+
* **Pre-computed text embeddings** for your custom label set
|
| 11 |
+
(`items.json`) — every request encodes **only** the image.
|
| 12 |
+
* Built with vanilla **`open-clip-torch`** (no forks) and a
|
| 13 |
+
60-line local helper (`reparam.py`) to fuse MobileOne blocks.
|
| 14 |
+
|
| 15 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
## ✨ What’s inside
|
| 18 |
+
|
| 19 |
+
| File | Purpose |
|
| 20 |
+
|------|---------|
|
| 21 |
+
| `handler.py` | Hugging Face entry-point — loads weights, caches text features, serves requests |
|
| 22 |
+
| `reparam.py` | Stand-alone copy of `reparameterize_model` from Apple’s repo (removes heavy upstream dependency) |
|
| 23 |
+
| `requirements.txt` | Minimal, conflict-free dependency set (`torch`, `torchvision`, `open-clip-torch`) |
|
| 24 |
+
| `items.json` | Your label spec — each element must have `id`, `name`, and `prompt` fields |
|
| 25 |
+
| `README.md` | You are here |
|
| 26 |
|
| 27 |
+
---
|
|
|
|
| 28 |
|
| 29 |
+
## 🔧 Quick start (local smoke-test)
|
| 30 |
|
| 31 |
+
```bash
|
| 32 |
+
python -m venv venv && source venv/bin/activate
|
| 33 |
+
pip install -r requirements.txt
|
| 34 |
+
python - <<'PY'
|
| 35 |
+
from pathlib import Path, PurePosixPath
|
| 36 |
+
import base64, json, requests
|
| 37 |
|
| 38 |
+
# Load a demo image and encode it
|
| 39 |
+
img_path = Path("tests/cat.jpg")
|
| 40 |
+
payload = {
|
| 41 |
+
"image": base64.b64encode(img_path.read_bytes()).decode()
|
| 42 |
+
}
|
| 43 |
|
| 44 |
+
# Local simulation — spin up uvicorn the same way the HF container does
|
| 45 |
+
import handler, uvicorn
|
| 46 |
+
app = handler.EndpointHandler()
|
| 47 |
|
| 48 |
+
print(app({"inputs": payload})[:5]) # top-5 classes
|
| 49 |
+
PY
|
| 50 |
+
````
|
| 51 |
|
| 52 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
## 🚀 Calling the deployed endpoint
|
| 55 |
+
|
| 56 |
+
```bash
|
| 57 |
+
ENDPOINT_URL="https://<your-endpoint>.aws.endpoints.huggingface.cloud"
|
| 58 |
+
HF_TOKEN="hf_xxxxxxxxxxxxxxxxx"
|
| 59 |
+
IMG="cat.jpg"
|
| 60 |
+
|
| 61 |
+
python - <<'PY'
|
| 62 |
+
import base64, json, requests, sys, os
|
| 63 |
+
url = os.environ["ENDPOINT_URL"]
|
| 64 |
+
token = os.environ["HF_TOKEN"]
|
| 65 |
+
img = sys.argv[1]
|
| 66 |
+
|
| 67 |
+
payload = {
|
| 68 |
+
"inputs": {
|
| 69 |
+
"image": base64.b64encode(open(img, "rb").read()).decode()
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
resp = requests.post(
|
| 73 |
+
url,
|
| 74 |
+
headers={
|
| 75 |
+
"Authorization": f"Bearer {token}",
|
| 76 |
+
"Content-Type": "application/json",
|
| 77 |
+
"Accept": "application/json",
|
| 78 |
+
},
|
| 79 |
+
json=payload,
|
| 80 |
+
timeout=60,
|
| 81 |
+
)
|
| 82 |
+
print(json.dumps(resp.json()[:5], indent=2)) # top-5
|
| 83 |
+
PY
|
| 84 |
+
$IMG
|
| 85 |
+
```
|
| 86 |
|
| 87 |
+
Sample response:
|
|
|
|
| 88 |
|
| 89 |
+
```json
|
| 90 |
+
[
|
| 91 |
+
{ "id": 23, "label": "cat", "score": 0.92 },
|
| 92 |
+
{ "id": 11, "label": "tiger cat", "score": 0.05 },
|
| 93 |
+
{ "id": 48, "label": "siamese cat", "score": 0.02 },
|
| 94 |
+
…
|
| 95 |
+
]
|
| 96 |
```
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
## 🏗️ How the handler works (high-level)
|
| 101 |
+
|
| 102 |
+
1. **Startup**
|
| 103 |
+
|
| 104 |
+
* Downloads / loads the `datacompdr` MobileCLIP-B checkpoint.
|
| 105 |
+
* Runs `reparameterize_model` to fuse MobileOne branches.
|
| 106 |
+
* Reads `items.json`, tokenises all prompts, and caches the resulting
|
| 107 |
+
text embeddings (`[n_classes, 512]`).
|
| 108 |
|
| 109 |
+
2. **Per request**
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
* Decodes the incoming base-64 JPEG/PNG.
|
| 112 |
+
* Applies the exact OpenCLIP preprocessing (224 × 224 center-crop,
|
| 113 |
+
mean/std normalisation).
|
| 114 |
+
* Encodes the image, L2-normalises, and performs one `softmax(cosine)`
|
| 115 |
+
against the cached text matrix.
|
| 116 |
+
* Returns a sorted JSON list `[{"id", "label", "score"}, …]`.
|
| 117 |
|
| 118 |
+
This design keeps bandwidth low (compressed image over the wire) and
|
| 119 |
+
latency low (no per-request text encoding).
|
| 120 |
|
| 121 |
+
---
|
| 122 |
+
|
| 123 |
+
## 📝 Updating the label set
|
| 124 |
+
|
| 125 |
+
Edit `items.json`, **rebuild the endpoint**, done.
|
| 126 |
+
|
| 127 |
+
```json
|
| 128 |
+
[
|
| 129 |
+
{ "id": 0, "name": "cat", "prompt": "a photo of a cat" },
|
| 130 |
+
{ "id": 1, "name": "dog", "prompt": "a photo of a dog" },
|
| 131 |
+
…
|
| 132 |
+
]
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
* `id` is your internal numeric key (stays stable).
|
| 136 |
+
* `name` is the human-readable label returned to clients.
|
| 137 |
+
* `prompt` is what the model actually “sees” — tweak wording to improve accuracy.
|
| 138 |
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
## ⚖️ Licence
|
| 142 |
+
|
| 143 |
+
* **Weights**: Apple AMLR (see [`LICENSE_weights_data`](./LICENSE_weights_data)).
|
| 144 |
+
* **Code in this repo**: MIT.
|
| 145 |
|
| 146 |
+
---
|
| 147 |
+
|
| 148 |
+
<div align="center"><sub>Maintained with ❤️ by Your Team — August 2025</sub></div>
|
| 149 |
```
|
| 150 |
+
::contentReference[oaicite:0]{index=0}
|
| 151 |
+
|