levit-256 / README.md
IMvision12's picture
Upload README.md with huggingface_hub
555bcc8 verified
|
Raw
History Blame Contribute Delete
4.15 kB
---
pipeline_tag: image-classification
license: apache-2.0
base_model: facebook/levit-256
library_name: zeromodels
tags:
- keras
- zeromodels
- image-classification
- vit
- backbone
- levit
- arxiv:2104.01136
- pytorch
- jax
- tf
---
## ***See [our collection](https://huggingface.co/collections/zeromodels/levit-6a937f8760837c24b7a51d25) for all versions of LeViT.***
# Run LeViT with Keras 3: JAX, PyTorch, or TensorFlow
[![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-black?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-Backbones-blue)](https://imvision12.github.io/ZeroModels/classification_backbones/) [![Collection](https://img.shields.io/badge/HF-LeViT%20collection-yellow)](https://huggingface.co/collections/zeromodels/levit-6a937f8760837c24b7a51d25)
# zeromodels/levit-256
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)
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. Larger LeViT (hidden sizes 256/384/512).
For more details on the model, please go to Meta's original [model card](https://huggingface.co/facebook/levit-256).
Pure-**Keras 3** conversion of [`facebook/levit-256`](https://huggingface.co/facebook/levit-256) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.
## ✨ Quick start
```python
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
from PIL import Image
from zeromodels.models.levit import LevitImageClassify, LevitModel, LevitImageProcessor
model = LevitImageClassify.from_weights("zeromodels/levit-256")
processor = LevitImageProcessor.from_weights("zeromodels/levit-256")
image = Image.open("your_image.jpg").convert("RGB")
pixels = processor(image) # resize + normalize (normalization lives in the processor)
logits = model(pixels, training=False)
print(logits.shape) # (1, num_classes)
# Feature extraction: the backbone without the classifier head
backbone = LevitModel.from_weights("zeromodels/levit-256")
features = backbone(pixels, training=False)
```
Load any LeViT variant the same way with `from_weights("zeromodels/<variant>")`:
| Variant | Hub |
|---|---|
| `levit-128S` | [`zeromodels/levit-128S`](https://huggingface.co/zeromodels/levit-128S) |
| `levit-128` | [`zeromodels/levit-128`](https://huggingface.co/zeromodels/levit-128) |
| `levit-192` | [`zeromodels/levit-192`](https://huggingface.co/zeromodels/levit-192) |
| `levit-256` | [`zeromodels/levit-256`](https://huggingface.co/zeromodels/levit-256) |
| `levit-384` | [`zeromodels/levit-384`](https://huggingface.co/zeromodels/levit-384) |
## Tips
- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
- ImageNet normalization is baked into the model, so pass raw `[0, 255]` pixels.
- 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.
- `LevitImageClassify` averages the two distillation heads internally; `LevitModel.from_weights(...)` gives the backbone (the final token sequence, no head).
- See [Classification backbones](https://imvision12.github.io/ZeroModels/classification_backbones/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `LevitImageClassify.from_weights("hf:facebook/levit-256")`.
## Special Thanks
A huge thank you to the Meta AI LeViT authors for creating and releasing these models.
License: Apache 2.0.