Zero-Shot Image Classification
PerceptionEncoder
jz2023 commited on
Commit
73f7ad8
·
verified ·
1 Parent(s): c0b73e5

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +87 -3
README.md CHANGED
@@ -1,3 +1,87 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+
5
+ # Model Details
6
+
7
+ Perception Encoder (PE) is a state-of-the-art encoder for image and video understanding trained via simple vision-language learning. It was introduced in "[Perception Encoder: The best visual embeddings
8
+ are not at the output of the network](https://ai.meta.com/research/publications/perception-encoder-the-best-visual-embeddings-are-not-at-the-output-of-the-network/)".
9
+
10
+ **Model Developer**: Meta
11
+
12
+ **Model Overview**: Perception Encoder (PE) is a family of large-scale vision encoder models with state-of-the-art performance on a large variety of vision tasks. By using a robust contrastive pretraining recipe and finetuning on synthetically aligned videos, PE not only outperforms all existing models on classification and retrieval, but it also internally produces strong, general features that scale for downstream tasks. PE unlocks the ability for large-scale contrastive pretraining to transfer to downstream tasks with alignment tuning to capitalize on those general features.
13
+
14
+ <img src="https://huggingface.co/facebook/PE-Core-G14-448/resolve/main/docs/pe_image1.png" style="width: 100%; margin: 0 auto; display: block;" />
15
+
16
+
17
+ | Scale | Tower | Params | Width | Depth | MLP | Heads | CLIP Dim | Resolution | Patch Size | Text Context Length |
18
+ | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
19
+ | **B** | Vision | 0.09B | 768 | 12 | 3072 | 12 | 1024 | 224 | 16 | 32 |
20
+ | | Text | 0.31B | 1024 | 24 | 4096 | 16 | 1024 | 224 | 16 | 32 |
21
+ | **L** | Vision | 0.32B | 1024 | 24 | 4096 | 16 | 1024 | 336 | 14 | 32 |
22
+ | | Text | 0.31B | 1024 | 24 | 4096 | 16 | 1024 | 336 | 14 | 32 |
23
+ | **G** | Vision | 1.88B | 1536 | 50 | 8960 | 16 | 1280 | 448 | 14 | 72 |
24
+ | | Text | 0.47B | 1280 | 24 | 5120 | 20 | 1280 | 448 | 14 | 72 |
25
+
26
+
27
+ # How to use
28
+
29
+ ## PE codebase
30
+ We provide the pretraining code in https://github.com/facebookresearch/perception_models
31
+ ```shell
32
+ git clone https://github.com/facebookresearch/perception_models.git
33
+ cd perception_models
34
+ conda create --name occhi-env python=3.12
35
+ conda activate occhi-env
36
+ # Install PyTorch
37
+ pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 xformers --index-url https://download.pytorch.org/whl/cu124
38
+ # We use torchcodec for decoding videos into PyTorch tensors
39
+ conda install ffmpeg -c conda-forge
40
+ pip install torchcodec==0.1 --index-url=https://download.pytorch.org/whl/cu124
41
+ pip install -e .
42
+ ```
43
+ ## Image and Textg Feature extraction with a Trained Model :robot:
44
+ ```python
45
+ import torch
46
+ from occhi.vision_encoder.factory import create_model_and_transforms, get_tokenizer
47
+ from PIL import Image
48
+
49
+ model_name = 'PEv1-B16-224 '
50
+ pretrained='PATH_TO_PE_Core_B16_224'
51
+
52
+ model, _, preprocess = create_model_and_transforms(
53
+ model_name,
54
+ pretrained=pretrained,
55
+ )
56
+ model = model.cuda()
57
+ tokenizer = get_tokenizer(model_name)
58
+ image = preprocess(Image.open("docs/cat.png")).unsqueeze(0).cuda()
59
+ text = tokenizer(["a diagram", "a dog", "a cat"]).cuda()
60
+
61
+ with torch.no_grad(), torch.autocast("cuda"):
62
+ image_features = model.encode_image(image)
63
+ text_features = model.encode_text(text)
64
+ image_features /= image_features.norm(dim=-1, keepdim=True)
65
+ text_features /= text_features.norm(dim=-1, keepdim=True)
66
+ text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
67
+
68
+ print("Label probs:", text_probs) # prints: [[0.0, 0.0, 1.0]]
69
+ ```
70
+ You can find more details in the GitHub repo.
71
+ # Evaluation
72
+ We evaluate the pretrained PE models on Zero-shot Common Sense Reasoning tasks
73
+ Here is the table in Markdown format:
74
+ ## Zero-Shot Image Results
75
+ <img src="https://huggingface.co/facebook/PE-Core-G14-448/resolve/main/docs/pe_zeroshot_image.png" style="width: 100%; margin: 0;" />
76
+ ## Zero-Shot Video Results
77
+ <img src="https://huggingface.co/facebook/PE-Core-G14-448/resolve/main/docs/pe_zeroshot_video.png" style="width: 90%; margin: 0" />
78
+ # Citation
79
+ If you find our code useful for your research, please consider citing:
80
+
81
+ @article{PE,
82
+ title={Perception Encoder: The best visual embeddings are not at the output of the network},
83
+ author={},
84
+ journal={arXiv:xxx.xxxxx},
85
+ year={2025}
86
+ }
87
+