kimihailv commited on
Commit
c77c978
1 Parent(s): fbda967

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +135 -0
README.md ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: feature-extraction
4
+ tags:
5
+ - clip
6
+ - vision
7
+ datasets:
8
+ - Ziyang/yfcc15m
9
+ - conceptual_captions
10
+ ---
11
+ <h1 align="center">UForm</h1>
12
+ <h3 align="center">
13
+ Multi-Modal Inference Library<br/>
14
+ For Semantic Search Applications<br/>
15
+ </h3>
16
+
17
+ ---
18
+
19
+ UForm is a Multi-Modal Modal Inference package, designed to encode Multi-Lingual Texts, Images, and, soon, Audio, Video, and Documents, into a shared vector space!
20
+
21
+ This is model card of the __English only model__ with:
22
+
23
+ * 4 layers BERT (2 layers for unimodal encoding and rest layers for multimodal encoding)
24
+ * ViT-S/16 (image resolution is 224x224)
25
+
26
+
27
+ If you need Multilingual model, check [this](https://huggingface.co/unum-cloud/uform-vl-multilingual).
28
+
29
+ ## Evaluation
30
+
31
+ The following metrics were obtained with multimodal re-ranking (text-to-image retrieval):
32
+
33
+ | Dataset |Recall@1 | Recall@5 | Recall@10 |
34
+ | :------ | ------: | --------: | --------: |
35
+ | Zero-Shot Flickr | 0.565 | 0.790 | 0.860 |
36
+ | Zero-Shot MS-COCO | 0.281 | 0.525 | 0.645 |
37
+
38
+ ImageNet-Top1: 0.361 \
39
+ ImageNet-Top5: 0.608
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ pip install uform[onnx-gpu]
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ To load the model:
50
+
51
+ ```python
52
+ import uform
53
+
54
+ model = uform.get_model_onnx('unum-cloud/uform-vl-english-small', device='gpu', dtype='fp16')
55
+ ```
56
+
57
+ To encode data:
58
+
59
+ ```python
60
+ from PIL import Image
61
+
62
+ text = 'a small red panda in a zoo'
63
+ image = Image.open('red_panda.jpg')
64
+
65
+ image_data = model.preprocess_image(image)
66
+ text_data = model.preprocess_text(text)
67
+
68
+ image_embedding = model.encode_image(image_data)
69
+ text_embedding = model.encode_text(text_data)
70
+ score, joint_embedding = model.encode_multimodal(
71
+ image_features=image_features,
72
+ text_features=text_features,
73
+ attention_mask=text_data['attention_mask'],
74
+ return_scores=True
75
+ )
76
+ ```
77
+
78
+ To get features:
79
+
80
+ ```python
81
+ image_features, image_embedding = model.encode_image(image_data, return_features=True)
82
+ text_features, text_embedding = model.encode_text(text_data, return_features=True)
83
+ ```
84
+
85
+ These features can later be used to produce joint multimodal encodings faster, as the first layers of the transformer can be skipped:
86
+
87
+ ```python
88
+ joint_embedding = model.encode_multimodal(
89
+ image_features=image_features,
90
+ text_features=text_features,
91
+ attention_mask=text_data['attention_mask']
92
+ )
93
+ ```
94
+
95
+ There are two options to calculate semantic compatibility between an image and a text: [Cosine Similarity](#cosine-similarity) and [Matching Score](#matching-score).
96
+
97
+ ### Cosine Similarity
98
+
99
+ ```python
100
+ import torch.nn.functional as F
101
+
102
+ similarity = F.cosine_similarity(image_embedding, text_embedding)
103
+ ```
104
+
105
+ The `similarity` will belong to the `[-1, 1]` range, `1` meaning the absolute match.
106
+
107
+ __Pros__:
108
+
109
+ - Computationally cheap.
110
+ - Only unimodal embeddings are required, unimodal encoding is faster than joint encoding.
111
+ - Suitable for retrieval in large collections.
112
+
113
+ __Cons__:
114
+
115
+ - Takes into account only coarse-grained features.
116
+
117
+
118
+ ### Matching Score
119
+
120
+ Unlike cosine similarity, unimodal embedding are not enough.
121
+ Joint embedding will be needed and the resulting `score` will belong to the `[0, 1]` range, `1` meaning the absolute match.
122
+
123
+ ```python
124
+ score = model.get_matching_scores(joint_embedding)
125
+ ```
126
+
127
+ __Pros__:
128
+
129
+ - Joint embedding captures fine-grained features.
130
+ - Suitable for re-ranking – sorting retrieval result.
131
+
132
+ __Cons__:
133
+
134
+ - Resource-intensive.
135
+ - Not suitable for retrieval in large collections.