Update README_CN.md
Browse files- README_CN.md +21 -22
README_CN.md
CHANGED
@@ -170,36 +170,35 @@ pip install -r requirements.txt
|
|
170 |
```
|
171 |
|
172 |
## 推理代码
|
173 |
-
```bash
|
174 |
-
export PYTHONPATH=/yourpath/QA-CLIP-main
|
175 |
-
```
|
176 |
推理代码示例:
|
177 |
```python
|
178 |
-
import torch
|
179 |
from PIL import Image
|
|
|
|
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
print("Available models:", available_models())
|
184 |
-
# Available models: ['ViT-B-16', 'ViT-L-14', 'RN50']
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
text = clip.tokenize(["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]).to(device)
|
191 |
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
image_features /= image_features.norm(dim=-1, keepdim=True)
|
197 |
-
text_features /= text_features.norm(dim=-1, keepdim=True)
|
198 |
|
199 |
-
|
200 |
-
|
|
|
|
|
201 |
|
202 |
-
|
|
|
|
|
|
|
|
|
203 |
```
|
204 |
<br><br>
|
205 |
|
|
|
170 |
```
|
171 |
|
172 |
## 推理代码
|
|
|
|
|
|
|
173 |
推理代码示例:
|
174 |
```python
|
|
|
175 |
from PIL import Image
|
176 |
+
import requests
|
177 |
+
from transformers import ChineseCLIPProcessor, ChineseCLIPModel
|
178 |
|
179 |
+
model = ChineseCLIPModel.from_pretrained("TencentARC/QA-CLIP-ViT-B-16")
|
180 |
+
processor = ChineseCLIPProcessor.from_pretrained("TencentARC/QA-CLIP-ViT-B-16")
|
|
|
|
|
181 |
|
182 |
+
url = "https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg"
|
183 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
184 |
+
# Squirtle, Bulbasaur, Charmander, Pikachu in English
|
185 |
+
texts = ["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]
|
|
|
186 |
|
187 |
+
# compute image feature
|
188 |
+
inputs = processor(images=image, return_tensors="pt")
|
189 |
+
image_features = model.get_image_features(**inputs)
|
190 |
+
image_features = image_features / image_features.norm(p=2, dim=-1, keepdim=True) # normalize
|
|
|
|
|
191 |
|
192 |
+
# compute text features
|
193 |
+
inputs = processor(text=texts, padding=True, return_tensors="pt")
|
194 |
+
text_features = model.get_text_features(**inputs)
|
195 |
+
text_features = text_features / text_features.norm(p=2, dim=-1, keepdim=True) # normalize
|
196 |
|
197 |
+
# compute image-text similarity scores
|
198 |
+
inputs = processor(text=texts, images=image, return_tensors="pt", padding=True)
|
199 |
+
outputs = model(**inputs)
|
200 |
+
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
|
201 |
+
probs = logits_per_image.softmax(dim=1)
|
202 |
```
|
203 |
<br><br>
|
204 |
|