Kunyi commited on
Commit
d7e6ab3
1 Parent(s): 6fbc593

Update README_CN.md

Browse files
Files changed (1) hide show
  1. 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
- import clip as clip
182
- from clip import load_from_name, available_models
183
- print("Available models:", available_models())
184
- # Available models: ['ViT-B-16', 'ViT-L-14', 'RN50']
185
 
186
- device = "cuda" if torch.cuda.is_available() else "cpu"
187
- model, preprocess = load_from_name("ViT-B-16", device=device, download_root='./')
188
- model.eval()
189
- image = preprocess(Image.open("examples/pokemon.jpeg")).unsqueeze(0).to(device)
190
- text = clip.tokenize(["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]).to(device)
191
 
192
- with torch.no_grad():
193
- image_features = model.encode_image(image)
194
- text_features = model.encode_text(text)
195
- # 对特征进行归一化,请使用归一化后的图文特征用于下游任务
196
- image_features /= image_features.norm(dim=-1, keepdim=True)
197
- text_features /= text_features.norm(dim=-1, keepdim=True)
198
 
199
- logits_per_image, logits_per_text = model.get_similarity(image, text)
200
- probs = logits_per_image.softmax(dim=-1).cpu().numpy()
 
 
201
 
202
- print("Label probs:", probs)
 
 
 
 
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