See our collection for all versions of LeViT.

Run LeViT with Keras 3: JAX, PyTorch, or TensorFlow

GitHub Docs Collection

zeromodels/levit-256

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. Larger LeViT (hidden sizes 256/384/512).

For more details on the model, please go to Meta's original model card.

Pure-Keras 3 conversion of facebook/levit-256 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-256")

# 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_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 and 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.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Model tree for zeromodels/levit-256

Finetuned
(2)
this model

Collection including zeromodels/levit-256

Paper for zeromodels/levit-256