惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
P
Palo Alto Networks Blog
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
aimingoo的专栏
aimingoo的专栏
腾讯CDC
T
Threatpost
D
DataBreaches.Net
Vercel News
Vercel News
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
U
Unit 42
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
O
OpenAI News
量子位
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Visual Studio Blog
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
S
Security Affairs
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
S
SegmentFault 最新的问题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
I
Intezer
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
博客园_首页
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

StudyingLover's Blog

Diffusion Policy笔记 rwkv笔记 act笔记 nanovllm-block_manager opencode多智能体 nanobot-pre-train nanobot-rl nanobot-sft nanobot-checkpoint_manager nanobot-gpt nanobot-mid-train Vision Mamba (Vim)笔记 BPE演示 最后一遍学习Transformer YOLOv5 目标检测笔记 下载根服务器解析记录 Dynaseal A Backend-Controlled LLM API Key Distribution Scheme with Constrained Invocation Parameters 判断链表有环 王道25数据结构勘误 关于perplexity的open-sourcing-r1-1776 AI为什么不像人类一样进行多轮对话 新博客改造日记和功能测试 linuxqq只显示登陆背景图 数字设计和计算机体系结构(机械工业出版社)勘误(自制) Dynaseal:面向未来端侧llm agent的llm api key分发机制 A Definitive Guide to Markdown Style This post is using MDX, Where you can embed JSX and Astro components RT-Patch学习 pydantic实现的LLM ReAct fastapi 和 uvicorn 设置监听 ipv6 pydantic+openai+json 控制大模型输出的最佳范式 解决 Matplotlib Scatter 不支持 Marker 列表的问题:mscatter 实现 roofline model zhipuAI接口兼容openai 在docker部署fastapi宝塔里使用nginx反代套上cloudflare获取请求的真实ip clion搭建libbpf-bootstrap开发环境 coze+coze-discord-proxy+ChatNextWebUI实现AI自由 安卓内核时间使用的是UTC时间 colab运行google最新开源模型Gemma Sora技术报告 视频生成模型作为世界模拟器 笔记 archlinux flutter开发踩坑 fastapi集成google auth登录 linux下NTFS磁盘报错输入输出错误 Venn-Abers 预测器 基于Venn-Abers预测器的系统日志异常检测方法_顾兆军 手机平板远程访问kvm虚拟机的windows phi-2弱智吧测评 poe的gemini pro或是百度开发 google gemini api使用 google gemini api申请 构建用于复杂数据处理的高效UDP服务器和客户端 matplotlib中文字体渲染 TruFor笔记和代码复现 深入分析:GitHub Trending 项目 "multipleWindow3dScene" pua大模型 ggml教程|mnist手写体识别量化推理 xgboost2.0最佳实践 xgboost使用GPU最佳实践 马踏棋盘 cloudlflare推理llama2 docker搭建elasticsearch并使用python连接 FreeU-文字生成图片的免费午餐笔记 使用xgboost的c接口推理模型 Archlinux使用CMake调用xgboost的c接口 m2cgen生成机器学习c语言推理代码 xgboost模型序列化存储并推理 speculative-sampling笔记 prompt2model笔记 RoboTAP笔记 自建obsidian同步服务 MediaPipe即将推出图像生成服务 Dual-Stream Diffusion Net for Text-to-Video Generation笔记 ViT在DDPM取代UNet(DiT) arch4edu搞崩了我的flutter LISA(推理分割)笔记 在终端绘制GPU显存使用曲线 GPTBot介绍 arch蓝牙无法连接 GPU部署llama-cpp-python(llama.cpp通用) 花式求GCD 使用llama构建一个蜜罐(前端) 使用llama构建一个蜜罐(后端) llama-cpp-python快速上手 快速上手llama2.c(更新版) Paper Gestalt笔记 DINO-v2笔记 快速上手llama2.c AnyDoor笔记 Archlinux安装scrcpy加载共享库出错 error while loading shared libraries:libusb-1.0.so.0:wrong ELF class:ELFCLASS32 npc_gzip笔记 python调用c++函数 Filesystem type ntfs3,ntfs not configured in kernel open_clip编码图像和文本 PicGo配置CloudflareR2图片储存 ArchlinuxGnome快捷键打开终端 clip-interrogator代码解析 GroundingDINO安装报错解决 2023华为鲲鹏畅想日暨西安高新国际会议中心零食午饭测评 RoboMaster开源仓库汇总(长期更新) 没有手都可以在腾讯云创建镜像
clip_interrogator教程
About the Author StudyingLover · 2023-04-23 · via StudyingLover's Blog

clip_interrogator教程

文字生成图片是近年来多模态和大模型研究的热门方向,openai提出的CLIP提供了一个方法建立起了图片和文字的联系,但是只能做到给定一张图片选择给定文本语义最相近的那一个,实际项目开发中我们总是需要从一张图片获取描述,感谢社区的活力,clip-interrogator应运而生。

受限于clip-interrogator 等于没有的文档,就有了这篇文章来写一些怎么使用clip-interrogator。

clip-interrogator项目地址GitHub

在线体验huggingface-clip-interrogator huggingface-clip-interrogator2

clip-interrogator原理

首先,clip-interrogator会使用BILP生成一段对图片的自然语言描述。

接下来会根据四种模式,从data文件夹下的txt文件中组合出文字生成图片常用的prompt,通过CLIP进行编码,然后将图片也用CLIP进行编码,计算出相似度最大的一组prompt,和BILP生成的prompt拼接到一起,就得到了一组prompt。

安装

pip install clip-interrogator==0.5.4

如果需要BLIP2最新的WIP支持,运行

pip install clip-interrogator==0.6.0

