yangapku commited on
Commit
09c9022
1 Parent(s): 1a2fce3

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +28 -34
README.md CHANGED
@@ -9,42 +9,36 @@ license: apache-2.0
9
  This is the base-version of the Chinese CLIP, with ViT-B/16 as the image encoder and RoBERTa-wwm-base as the text encoder. Chinese CLIP is a simple implementation of CLIP on a large-scale dataset of around 200 million Chinese image-text pairs. For more details, please refer to our technical report https://arxiv.org/abs/2211.01335 and our official github repo https://github.com/OFA-Sys/Chinese-CLIP
10
 
11
  ## Use with the official API
12
- We provide a simple code snippet to show how to use the API for Chinese-CLIP. For starters, please install cn_clip:
13
- ```bash
14
- # to install the latest stable release
15
- pip install cn_clip
16
-
17
- # or install from source code
18
- cd Chinese-CLIP
19
- pip install -e .
20
- ```
21
- After installation, use Chinese CLIP as shown below:
22
  ```python
23
- import torch
24
  from PIL import Image
25
-
26
- import cn_clip.clip as clip
27
- from cn_clip.clip import load_from_name, available_models
28
- print("Available models:", available_models())
29
- # Available models: ['ViT-B-16', 'ViT-L-14', 'ViT-L-14-336', 'ViT-H-14', 'RN50']
30
-
31
- device = "cuda" if torch.cuda.is_available() else "cpu"
32
- model, preprocess = load_from_name("ViT-B-16", device=device, download_root='./')
33
- model.eval()
34
- image = preprocess(Image.open("examples/pokemon.jpeg")).unsqueeze(0).to(device)
35
- text = clip.tokenize(["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]).to(device)
36
-
37
- with torch.no_grad():
38
- image_features = model.encode_image(image)
39
- text_features = model.encode_text(text)
40
- # Normalize the features. Please use the normalized features for downstream tasks.
41
- image_features /= image_features.norm(dim=-1, keepdim=True)
42
- text_features /= text_features.norm(dim=-1, keepdim=True)
43
-
44
- logits_per_image, logits_per_text = model.get_similarity(image, text)
45
- probs = logits_per_image.softmax(dim=-1).cpu().numpy()
46
-
47
- print("Label probs:", probs) # [[1.268734e-03 5.436878e-02 6.795761e-04 9.436829e-01]]
 
 
 
48
  ```
49
 
50
  However, if you are not satisfied with only using the API, feel free to check our github repo https://github.com/OFA-Sys/Chinese-CLIP for more details about training and inference.
 
9
  This is the base-version of the Chinese CLIP, with ViT-B/16 as the image encoder and RoBERTa-wwm-base as the text encoder. Chinese CLIP is a simple implementation of CLIP on a large-scale dataset of around 200 million Chinese image-text pairs. For more details, please refer to our technical report https://arxiv.org/abs/2211.01335 and our official github repo https://github.com/OFA-Sys/Chinese-CLIP
10
 
11
  ## Use with the official API
12
+ We provide a simple code snippet to show how to use the API of Chinese-CLIP to compute the image & text embeddings and similarities.
13
+
 
 
 
 
 
 
 
 
14
  ```python
 
15
  from PIL import Image
16
+ import requests
17
+ from transformers import ChineseCLIPProcessor, ChineseCLIPModel
18
+
19
+ model = ChineseCLIPModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")
20
+ processor = ChineseCLIPProcessor.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")
21
+
22
+ url = "https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg"
23
+ image = Image.open(requests.get(url, stream=True).raw)
24
+ # Squirtle, Bulbasaur, Charmander, Pikachu in English
25
+ texts = ["杰尼龟", "妙蛙种子", "小火龙", "皮卡丘"]
26
+
27
+ # compute image feature
28
+ inputs = processor(images=image, return_tensors="pt")
29
+ image_features = model.get_image_features(**inputs)
30
+ image_features = image_features / image_features.norm(p=2, dim=-1, keepdim=True) # normalize
31
+
32
+ # compute text features
33
+ inputs = processor(text=texts, padding=True, return_tensors="pt")
34
+ text_features = model.get_text_features(**inputs)
35
+ text_features = text_features / text_features.norm(p=2, dim=-1, keepdim=True) # normalize
36
+
37
+ # compute image-text similarity scores
38
+ inputs = processor(text=texts, images=image, return_tensors="pt", padding=True)
39
+ outputs = model(**inputs)
40
+ logits_per_image = outputs.logits_per_image # this is the image-text similarity score
41
+ probs = logits_per_image.softmax(dim=1) # probs: [[1.2686e-03, 5.4499e-02, 6.7968e-04, 9.4355e-01]]
42
  ```
43
 
44
  However, if you are not satisfied with only using the API, feel free to check our github repo https://github.com/OFA-Sys/Chinese-CLIP for more details about training and inference.