Instructions to use zeromodels/levit-128 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use zeromodels/levit-128 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-128") - Notebooks
- Google Colab
- Kaggle
See our collection for all versions of LeViT.
Run LeViT with Keras 3: JAX, PyTorch, or TensorFlow
zeromodels/levit-128
Paper: LeViT: a Vision Transformer in ConvNet's Clothing for Faster Inference (arXiv:2104.01136) · HF Papers
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. Base LeViT (hidden sizes 128/256/384, depths 4/4/4).
For more details on the model, please go to Meta's original model card.
Pure-Keras 3 conversion of facebook/levit-128 for zeromodels. One implementation runs unmodified on TensorFlow / Torch / JAX.
✨ Quick start
import os
os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
import keras
import numpy as np
from PIL import Image
from zeromodels.models.levit import LevitImageClassify
model = LevitImageClassify.from_weights("zeromodels/levit-128")
# LeViT preprocessing: resize the shortest edge to 256, then center-crop 224.
image = Image.open("your_image.jpg").convert("RGB")
w, h = image.size
short = 256
image = image.resize((round(short * w / h), short) if h <= w else (short, round(short * h / w)))
w, h = image.size
left, top = (w - 224) // 2, (h - 224) // 2
image = image.crop((left, top, left + 224, top + 224))
pixels = np.asarray(image, "float32")[None] # raw [0, 255]; normalization is inside the model
logits = model(pixels, training=False)
print("top-1 ImageNet class id:", int(keras.ops.convert_to_numpy(logits)[0].argmax()))
Load any LeViT variant the same way with from_weights("zeromodels/<variant>"):
| Variant | Hub |
|---|---|
levit-128S |
zeromodels/levit-128S |
levit-128 |
zeromodels/levit-128 |
levit-192 |
zeromodels/levit-192 |
levit-256 |
zeromodels/levit-256 |
levit-384 |
zeromodels/levit-384 |
Tips
- Set
KERAS_BACKENDbefore 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. LevitImageClassifyaverages the two distillation heads internally;LevitModel.from_weights(...)gives the backbone (the final token sequence, no head).- See Classification backbones and Loading Weights.
- Community / upstream safetensors still work via the
hf:prefix, e.g.LevitImageClassify.from_weights("hf:facebook/levit-128").
Special Thanks
A huge thank you to the Meta AI LeViT authors for creating and releasing these models.
License: Apache 2.0.
Model tree for zeromodels/levit-128
Base model
facebook/levit-128