File size: 3,289 Bytes
c17c3e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
---
license: apache-2.0
model-index:
  - name: SPEC-CLIP-ViT-B-32
    results:
      - task:
          type: image-text-matching
        dataset:
          name: SPEC
          type: compositional-reasoning
        metrics:
          - name: Absolute Size I2T
            type: Image to Text Matching
            value: 68.9
          - name: Absolute Size T2I
            type: Image to Text Matching
            value: 60.7
          - name: Relative Size I2T
            type: Image to Text Matching
            value: 40.3
          - name: Relative Size T2I
            type: Image to Text Matching
            value: 44.1
          - name: Absolute Position I2T
            type: Image to Text Matching
            value: 30.6
          - name: Absolute Position T2I
            type: Image to Text Matching
            value: 34.2
          - name: Relative Position I2T
            type: Image to Text Matching
            value: 46.6
          - name: Relative Position T2I
            type: Image to Text Matching
            value: 46.9
          - name: Existence I2T
            type: Image to Text Matching
            value: 83.4
          - name: Existence T2I
            type: Image to Text Matching
            value: 53.1
          - name: Count I2T
            type: Image to Text Matching
            value: 55.6
          - name: Count T2I
            type: Image to Text Matching
            value: 57.8
        source:
          name: SPEC paper
          url: https://arxiv.org/pdf/2312.00081
---
# SPEC-CLIP-ViT-B-32

### Model Sources
[**Code**](https://github.com/wjpoom/SPEC) | [**Paper**](https://huggingface.co/papers/2312.00081) | [**arXiv**](https://arxiv.org/abs/2312.00081)

### Model Usage
* download checkpoint
```shell
huggingface-cli download wjpoom/SPEC-CLIP-ViT-B-32 --local-dir checkpoints/SPEC-CLIP-ViT-B-32
```

* load model
```python
# pip install open_clip_torch
import torch
from PIL import Image
import open_clip

model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='checkpoints/SPEC-CLIP-ViT-B-32', load_weights_only=False)
model.eval()  # model in train mode by default, impacts some models with BatchNorm or stochastic depth active
tokenizer = open_clip.get_tokenizer('ViT-B-32')

image = preprocess(Image.open("docs/CLIP.png")).unsqueeze(0)
text = tokenizer(["a diagram", "a dog", "a cat"])

with torch.no_grad(), torch.autocast("cuda"):
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)
    image_features /= image_features.norm(dim=-1, keepdim=True)
    text_features /= text_features.norm(dim=-1, keepdim=True)

    text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)

print("Label probs:", text_probs)  # prints: [[1., 0., 0.]]
```

## Contact
Feel free to contact us if you have any questions or suggestions 
- Email (Wujian Peng): wjpeng24@m.fudan.edu.cn

## Citation
``` bibtex
@inproceedings{peng2024synthesize,
  title={Synthesize diagnose and optimize: Towards fine-grained vision-language understanding},
  author={Peng, Wujian and Xie, Sicheng and You, Zuyao and Lan, Shiyi and Wu, Zuxuan},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={13279--13288},
  year={2024}
}
```