weifeng commited on
Commit
75c0a66
1 Parent(s): a653c4d
Files changed (6) hide show
  1. README.md +121 -0
  2. config.json +34 -0
  3. pytorch_model.bin +3 -0
  4. special_tokens_map.json +7 -0
  5. tokenizer_config.json +15 -0
  6. vocab.txt +0 -0
README.md ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ # inference: false
4
+ # pipeline_tag: zero-shot-image-classification
5
+ pipeline_tag: feature-extraction
6
+
7
+ # inference:
8
+ # parameters:
9
+ tags:
10
+ - clip
11
+ - zh
12
+ - image-text
13
+ - feature-extraction
14
+ ---
15
+
16
+ # Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese
17
+
18
+ - Github: [Fengshenbang-LM](https://github.com/IDEA-CCNL/Fengshenbang-LM)
19
+ - Docs: [Fengshenbang-Docs](https://fengshenbang-doc.readthedocs.io/)
20
+
21
+ ## 简介 Brief Introduction
22
+
23
+ 首个开源的中文CLIP模型,1.23亿图文对上进行预训练的文本端RoBERTa-base
24
+
25
+ The first open source Chinese CLIP, pre-training on 123M image-text pairs, the text encoder: RoBERTa-base.
26
+
27
+ ## 模型分类 Model Taxonomy
28
+
29
+ | 需求 Demand | 任务 Task | 系列 Series | 模型 Model | 参数 Parameter | 额外 Extra |
30
+ | :----: | :----: | :----: | :----: | :----: | :----: |
31
+ | 特殊 Special | 多模态 Multimodal | 太乙 Taiyi | CLIP (Roberta) | 102M | Chinese |
32
+
33
+ ## 模型信息 Model Information
34
+
35
+ 我们遵循CLIP的实验设置,以获得强大的视觉-语言表征。在训练中文版的CLIP时,我们使用[chinese-roberta-wwm](https://huggingface.co/hfl/chinese-roberta-wwm-ext)作为语言的编码器,并将[open_clip](https://github.com/mlfoundations/open_clip)中的**ViT-H-14**应用于视觉的编码器。为了快速且稳定地进行预训练,我们冻结了视觉编码器并且只微调语言编码器。此外,我们将[Noah-Wukong](https://wukong-dataset.github.io/wukong-dataset/)数据集(100M)和[Zero](https://zero.so.com/)数据集(23M)用作预训练的数据集。在悟空数据集和zero数据集上预训练24轮。据我们所知,我们的Taiyi-CLIP是目前Huggingface社区中首个的开源中文CLIP。
36
+
37
+ We follow the experimental setup of CLIP to obtain powerful visual-language intelligence. To obtain the CLIP for Chinese, we employ [chinese-roberta-wwm](https://huggingface.co/hfl/chinese-roberta-wwm-ext) for the language encoder, and apply the **ViT-H-14** in [open_clip](https://github.com/mlfoundations/open_clip) for the vision encoder. We freeze the vision encoder and tune the language encoder to speed up and stabilize the pre-training process. Moreover, we apply [Noah-Wukong](https://wukong-dataset.github.io/wukong-dataset/) dataset (100M) and [Zero](https://zero.so.com/) dataset (23M) as the pre-training datasets. The model was first trained 24 epochs on wukong and zero. To the best of our knowledge, our TaiyiCLIP is currently the only open-sourced Chinese CLIP in the huggingface community.
38
+
39
+ ### 下游效果 Performance
40
+
41
+ **Zero-Shot Classification**
42
+
43
+ | model | dataset | Top1 | Top5 |
44
+ | ---- | ---- | ---- | ---- |
45
+ | Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese | ImageNet1k-CN | 54.35% | 80.64% |
46
+
47
+ **Zero-Shot Text-to-Image Retrieval**
48
+
49
+ | model | dataset | Top1 | Top5 | Top10 |
50
+ | ---- | ---- | ---- | ---- | ---- |
51
+ | Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese | Flickr30k-CNA-test | 60.82% | 85.00% | 91.04% |
52
+ | Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese | COCO-CN-test | 60.02% | 83.95% | 93.26% |
53
+ | Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese | wukong50k | 66.85% | 92.81% | 96.69% |
54
+
55
+ ## 使用 Usage
56
+
57
+ ```python3
58
+ from PIL import Image
59
+ import requests
60
+ import open_clip
61
+ import torch
62
+ from transformers import BertModel, BertConfig, BertTokenizer
63
+ from transformers import CLIPProcessor, CLIPModel
64
+ import numpy as np
65
+
66
+ query_texts = ["一只猫", "一只狗",'两只猫', '两只老虎','一只老虎'] # 这里是输入文本的,可以随意替换。
67
+ # 加载Taiyi 中文 text encoder
68
+ text_tokenizer = BertTokenizer.from_pretrained("IDEA-CCNL/Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese")
69
+ text_encoder = BertModel.from_pretrained("IDEA-CCNL/Taiyi-CLIP-RoBERTa-102M-ViT-L-Chinese").eval()
70
+
71
+ url = "http://images.cocodataset.org/val2017/000000039769.jpg" # 这里可以换成任意图片的url
72
+ # 加载openclip的image encoder
73
+ clip_model, _, processor = open_clip.create_model_and_transforms('ViT-L-14', pretrained='openai')
74
+ clip_model = clip_model.eval()
75
+
76
+
77
+ text = text_tokenizer(query_texts, return_tensors='pt', padding=True)['input_ids']
78
+ image = processor(Image.open(requests.get(url, stream=True).raw)).unsqueeze(0)
79
+ with torch.no_grad():
80
+ image_features = clip_model.encode_image(image)
81
+ text_features = text_encoder(text)[1]
82
+ # 归一化
83
+ image_features = image_features / image_features.norm(dim=1, keepdim=True)
84
+ text_features = text_features / text_features.norm(dim=1, keepdim=True)
85
+ # 计算余弦相似度 logit_scale是尺度系数
86
+ logit_scale = clip_model.logit_scale.exp()
87
+ logits_per_image = logit_scale * image_features @ text_features.t()
88
+ logits_per_text = logits_per_image.t()
89
+ probs = logits_per_image.softmax(dim=-1).cpu().numpy()
90
+ print(np.around(probs, 3))
91
+
92
+ ```
93
+
94
+ ## 引用 Citation
95
+
96
+ 如果您在您的工作中使用了我们的模型,可以引用我们的[论文](https://arxiv.org/abs/2209.02970):
97
+
98
+ If you are using the resource for your work, please cite the our [paper](https://arxiv.org/abs/2209.02970):
99
+
100
+ ```text
101
+ @article{fengshenbang,
102
+ author = {Junjie Wang and Yuxiang Zhang and Lin Zhang and Ping Yang and Xinyu Gao and Ziwei Wu and Xiaoqun Dong and Junqing He and Jianheng Zhuo and Qi Yang and Yongfeng Huang and Xiayu Li and Yanghan Wu and Junyu Lu and Xinyu Zhu and Weifeng Chen and Ting Han and Kunhao Pan and Rui Wang and Hao Wang and Xiaojun Wu and Zhongshen Zeng and Chongpei Chen and Ruyi Gan and Jiaxing Zhang},
103
+ title = {Fengshenbang 1.0: Being the Foundation of Chinese Cognitive Intelligence},
104
+ journal = {CoRR},
105
+ volume = {abs/2209.02970},
106
+ year = {2022}
107
+ }
108
+ ```
109
+
110
+ 也可以引用我们的[网站](https://github.com/IDEA-CCNL/Fengshenbang-LM/):
111
+
112
+ You can also cite our [website](https://github.com/IDEA-CCNL/Fengshenbang-LM/):
113
+
114
+ ```text
115
+ @misc{Fengshenbang-LM,
116
+ title={Fengshenbang-LM},
117
+ author={IDEA-CCNL},
118
+ year={2021},
119
+ howpublished={\url{https://github.com/IDEA-CCNL/Fengshenbang-LM}},
120
+ }
121
+ ```
config.json ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "hfl/chinese-roberta-wwm-ext",
3
+ "architectures": [
4
+ "BertModel"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "bos_token_id": 0,
8
+ "classifier_dropout": null,
9
+ "directionality": "bidi",
10
+ "eos_token_id": 2,
11
+ "hidden_act": "gelu",
12
+ "hidden_dropout_prob": 0.1,
13
+ "hidden_size": 768,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 3072,
16
+ "layer_norm_eps": 1e-12,
17
+ "max_position_embeddings": 512,
18
+ "model_type": "bert",
19
+ "num_attention_heads": 12,
20
+ "num_hidden_layers": 12,
21
+ "output_past": true,
22
+ "pad_token_id": 0,
23
+ "pooler_fc_size": 768,
24
+ "pooler_num_attention_heads": 12,
25
+ "pooler_num_fc_layers": 3,
26
+ "pooler_size_per_head": 128,
27
+ "pooler_type": "first_token_transform",
28
+ "position_embedding_type": "absolute",
29
+ "torch_dtype": "float32",
30
+ "transformers_version": "4.22.1",
31
+ "type_vocab_size": 2,
32
+ "use_cache": true,
33
+ "vocab_size": 21128
34
+ }
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:905c1d9e6f25d5c04667d2267e939b3fc46f557185fa878821966ac47fbb453d
3
+ size 409140017
special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
tokenizer_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "do_basic_tokenize": true,
4
+ "do_lower_case": true,
5
+ "mask_token": "[MASK]",
6
+ "name_or_path": "hfl/chinese-roberta-wwm-ext",
7
+ "never_split": null,
8
+ "pad_token": "[PAD]",
9
+ "sep_token": "[SEP]",
10
+ "special_tokens_map_file": "/home/chenweifeng/.cache/huggingface/hub/models--hfl--chinese-roberta-wwm-ext/snapshots/5c58d0b8ec1d9014354d691c538661bf00bfdb44/special_tokens_map.json",
11
+ "strip_accents": null,
12
+ "tokenize_chinese_chars": true,
13
+ "tokenizer_class": "BertTokenizer",
14
+ "unk_token": "[UNK]"
15
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff