thu-ml/zh-clip

73

stars

8

commits

Python

primary language

Jun 28, 2023

updated

Browse cluster: Vision-Language Model Checkpoints

README

ZH-CLIP: A Chinese CLIP Model

Hugging Face Spaces

Models

You can download ZH-CLIP model from 🤗 thu-ml/zh-clip-vit-roberta-large-patch14. The model structure is shown below:

Results

Note: We tried to maintain consistency with the majority of comparative models in terms of scale. However, some models used smaller text encoders (eg. CNCLIP), so the following comparisons may not be entirely fair.

COCO-CN Retrieval (Official Test Set):

ModelText-to-ImageImage-to-Text
R@1R@5R@10MeanR@1R@5R@10Mean
Clip-Chinese22.6050.0465.2445.9622.849.864.145.57
mclip56.5183.5790.7976.9559.987.394.180.43
Taiyi-CLIP52.5281.1089.9374.5245.8075.8088.1069.90
CN-CLIP64.1088.7994.4082.4361.0084.4093.1079.5
altclip-xlmr-l62.8787.1894.0181.3563.388.395.382.3
ZH-CLIP68.0089.4695.4484.3068.5090.1096.5085.03

Flickr30K-CN Retrieval (Official Test Set):

ModelText-to-ImageImage-to-Text
R@1R@5R@10MeanR@1R@5R@10Mean
Clip-Chinese17.7640.3451.8836.6630.455.3067.1050.93
mclip62.386.4292.5880.4384.497.398.993.53
Taiyi-CLIP53.580.587.2473.7565.490.695.783.9
CN-CLIP67.9889.5494.4683.9981.296.698.292.0
altclip-xlmr-l69.1689.9494.584.5385.197.799.294.0
ZH-CLIP69.6490.1494.384.6986.697.698.894.33

Muge Text-to-Image Retrieval (Official Validation Set):

ModelText-to-Image
R@1R@5R@10Mean
Clip-Chinese15.0634.9646.2132.08
mclip22.3441.1550.2637.92
Taiyi-CLIP42.0967.7577.2162.35
cn-clip56.2579.8786.5074.21
altclip-xlmr-l29.6949.9258.8746.16
ZH-CLIP56.7579.7586.6674.38

Zero-shot Image Classification:

ModelZero-shot Classification (ACC1)
CIFAR10CIFAR100DTDEuroSATFERFGVCKITTIMNISTPCVOCImageNet
Clip-Chinese86.8544.2118.4034.8614.213.8732.6314.3752.4967.7322.22
mclip92.8865.5429.5746.7641.187.2023.2152.8051.6477.5642.99
Taiyi-CLIP95.6273.3040.6961.6236.2213.9841.2173.9150.0275.2849.82
CN-CLIP94.7575.0444.7352.3448.5720.5520.1161.9962.5979.1253.40
Altclip-xlmr-l95.4977.2942.0756.9651.5226.8524.8965.6850.0277.9959.21
ZH-CLIP97.0880.7347.6651.5848.4820.7320.1161.9462.3178.0756.87

Getting Started

Dependency

  • python >= 3.9
  • pip install -r requirements.txt

Inference

from PIL import Image
import requests
from models.zhclip import ZhCLIPProcessor, ZhCLIPModel

version = 'thu-ml/zh-clip-vit-roberta-large-patch14'
model = ZhCLIPModel.from_pretrained(version)
processor = ZhCLIPProcessor.from_pretrained(version)

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=["一只猫", "一只狗"], images=image, return_tensors="pt", padding=True)

outputs = model(**inputs)
image_features = outputs.image_features
text_features = outputs.text_features
text_probs = (image_features @ text_features.T).softmax(dim=-1)

Other Chinese CLIP Models

In addition, to compare the effectiveness of different methods, the inference methods of other Chinese CLIP models have been integrated. For the convenience of use, the inference code has also been made public, and please contact us if there is any infringement. The code only implements models at the same level as clip-vit-large-patch14, but it may be adapted for the use of more different versions of models in the future.

#modelalias
0ZH-CLIPzhclip
1AltCLIPaltclip
2Chinese-CLIPcnclip
3TaiyiCLIPtaiyiclip
4Multilingual-CLIPmclip
5CLIP-Chineseclip-chinese

Usage in inference.py

Contributors

yue-gang

8 commits