使用

快速开始

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(clip_model_name="ViT-L-14/openai"))
print(ci.interrogate(image))

image_path 换成自己图片的路径即可

模型

BLIP

BLIP可以传入两种选项,largebase,默认使用large

base用法是

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(caption_model_name='blip-base',clip_model_name="RN50-quickgelu/openai"))
print(ci.interrogate_fast(image))

CLIP

这里使用的模型的是openai的ViT-L-14。

我们也可以更改模型,文档在这完全没说清可以用什么,我做了试错

报错显示可用的模型有'coca_base', 'coca_roberta-ViT-B-32', 'coca_ViT-B-32', 'coca_ViT-L-14', 'convnext_base', 'convnext_base_w', 'convnext_base_w_320', 'convnext_large', 'convnext_large_d', 'convnext_large_d_320', 'convnext_small', 'convnext_tiny', 'convnext_xlarge', 'convnext_xxlarge', 'convnext_xxlarge_320', 'mt5-base-ViT-B-32', 'mt5-xl-ViT-H-14', 'RN50', 'RN50-quickgelu', 'RN50x4', 'RN50x16', 'RN50x64', 'RN101', 'RN101-quickgelu', 'roberta-ViT-B-32', 'swin_base_patch4_window7_224', 'ViT-B-16', 'ViT-B-16-plus', 'ViT-B-16-plus-240', 'ViT-B-32', 'ViT-B-32-plus-256', 'ViT-B-32-quickgelu', 'ViT-bigG-14', 'ViT-e-14', 'ViT-g-14', 'ViT-H-14', 'ViT-H-16', 'ViT-L-14', 'ViT-L-14-280', 'ViT-L-14-336', 'ViT-L-16', 'ViT-L-16-320', 'ViT-M-16', 'ViT-M-16-alt', 'ViT-M-32', 'ViT-M-32-alt', 'ViT-S-16', 'ViT-S-16-alt', 'ViT-S-32', 'ViT-S-32-alt', 'vit_medium_patch16_gap_256', 'vit_relpos_medium_patch16_cls_224', 'xlm-roberta-base-ViT-B-32', 'xlm-roberta-large-ViT-H-14'

这里其实是我一直没搞懂的一个地方,经过很多次试错,/ 前面被称为model,但是很多模型是用不了的,/ 后面被称作 tag (通过读源码猜测是预训练模型来源) ,是可以写不同的内容,例如openai ,有的时候还需要不填,但是究竟可以怎么组合一直没找到,下面做了一个总结,

模型tag
coca_base不传
RN50’openai’, ‘yfcc15m’, ‘cc12m’
RN50-quickgelu’openai’, ‘yfcc15m’, ‘cc12m’
RN101’openai’, ‘yfcc15m’
RN101-quickgelu’openai’, ‘yfcc15m’
RN50x4’openai’
RN50x16’openai’
RN50x64’openai’
ViT-B-32’openai’, ‘laion400m_e31’, ‘laion400m_e32’, ‘laion2b_e16’, ‘laion2b_s34b_b79k’
ViT-B-32-quickgelu’openai’, ‘laion400m_e31’, ‘laion400m_e32’
ViT-B-16’openai’, ‘laion400m_e31’, ‘laion400m_e32’, ‘laion2b_s34b_b88k’
ViT-L-14-336’openai’
ViT-S-32-alt不传
ViT-S-32不传
ViT-S-16-alt不传
ViT-S-16不传
ViT-M-32-alt不传
ViT-M-32不传
ViT-M-16-alt不传
ViT-M-16不传
xlm-roberta-base-ViT-B-32’laion5b_s13b_b90k’
xlm-roberta-large-ViT-H-14’frozen_laion5b_s13b_b90k’

不传的意思是不写/ 后面的部分不是只写模型名字,正确的用法例如coca_base/

例如使用RN50-quickgelu/openai 的用法就是ci = Interrogator(Config(clip_model_name="RN50-quickgelu/openai"))

文档中有这么一句ViT-L for Stable Diffusion 1, and ViT-H for Stable Diffusion 2,意思是 ViT-L 是给 Stable Diffusion 1 用的,ViT-H是给 Stable Diffusion 2 用的

模式

模式有bestclassicfastnegative 三种,开发者在这里的设计很奇怪,不同模式的使用不是传不同的参数而是使用不同的方法。best 模式就是上面的用法,fast 模式的用法是

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(clip_model_name="RN50-quickgelu/openai"))
print(ci.interrogate_fast(image))

classic 模式用法

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(clip_model_name="RN50-quickgelu/openai"))
print(ci.interrogate_classic(image))

negative 模式用法

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open(image_path).convert('RGB')
ci = Interrogator(Config(clip_model_name="RN50-quickgelu/openai"))
print(ci.interrogate_negative(image))

quiet

quiet 选项的作用是不输出中间过程,使用方法是直接写进Config 即可 ,例如

from PIL import Image
from clip_interrogator import Config, Interrogator
image = Image.open('/content/test.png').convert('RGB')
ci = Interrogator(Config(clip_model_name="RN50-quickgelu/openai",quiet=True))
print(ci.interrogate_fast(image))

使用前,会有各种进度条 image.png

使用后,所有过程中的输出会被隐藏 image.png

自定义词库

如果你安装的是0.6.0,那么可以使用自定义词库

from clip_interrogator import Config, Interrogator, LabelTable, load_list
from PIL import Image

ci = Interrogator(Config(blip_model_type=None))
image = Image.open(image_path).convert('RGB')
table = LabelTable(load_list('terms.txt'), 'terms', ci)
best_match = table.rank(ci.image_to_features(image), top_count=1)[0]
print(best_match)