File size: 4,356 Bytes
3996190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56f38c8
3996190
 
 
56f38c8
3996190
 
56f38c8
3996190
56f38c8
 
 
 
 
 
 
 
3996190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---

pipeline_tag: image-classification
license: apache-2.0
base_model: OpenGVLab/pvt_v2_b2
library_name: zeromodels
tags:
- keras
- zeromodels
- image-classification
- pvt-v2
- backbone
- arxiv:2106.13797
- pytorch
- jax
- tf
---


## ***See [our collection](https://huggingface.co/collections/zeromodels/pvt-and-pvtv2-6a90e9dd0a2b03a982d0b876) for all PVT and PVTv2 versions.***

# Run PVTv2 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-PVTv2-blue)](https://imvision12.github.io/ZeroModels/pvt_v2/) [![Collection](https://img.shields.io/badge/HF-PVTv2%20collection-yellow)](https://huggingface.co/collections/zeromodels/pvt-and-pvtv2-6a90e9dd0a2b03a982d0b876)

# zeromodels/pvt-v2-b2

Paper: [PVTv2: Improved Baselines with Pyramid Vision Transformer (arXiv:2106.13797)](https://arxiv.org/abs/2106.13797) · [HF Papers](https://huggingface.co/papers/2106.13797)

PVTv2 improves PVT with overlapping patch embeddings, a convolutional feed-forward network, and no position embeddings (so any input resolution works), plus an optional linear-attention variant. Use `PvtV2ImageClassify` for logits or `PvtV2Model` for tokens / per-stage features via `as_backbone=True`.

- Parameters: ~25.4M
- ImageNet-1k top-1: **82.0%**

For more details on the model, see the upstream [model card](https://huggingface.co/OpenGVLab/pvt_v2_b2).

Pure-**Keras 3** conversion of [`OpenGVLab/pvt_v2_b2`](https://huggingface.co/OpenGVLab/pvt_v2_b2) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.

This is an **image-classification / backbone** checkpoint (`PvtV2ImageClassify` / `PvtV2Model`).

## ✨ Quick start

```python

import os



os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"



from PIL import Image

from zeromodels.models.pvt_v2 import PvtV2ImageClassify, PvtV2Model, PvtV2ImageProcessor



model = PvtV2ImageClassify.from_weights("zeromodels/pvt-v2-b2")

processor = PvtV2ImageProcessor.from_weights("zeromodels/pvt-v2-b2")



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 = PvtV2Model.from_weights("zeromodels/pvt-v2-b2", as_backbone=True)

features = backbone(pixels, training=False)

```

Normalization is baked into the graph, so pass raw `[0, 255]` pixels. Load any PVTv2
variant the same way with `from_weights("zeromodels/<variant>")`:

| Variant | ImageNet-1k top-1 | Hub |
|---|---|---|
| `pvt-v2-b0` | 70.5% | [`zeromodels/pvt-v2-b0`](https://huggingface.co/zeromodels/pvt-v2-b0) |
| `pvt-v2-b1` | 78.7% | [`zeromodels/pvt-v2-b1`](https://huggingface.co/zeromodels/pvt-v2-b1) |
| `pvt-v2-b2` | 82.0% | [`zeromodels/pvt-v2-b2`](https://huggingface.co/zeromodels/pvt-v2-b2) |
| `pvt-v2-b2-linear` | 82.1% | [`zeromodels/pvt-v2-b2-linear`](https://huggingface.co/zeromodels/pvt-v2-b2-linear) |
| `pvt-v2-b3` | 83.1% | [`zeromodels/pvt-v2-b3`](https://huggingface.co/zeromodels/pvt-v2-b3) |
| `pvt-v2-b4` | 83.6% | [`zeromodels/pvt-v2-b4`](https://huggingface.co/zeromodels/pvt-v2-b4) |
| `pvt-v2-b5` | 83.8% | [`zeromodels/pvt-v2-b5`](https://huggingface.co/zeromodels/pvt-v2-b5) |

## Tips

- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
- `PvtV2ImageClassify` returns class logits; `PvtV2Model` returns features (`as_backbone=True` for the four-stage pyramid).
- Both the model and its data format (`channels_last` / `channels_first`) are supported and bit-exact.
- See the [docs](https://imvision12.github.io/ZeroModels/pvt_v2/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
- Upstream checkpoints load directly: `PvtV2ImageClassify.from_weights("hf:OpenGVLab/pvt_v2_b2")`.

## Special Thanks

A huge thank you to the PVT authors ([whai362/PVT](https://github.com/whai362/PVT)) and the Hugging Face
community for creating and releasing these models.

License: see the YAML `license` above (matches the upstream checkpoint).