thu-ml/zh-clip

73

stars

8

commits

Python

primary language

Jun 28, 2023

updated

Browse cluster: Vision-Language Model Checkpoints

README

ZH-CLIP: A Chinese CLIP Model

Hugging Face Spaces

Models

You can download ZH-CLIP model from 🤗 thu-ml/zh-clip-vit-roberta-large-patch14. The model structure is shown below:

Results

Note: We tried to maintain consistency with the majority of comparative models in terms of scale. However, some models used smaller text encoders (eg. CNCLIP), so the following comparisons may not be entirely fair.

COCO-CN Retrieval (Official Test Set):

ModelText-to-ImageImage-to-Text
R@1R@5R@10MeanR@1R@5R@10Mean
Clip-Chinese22.6050.0465.2445.9622.849.864.145.57
mclip56.5183.5790.7976.9559.987.394.180.43
Taiyi-CLIP52.5281.1089.9374.5245.8075.8088.1069.90
CN-CLIP64.1088.7994.4082.4361.0084.4093.1079.5
altclip-xlmr-l62.8787.1894.0181.3563.388.395.382.3
ZH-CLIP68.0089.4695.4484.3068.5090.1096.5085.03

Flickr30K-CN Retrieval (Official Test Set):

ModelText-to-ImageImage-to-Text
R@1R@5R@10MeanR@1R@5R@10Mean
Clip-Chinese17.7640.3451.8836.6630.455.3067.1050.93
mclip62.386.4292.5880.4384.497.398.993.53
Taiyi-CLIP53.580.587.2473.7565.490.695.783.9
CN-CLIP67.9889.5494.4683.9981.296.698.292.0
altclip-xlmr-l69.1689.9494.584.5385.197.799.294.0
ZH-CLIP69.6490.1494.384.6986.697.698.894.33

Muge Text-to-Image Retrieval (Official Validation Set):

ModelText-to-Image
R@1R@5R@10Mean
Clip-Chinese15.0634.9646.2132.08
mclip22.3441.1550.2637.92
Taiyi-CLIP42.0967.7577.2162.35
cn-clip56.2579.8786.5074.21
altclip-xlmr-l29.6949.9258.8746.16
ZH-CLIP56.7579.7586.6674.38

Zero-shot Image Classification:

ModelZero-shot Classification (ACC1)
CIFAR10CIFAR100DTDEuroSATFERFGVCKITTIMNISTPCVOCImageNet
Clip-Chinese86.8544.2118.4034.8614.213.8732.6314.3752.4967.7322.22
mclip92.8865.5429.5746.7641.187.2023.2152.8051.6477.5642.99
Taiyi-CLIP95.6273.3040.6961.6236.2213.9841.2173.9150.0275.2849.82
CN-CLIP94.7575.0444.7352.3448.5720.5520.1161.9962.5979.1253.40
Altclip-xlmr-l95.4977.2942.0756.9651.5226.8524.8965.6850.0277.9959.21
ZH-CLIP97.0880.7347.6651.5848.4820.7320.1161.9462.3178.0756.87

Getting Started

Dependency

  • python >= 3.9
  • pip install -r requirements.txt

Inference

from PIL import Image
import requests
from models.zhclip import ZhCLIPProcessor, ZhCLIPModel

version = 'thu-ml/zh-clip-vit-roberta-large-patch14'
model = ZhCLIPModel.from_pretrained(version)
processor = ZhCLIPProcessor.from_pretrained(version)

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=["一只猫", "一只狗"], images=image, return_tensors="pt", padding=True)

outputs = model(**inputs)
image_features = outputs.image_features
text_features = outputs.text_features
text_probs = (image_features @ text_features.T).softmax(dim=-1)

Other Chinese CLIP Models

In addition, to compare the effectiveness of different methods, the inference methods of other Chinese CLIP models have been integrated. For the convenience of use, the inference code has also been made public, and please contact us if there is any infringement. The code only implements models at the same level as clip-vit-large-patch14, but it may be adapted for the use of more different versions of models in the future.

#modelalias
0ZH-CLIPzhclip
1AltCLIPaltclip
2Chinese-CLIPcnclip
3TaiyiCLIPtaiyiclip
4Multilingual-CLIPmclip
5CLIP-Chineseclip-chinese

Usage in inference.py

Contributors

yue-gang

8 commits

Languages

Python

100.0%