# AltCLIP

AltCLIP text-image representation 中英文 Chinese&English CLIP FlagAI

## 简介 Brief Introduction

AltCLIP模型可以为本项目中的AltDiffusion模型提供支持，关于AltDiffusion模型的具体信息可查看此教程

We propose a simple and efficient method to train a better bilingual CLIP model. Named AltCLIP. AltCLIP is trained based on Stable Diffusiosn with training data from WuDao dataset and Liaon.

The AltCLIP model can provide support for the AltDiffusion model in this project. Specific information on the AltDiffusion model can be found in this tutorial.

The model code has been open sourced on FlagAI and the weights are located on modelhub. We also provide scripts for fine-tuning, inference, and validation, so feel free to try them out.

## 引用

@article{https://doi.org/10.48550/arxiv.2211.06679,
doi = {10.48550/ARXIV.2211.06679},
url = {https://arxiv.org/abs/2211.06679},
author = {Chen, Zhongzhi and Liu, Guang and Zhang, Bo-Wen and Ye, Fulong and Yang, Qinghong and Wu, Ledell},
keywords = {Computation and Language (cs.CL), FOS: Computer and information sciences},
title = {AltCLIP: Altering the Language Encoder in CLIP for Extended Language Capabilities},
publisher = {arXiv},
year = {2022},
}


## 训练 Training

There are two phases of training. In the parallel knowledge distillation phase, we only use parallel corpus texts for distillation (parallel corpus is easier to obtain and larger in number compared to image text pairs). In the bilingual comparison learning phase, we use a small number of Chinese-English image-text pairs (about 2 million in total) to train our text encoder to better fit the image encoder.

## 下游效果 Performance

 Language Method Text-to-Image Retrival Image-to-Text Retrival MR R@1 R@5 R@10 R@1 R@5 R@10 English CLIP 65.0 87.1 92.2 85.1 97.3 99.2 87.6 Taiyi 25.3 48.2 59.2 39.3 68.1 79.6 53.3 Wukong - - - - - - - R2D2 - - - - - - - CN-CLIP 49.5 76.9 83.8 66.5 91.2 96.0 77.3 AltCLIP 66.3 87.8 92.7 85.9 97.7 99.1 88.3 AltCLIP∗ 72.5 91.6 95.4 86.0 98.0 99.1 90.4 Chinese CLIP 0.0 2.4 4.0 2.3 8.1 12.6 5.0 Taiyi 53.7 79.8 86.6 63.8 90.5 95.9 78.4 Wukong 51.7 78.9 86.3 76.1 94.8 97.5 80.9 R2D2 60.9 86.8 92.7 77.6 96.7 98.9 85.6 CN-CLIP 68.0 89.7 94.4 80.2 96.6 98.2 87.9 AltCLIP 63.7 86.3 92.1 84.7 97.4 98.7 87.2 AltCLIP∗ 69.8 89.9 94.7 84.8 97.4 98.8 89.2

## 可视化效果 Visualization effects

Based on AltCLIP, we have also developed the AltDiffusion model, visualized as follows.

## 模型推理 Inference

from PIL import Image
import requests

# transformers version >= 4.21.0
from modeling_altclip import AltCLIP
from processing_altclip import AltCLIPProcessor

# now our repo's in private, so we need use_auth_token=True
model = AltCLIP.from_pretrained("BAAI/AltCLIP")
processor = AltCLIPProcessor.from_pretrained("BAAI/AltCLIP")

url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw)

inputs = processor(text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True)

outputs = model(**inputs)
logits_per_image = outputs.logits_per_image # this is the image-text similarity score
probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities