
CLIP模型数据处理优化工具
MetaCLIP是一个改进CLIP模型数据处理的开源项目。它提出了一种新的数据筛选算法,无需依赖现有模型即可从头整理数据。该项目强调数据质量,提供了可扩展到CommonCrawl全数据集的方法。MetaCLIP公开训练数据分布,提高了透明度,并为CLIP模型提供标准化的实验设置,便于控制实验和公平比较。
本仓库包含了MetaCLIP的代码,该方法在论文《解密CLIP数据》中被描述,将CLIP数据整理过程形式化为一个简单的算法。主要贡献包括:
我们得出以下结论:
MetaCLIP使用经过人脸模糊处理的 图像进行训练。
@inproceedings{xu2023metaclip, title={Demystifying CLIP Data}, author={Hu Xu, Saining Xie, Xiaoqing Ellen Tan, Po-Yao Huang, Russell Howes, Vasu Sharma, Shang-Wen Li, Gargi Ghosh, Luke Zettlemoyer and Christoph Feichtenhofer}, journal={arXiv preprint arXiv:2309.16671}, year={2023} }
预训练的MetaCLIP模型可在以下位置获取:
<details> <summary>[Huggingface](https://huggingface.co/models?other=metaclip)</summary></details> <details> <summary>[OpenCLIP](https://github.com/mlfoundations/open_clip)(或本仓库)</summary>from PIL import Image from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("facebook/metaclip-b32-400m") model = AutoModel.from_pretrained("facebook/metaclip-b32-400m") image = Image.open("docs/CLIP.png") inputs = processor(text=["一张图表", "一只狗", "一只猫"], images=image, return_tensors="pt", padding=True) with torch.no_grad(): outputs = model(**inputs) logits_per_image = outputs.logits_per_image # 这是图像-文本相似度得分 text_probs = logits_per_image.softmax(dim=-1) print("标签概率:", text_probs)
</details>import torch from PIL import Image import open_clip model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32-quickgelu', pretrained='metaclip_400m') # 对于2.5B模型,在OpenCLIP中使用'metaclip_fullcc',或在本仓库中使用'metaclip_2_5b' image = preprocess(Image.open("docs/CLIP.png")).unsqueeze(0) text = open_clip.tokenize(["一张图表", "一只狗", "一只猫"]) with torch.no_grad(): image_features = model.encode_image(image) text_features = model.encode_text(text) image_features /= image_features.norm(dim=-1, keepdim=True) text_features /= text_features.norm(dim=-1, keepdim=True) text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1) print("标签概率:", text_probs)
所有MetaCLIP模型都遵循OpenAI CLIP的训练设置:我们希望在"CLIP时代的ImageNet"中恢复受控实验。具体来说,我们对所有模型配置使用OpenAI CLIP的quickgelu激活函数(这在较早版本的OpenCLIP中缺失,主要使用nn.GELU代替)。我们在本仓库中添加了ViT-B-16-quickgelu、ViT-L-14-quickgelu、ViT-H-14-quickgelu和ViT-bigG-14-quickgelu。
模型名称 | 预训练 | 数据卡 | 已见图像-文本对数量 | 分辨率 | GPU数量 | IN零样本准确率 |
|---|---|---|---|---|---|---|
ViT-B-32-quickgelu | metaclip_400m | 数据卡 | 128亿 | 224 | 64 x V100 | 65.5 |
ViT-B-16-quickgelu | metaclip_400m | 数据卡 | 128亿 | 224 | 64 x V100 | 70.8 |
ViT-L-14-quickgelu | metaclip_400m | 数据卡 | 128亿 | 224 | 128 x V100 | 76.2 |
ViT-B-32-quickgelu | metaclip_2_5b | 数据卡 | 128亿 | 224 | 64 x V100 | 67.6 |
ViT-B-16-quickgelu | metaclip_2_5b | 数据卡 | 128亿 | 224 | 64 x V100 | 72.1 |
ViT-L-14-quickgelu | metaclip_2_5b | 数据卡 | 128亿 | 224 | 128 x V100 | 79.2 |
ViT-H-14-quickgelu | metaclip_2_5b | 数据卡 | 128亿 | 224 | 256 x A100 | 80.5 |
ViT-bigG-14-quickgelu | metaclip_2_5b | 数据卡 | 128亿 | 224 | 256 x A100 | 82.1 |
此代码是基于 OpenCLIP 定制的,将单独维护用于 MetaCLIP 的研究。以下命令应安装 OpenCLIP 的requirements和本仓库使用的 submitit=1.2.1:
conda create -n metaclip python=3.10 pytorch torchvision pytorch-cuda=11.7 tqdm ftfy braceexpand regex pandas submitit=1.2.1 \ -c pytorch-nightly \ -c nvidia \ -c conda-forge \ -c anaconda
MetaCLIP 使用 500,000 个查询作为元数据,将训练数据与维基百科/WordNet 术语的高质量写作分布对齐。这些元数据还允许我们将已发布模型的训练数据分布作为数据卡发布。
我们有一个演示笔记本来展示提出的算法如何工作。
CLIP 整理仍然可以作为在线平衡帮助(论文中的表 6)。我们将 CLIP 整理包装在两个关键函数中:子串匹配(建议离线运行)和平衡(可离线或在线,请查看 metaclip.balancing:main)。
import json import numpy as np from metaclip.substr_matching import substr_matching from metaclip.balancing import balance_sampling with open("metadata.json") as f: metadata = json.load(f) # 我们的 1.6B(池) -> 400M(整理)的条目计数;请检查 balance_sampling:main 并在您自己的数据上进行子串匹配和计数。 with open("metaclip/entry_counts_400m.json") as f: entry_count_json = json.load(f) entry_count = np.array([entry_count_json[entry] for entry in metadata], dtype=np.uint64) # 使用 uint64 以确保安全缩放。 t = 20000 entry_count[entry_count < t] = t entry_prob = t / entry_count for text in ["jacksons chameleon", "battery plate"]: matched_entry_ids = substr_matching(text, metadata) # 这是为了演示目的重新进行子串匹配;请参阅 metaclip/README.md。 curation_prob = min(entry_prob[matched_entry_ids].sum(), 1.0) curated = balance_sampling(matched_entry_ids, entry_prob) print(f"[curation_prob={curation_prob:.3f}, curated={curated}] {text}")
我们发布了一个用于从 CommonCrawl WAT 或 WARC 进行子串匹配和平衡的骨架代码。详情请查看这里。
算法的 Numpy 实现可以在 metaclip.pipeline 中找到,接近论文中使用的实现。
python submitit_openclip.py b32_400m
请在 run_configs_400m.py 中配置相应的 training_data。
考虑从我们用于构建 CLIP 的 500k 元数据的代码开始。
如果您有任何与代码或论 文相关的问题,请随时发邮件给 Hu Xu(huxu@meta.com)。
如果 MetaCLIP 对您的工作有帮助,请引用我们的论文(被 ICLR2024 接受为聚光报告):
@inproceedings{xu2023metaclip, title={Demystifying CLIP Data}, author={Hu Xu, Saining Xie, Xiaoqing Ellen Tan, Po-Yao Huang, Russell Howes, Vasu Sharma, Shang-Wen Li, Gargi Ghosh, Luke Zettlemoyer and Christoph Feichtenhofer}, journal={arXiv preprint arXiv:2309.16671}, year={2023} }
训练代码基于 OpenCLIP 开发,修改为原始 CLIP 训练设置。
MetaCLIP 的大部分内容均根据 CC-BY-NC 许可,但项目的某些部分可根据单独的许可条款使用:open_clip 根据 https://github.com/mlfoundations/open_clip 许可证授权。
我们衷心感谢 OpenCLIP 团队提供初始 CLIP 代码库和集成,以及 NielsRogge 将其集成到 Huggingface 中。