x-clip

x-clip

灵活实现的CLIP视觉语言预训练模型

x-clip是一个简洁而全面的CLIP实现,整合了多项前沿研究成果。该项目支持灵活的模型配置,包括自定义文本和图像编码器、多视图对比学习和视觉自监督学习等功能。通过易用的API,研究人员可以快速实验各种CLIP变体和改进方案。x-clip适用于图像检索、跨模态理解等多种视觉语言任务。

CLIP对比学习视觉语言模型多模态深度学习Github开源项目

<img src="https://yellow-cdn.veclightyear.com/0a4dffa0/837dd3b7-2aea-43da-bf99-74dd5129f180.png" width="600px"></img>

<a href="https://discord.gg/xBPBXfcFHd"><img alt="加入我们的Discord" src="https://img.shields.io/discord/823813159592001537?color=5865F2&logo=discord&logoColor=white"></a>

x-clip

一个简洁但完整的<a href="https://openai.com/blog/clip/">CLIP</a>实现,包含了来自最近论文的各种实验性改进

安装

$ pip install x-clip

使用方法

import torch from x_clip import CLIP clip = CLIP( dim_text = 512, dim_image = 512, dim_latent = 512, num_text_tokens = 10000, text_enc_depth = 6, text_seq_len = 256, text_heads = 8, visual_enc_depth = 6, visual_image_size = 256, visual_patch_size = 32, visual_heads = 8, visual_patch_dropout = 0.5, # 图像块dropout概率,用于Kaiming He的FLIP中以节省计算并改善最终结果 - 0.5是一个好值,0.75是可接受的上限 use_all_token_embeds = False, # 是否使用细粒度对比学习(FILIP) decoupled_contrastive_learning = True, # 使用解耦对比学习(DCL)目标函数,从InfoNCE损失的分母中移除正样本对(CLOOB + DCL) extra_latent_projection = True, # 是否为文本到图像和图像到文本的比较使用单独的投影(CLOOB) use_visual_ssl = True, # 是否对图像进行自监督学习 use_mlm = False, # 对文本使用掩码语言学习(MLM)(DeCLIP) text_ssl_loss_weight = 0.05, # 文本MLM损失的权重 image_ssl_loss_weight = 0.05 # 图像自监督学习损失的权重 ) # 模拟数据 text = torch.randint(0, 10000, (4, 256)) images = torch.randn(4, 3, 256, 256) # 训练 loss = clip( text, images, freeze_image_encoder = False, # 如果使用预训练的图像网络,是否冻结图像编码器,由LiT论文提出 return_loss = True # 需要设置为True以返回对比损失 ) loss.backward()

你也可以传入外部的视觉transformer或残差网络。你只需确保你的图像编码器返回形状为batch x seq x dim的一组嵌入,并确保正确指定dim_image为返回嵌入的维度。以下是使用vit_pytorch中的视觉transformer的示例

$ pip install vit_pytorch>=0.25.6
import torch from x_clip import CLIP from vit_pytorch import ViT from vit_pytorch.extractor import Extractor base_vit = ViT( image_size = 256, patch_size = 32, num_classes = 1000, dim = 512, depth = 6, heads = 16, mlp_dim = 2048, dropout = 0.1, emb_dropout = 0.1 ) vit = Extractor( base_vit, return_embeddings_only = True ) clip = CLIP( image_encoder = vit, dim_image = 512, # 必须设置为与上面的视觉transformer相同的维度 dim_text = 512, dim_latent = 512, num_text_tokens = 10000, text_enc_depth = 6, text_seq_len = 256, text_heads = 8 ) text = torch.randint(0, 10000, (4, 256)) images = torch.randn(4, 3, 256, 256) loss = clip(text, images, return_loss = True) loss.backward()

最后,也可以外部定义文本transformer。目前,它需要返回包括CLS令牌在内的嵌入。

import torch from x_clip import CLIP, TextTransformer from vit_pytorch import ViT from vit_pytorch.extractor import Extractor base_vit = ViT( image_size = 256, patch_size = 32, num_classes = 1000, dim = 512, depth = 6, heads = 16, mlp_dim = 2048, dropout = 0.1, emb_dropout = 0.1 ) image_encoder = Extractor( base_vit, return_embeddings_only = True ) text_encoder = TextTransformer( dim = 512, num_tokens = 10000, max_seq_len = 256, depth = 6, heads = 8 ) clip = CLIP( image_encoder = image_encoder, text_encoder = text_encoder, dim_image = 512, dim_text = 512, dim_latent = 512 ) text = torch.randint(0, 10000, (4, 256)) images = torch.randn(4, 3, 256, 256) loss = clip(text, images, return_loss = True) loss.backward()

多视图对比学习损失

本仓库还支持多视图对比学习损失,如<a href="https://arxiv.org/abs/2110.05208">DeCLIP</a>中提出的。只需传入增强的文本和/或增强的图像,它就会自动计算,并按初始化时设置的multiview_loss_weight进行加权。 例如:

import torch from x_clip import CLIP, TextTransformer from vit_pytorch import ViT from vit_pytorch.extractor import Extractor base_vit = ViT( image_size = 256, patch_size = 32, num_classes = 1000, dim = 512, depth = 6, heads = 16, mlp_dim = 2048, dropout = 0.1, emb_dropout = 0.1 ) image_encoder = Extractor( base_vit, return_embeddings_only = True ) text_encoder = TextTransformer( dim = 512, num_tokens = 10000, max_seq_len = 256 + 1, depth = 6, heads = 8 ) clip = CLIP( image_encoder = image_encoder, text_encoder = text_encoder, dim_image = 512, dim_text = 512, dim_latent = 512, extra_latent_projection = True, multiview_loss_weight = 0.1 # 将多视图对比损失的权重设为0.1 ) text = torch.randint(0, 10000, (4, 256)) images = torch.randn(4, 3, 256, 256) aug_text = torch.randint(0, 10000, (4, 256)) # 增强文本(回译或EDA),与text维度相同 aug_images = torch.randn(4, 3, 256, 256) # 增强图像,与上面的images维度相同 loss = clip( text, images, aug_text = aug_text, # 传入增强文本 aug_image = aug_images, # 传入增强图像 return_loss = True, freeze_image_encoder = True ) loss.backward()

你甚至可以传入多个增强文本或图像

# ... aug_texts = ( torch.randint(0, 10000, (4, 256)), torch.randint(0, 10000, (4, 256)), ) aug_images = ( torch.randn(4, 3, 256, 256), torch.randn(4, 3, 256, 256), ) loss = clip( text, images, aug_text = aug_texts, aug_image = aug_images, return_loss = True, freeze_image_encoder = True ) loss.backward()

自定义视觉自监督学习模块

你可以通过visual_ssl关键字传入自己的视觉自监督学习模块,如下所示:

import torch from x_clip import CLIP from x_clip.visual_ssl import SimSiam from vit_pytorch import ViT from vit_pytorch.extractor import Extractor base_vit = ViT( image_size = 256, patch_size = 32, num_classes = 1000, dim = 512, depth = 6, heads = 16, mlp_dim = 2048, dropout = 0.1, emb_dropout = 0.1 ) image_encoder = Extractor( base_vit, return_embeddings_only = True ) visual_ssl = SimSiam( # 外部定义的SimSiam - 需要是一个接受与CLIP相同维度图像并返回标量损失的模块 image_encoder, image_size = 256, hidden_layer = -1 ) clip = CLIP( image_encoder = image_encoder, dim_image = 512, dim_text = 512, dim_latent = 512, use_mlm = True, visual_ssl = visual_ssl, # SSL模块传入CLIP use_all_token_embeds = False, extra_latent_projection = False, mlm_random_token_prob = 0.1 ) text = torch.randint(0, 10000, (4, 256)) images = torch.randn(4, 3, 256, 256) loss = clip(text, images, return_loss = True) loss.backward()

引用

@misc{radford2021learning, title = {Learning Transferable Visual Models From Natural Language Supervision}, author = {Alec Radford and Jong Wook Kim and Chris Hallacy and Aditya Ramesh and Gabriel Goh and Sandhini Agarwal and Girish Sastry and Amanda Askell and Pamela Mishkin and Jack Clark and Gretchen Krueger and Ilya Sutskever}, year = {2021}, eprint = {2103.00020}, archivePrefix = {arXiv}, primaryClass = {cs.CV} }
@misc{yao2021filip, title = {FILIP: Fine-grained Interactive Language-Image Pre-Training}, author = {Lewei Yao and Runhui Huang and Lu Hou and Guansong Lu and Minzhe Niu and Hang Xu and Xiaodan Liang and Zhenguo Li and Xin Jiang and Chunjing Xu}, year = {2021}, eprint = {2111.07783}, archivePrefix = {arXiv}, primaryClass = {cs.CV} }
@misc{fürst2021cloob, title = {CLOOB: Modern Hopfield Networks with InfoLOOB Outperform CLIP}, author = {Andreas Fürst and Elisabeth Rumetshofer and Viet Tran and Hubert Ramsauer and Fei Tang and Johannes Lehner and David Kreil and Michael Kopp and Günter Klambauer and Angela Bitto-Nemling and Sepp Hochreiter}, year = {2021}, eprint = {2110.11316}, archivePrefix = {arXiv}, primaryClass = {cs.LG} }
@misc{yeh2021decoupled, title = {解耦对比学习}, author = {叶骏晓 and 洪承耀 and 许彦齐 and 刘庭伦 and 陈宇北 and Yann LeCun}, year = {2021}, eprint = {2110.06848}, archivePrefix = {arXiv}, primaryClass = {cs.LG} }
@misc{zhai2021lit, title = {LiT: 使用锁定图像文本微调进行零样本迁移}, author = {翟晓华 and 王笑 and Basil Mustafa and Andreas Steiner and Daniel Keysers and Alexander Kolesnikov and Lucas Beyer}, year = {2021}, eprint = {2111.07991}, archivePrefix = {arXiv}, primaryClass = {cs.CV} }
@misc{li2021supervision, title = {监督无处不在:一种数据高效的对比语言-图像预训练范式}, author = {李阳光 and 梁峰 and 赵立晨 and 崔宇峰 and 欧阳万里 and 邵静 and 于凤伟 and 颜俊杰}, year = {2021}, eprint = {2110.05208}, archivePrefix = {arXiv}, primaryClass = {cs.CV} }
@Article{mu2021slip, author = {Norman Mu and Alexander Kirillov and David Wagner and 谢赛宁}, title = {SLIP: 自监督遇上语言-图像预训练}, journal = {arXiv预印本 arXiv:2112.12750}, year = {2021}, }
@misc{su2021roformer, title = {RoFormer: 具有旋转位置嵌入的增强型Transformer}, author = {苏剑林 and 卢钰 and 潘胜峰 and 温博 and 刘云峰}, year = {2021}, eprint = {2104.09864}, archivePrefix = {arXiv}, primaryClass = {cs.CL} }
@inproceedings{anonymous2022normformer, title = {NormFormer: 通过额外归一化改进Transformer预训练}, author = {匿名}, booktitle = {提交至第十届国际学习表示会议}, year = {2022}, url = {https://openreview.net/forum?id=GMYWzWztDx5}, note = {审核中} }
@inproceedings{Li2022ScalingLP, title = {通过掩码扩展语言-图像预训练}, author = {李扬豪 and 范浩琦 and 胡荣航 and Christoph Feichtenhofer and 何恺明}, year = {2022} }
@article{Liu2022PatchDropoutEV, title = {PatchDropout: 使用补丁丢弃来节约视觉Transformer资源}, author = {刘悦 and Christos Matsoukas and Fredrik Strand and Hossein Azizpour and Kevin Smith}, journal = {ArXiv}, year = {2022}, volume = {abs/2208.07220} }
@misc{shi2023enhance, title = {通过表示相似性正则化增强音频生成的可控性}, author = {石阳阳 and Gael Le Lan and Varun Nagaraja and 倪昭恒 and 梅鑫浩 and 张义 and Forrest Iandola and 刘洋 and Vikas Chandra}, year = {2023}, eprint = {2309.08773}, archivePrefix = {arXiv}, primaryClass = {cs.SD} }

编辑推荐精选

iTerms

iTerms

企业专属的AI法律顾问

iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。

SimilarWeb流量提升

SimilarWeb流量提升

稳定高效的流量提升解决方案,助力品牌曝光

稳定高效的流量提升解决方案,助力品牌曝光

Sora2视频免费生成

Sora2视频免费生成

最新版Sora2模型免费使用,一键生成无水印视频

最新版Sora2模型免费使用,一键生成无水印视频

Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
TRAE编程

TRAE编程

AI辅助编程,代码自动修复

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
商汤小浣熊

商汤小浣熊

最强AI数据分析助手

小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。

imini AI

imini AI

像人一样思考的AI智能体

imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。

Keevx

Keevx

AI数字人视频创作平台

Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。

即梦AI

即梦AI

一站式AI创作平台

提供 AI 驱动的图片、视频生成及数字人等功能,助力创意创作

下拉加载更多