Instructions to use zeromodels/levit-128S with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- ZeroModels
How to use zeromodels/levit-128S with ZeroModels:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Keras
How to use zeromodels/levit-128S with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://zeromodels/levit-128S") - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files- README.md +85 -0
- model.weights.h5 +3 -0
- zm_config.json +50 -0
README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pipeline_tag: image-classification
|
| 3 |
+
license: apache-2.0
|
| 4 |
+
base_model: facebook/levit-128S
|
| 5 |
+
library_name: zeromodels
|
| 6 |
+
tags:
|
| 7 |
+
- keras
|
| 8 |
+
- zeromodels
|
| 9 |
+
- image-classification
|
| 10 |
+
- vit
|
| 11 |
+
- backbone
|
| 12 |
+
- levit
|
| 13 |
+
- arxiv:2104.01136
|
| 14 |
+
- pytorch
|
| 15 |
+
- jax
|
| 16 |
+
- tf
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## ***See [our collection](https://huggingface.co/collections/zeromodels/levit-6a937f8760837c24b7a51d25) for all versions of LeViT.***
|
| 20 |
+
|
| 21 |
+
# Run LeViT with Keras 3: JAX, PyTorch, or TensorFlow
|
| 22 |
+
|
| 23 |
+
[](https://github.com/IMvision12/ZeroModels) [](https://imvision12.github.io/ZeroModels/classification_backbones/) [](https://huggingface.co/collections/zeromodels/levit-6a937f8760837c24b7a51d25)
|
| 24 |
+
|
| 25 |
+
# zeromodels/levit-128S
|
| 26 |
+
|
| 27 |
+
Paper: [LeViT: a Vision Transformer in ConvNet's Clothing for Faster Inference (arXiv:2104.01136)](https://arxiv.org/abs/2104.01136) · [HF Papers](https://huggingface.co/papers/2104.01136)
|
| 28 |
+
|
| 29 |
+
LeViT is a hybrid convolution/transformer image classifier built for fast inference: a four-layer conv stem downsamples the image 16x, then three attention stages (each adding a learnable 2D relative-position bias) run over the tokens, with a BatchNorm fused into every linear layer and Hardswish activations. The released checkpoints are distilled - a second classification head is averaged with the first at inference. The smallest LeViT (hidden sizes 128/256/384, depths 2/3/4), tuned for the fastest inference.
|
| 30 |
+
|
| 31 |
+
For more details on the model, please go to Meta's original [model card](https://huggingface.co/facebook/levit-128S).
|
| 32 |
+
|
| 33 |
+
Pure-**Keras 3** conversion of [`facebook/levit-128S`](https://huggingface.co/facebook/levit-128S) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.
|
| 34 |
+
|
| 35 |
+
## ✨ Quick start
|
| 36 |
+
|
| 37 |
+
```python
|
| 38 |
+
import os
|
| 39 |
+
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
|
| 40 |
+
|
| 41 |
+
import keras
|
| 42 |
+
import numpy as np
|
| 43 |
+
from PIL import Image
|
| 44 |
+
from zeromodels.models.levit import LevitImageClassify
|
| 45 |
+
|
| 46 |
+
model = LevitImageClassify.from_weights("zeromodels/levit-128S")
|
| 47 |
+
|
| 48 |
+
# LeViT preprocessing: resize the shortest edge to 256, then center-crop 224.
|
| 49 |
+
image = Image.open("your_image.jpg").convert("RGB")
|
| 50 |
+
w, h = image.size
|
| 51 |
+
short = 256
|
| 52 |
+
image = image.resize((round(short * w / h), short) if h <= w else (short, round(short * h / w)))
|
| 53 |
+
w, h = image.size
|
| 54 |
+
left, top = (w - 224) // 2, (h - 224) // 2
|
| 55 |
+
image = image.crop((left, top, left + 224, top + 224))
|
| 56 |
+
|
| 57 |
+
pixels = np.asarray(image, "float32")[None] # raw [0, 255]; normalization is inside the model
|
| 58 |
+
logits = model(pixels, training=False)
|
| 59 |
+
print("top-1 ImageNet class id:", int(keras.ops.convert_to_numpy(logits)[0].argmax()))
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
Load any LeViT variant the same way with `from_weights("zeromodels/<variant>")`:
|
| 63 |
+
|
| 64 |
+
| Variant | Hub |
|
| 65 |
+
|---|---|
|
| 66 |
+
| `levit-128S` | [`zeromodels/levit-128S`](https://huggingface.co/zeromodels/levit-128S) |
|
| 67 |
+
| `levit-128` | [`zeromodels/levit-128`](https://huggingface.co/zeromodels/levit-128) |
|
| 68 |
+
| `levit-192` | [`zeromodels/levit-192`](https://huggingface.co/zeromodels/levit-192) |
|
| 69 |
+
| `levit-256` | [`zeromodels/levit-256`](https://huggingface.co/zeromodels/levit-256) |
|
| 70 |
+
| `levit-384` | [`zeromodels/levit-384`](https://huggingface.co/zeromodels/levit-384) |
|
| 71 |
+
|
| 72 |
+
## Tips
|
| 73 |
+
|
| 74 |
+
- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
|
| 75 |
+
- ImageNet normalization is baked into the model, so pass raw `[0, 255]` pixels.
|
| 76 |
+
- Preprocess by resizing the shortest edge to 256 and center-cropping 224 (shown above) to match the reference; a plain `resize((224, 224))` is close and also works.
|
| 77 |
+
- `LevitImageClassify` averages the two distillation heads internally; `LevitModel.from_weights(...)` gives the backbone (the final token sequence, no head).
|
| 78 |
+
- See [Classification backbones](https://imvision12.github.io/ZeroModels/classification_backbones/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
|
| 79 |
+
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `LevitImageClassify.from_weights("hf:facebook/levit-128S")`.
|
| 80 |
+
|
| 81 |
+
## Special Thanks
|
| 82 |
+
|
| 83 |
+
A huge thank you to the Meta AI LeViT authors for creating and releasing these models.
|
| 84 |
+
|
| 85 |
+
License: Apache 2.0.
|
model.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:270469c7b9af634f3764cf2e768f3389ccdad9346fd92a88078a5159fd54015f
|
| 3 |
+
size 31776288
|
zm_config.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"library_name": "zeromodels",
|
| 3 |
+
"zeromodels_version": "1.2.7",
|
| 4 |
+
"model_module": "zeromodels.models.levit",
|
| 5 |
+
"model_class": "LevitImageClassify",
|
| 6 |
+
"variant": "levit-128S",
|
| 7 |
+
"weights": "model.weights.h5",
|
| 8 |
+
"schema_version": 2,
|
| 9 |
+
"model_type": "levit",
|
| 10 |
+
"vision_config": {
|
| 11 |
+
"image_size": 224,
|
| 12 |
+
"num_channels": 3,
|
| 13 |
+
"kernel_size": 3,
|
| 14 |
+
"stride": 2,
|
| 15 |
+
"padding": 1,
|
| 16 |
+
"patch_size": 16,
|
| 17 |
+
"hidden_sizes": [
|
| 18 |
+
128,
|
| 19 |
+
256,
|
| 20 |
+
384
|
| 21 |
+
],
|
| 22 |
+
"num_attention_heads": [
|
| 23 |
+
4,
|
| 24 |
+
6,
|
| 25 |
+
8
|
| 26 |
+
],
|
| 27 |
+
"depths": [
|
| 28 |
+
2,
|
| 29 |
+
3,
|
| 30 |
+
4
|
| 31 |
+
],
|
| 32 |
+
"key_dim": [
|
| 33 |
+
16,
|
| 34 |
+
16,
|
| 35 |
+
16
|
| 36 |
+
],
|
| 37 |
+
"mlp_ratio": [
|
| 38 |
+
2,
|
| 39 |
+
2,
|
| 40 |
+
2
|
| 41 |
+
],
|
| 42 |
+
"attention_ratio": [
|
| 43 |
+
2,
|
| 44 |
+
2,
|
| 45 |
+
2
|
| 46 |
+
],
|
| 47 |
+
"num_classes": 1000,
|
| 48 |
+
"use_distillation": true
|
| 49 |
+
}
|
| 50 |
+
}
|