ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

CLIP 安装与使用教程:图文匹配、首次运行与零样本分类

CLIP 安装与使用教程:图文匹配、首次运行与零样本分类 CLIP 安装与使用教程图文匹配、首次运行与零样本分类【免费下载链接】CLIPCLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image项目地址: https://gitcode.com/GitHub_Trending/cl/CLIP给定一张图片与几条文字描述如何判断哪条最贴切CLIP 把图像和文本编码到同一向量空间按相似度打分即可完成零样本匹配。本文覆盖 CLIP 安装步骤、首次运行验证与基础使用方法。CLIP 原理速览图文对比学习如何工作CLIP 在大量图像文本配对数据上做对比预训练图像编码器与文本编码器各自把输入压成向量正对样本相互拉近、负对样本相互推远。推理时无需针对具体任务重新训练——把候选描述送入文本编码器与图像向量计算余弦相似度得分最高者即为最匹配项。CLIP 安装步骤与依赖配置环境要求Python 3.6、PyTorch 1.7.1。以下流程跨平台通用GPU 相关差异以条件分支说明。创建并激活独立环境避免依赖冲突conda create -n clip-env python3.8 -y conda activate clip-env安装 PyTorch。若机器带 CUDA GPUconda install -c pytorch pytorch torchvision cudatoolkit11.3 -y若为纯 CPU 环境含 macOS 默认环境改用pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu安装其余 Python 依赖与 requirements.txt 一致pip install ftfy regex tqdm packaging克隆仓库并以包形式安装。若你是 Windows 或 macOS 用户命令相同git clone https://gitcode.com/GitHub_Trending/cl/CLIP cd CLIP pip install .场景关键差异CUDA GPU步骤 2 用conda安装带cudatoolkit的 PyTorch纯 CPU / macOS步骤 2 用pip安装cpu轮子无需 cudatoolkit离线环境先手动放置权重至~/.cache/clip/再执行pip install .CLIP 首次运行验证最小可运行代码保存为verify.py并执行。首次运行会自动下载ViT-B/32权重数百 MB请预留时间。import torch import clip from PIL import Image device cuda if torch.cuda.is_available() else cpu model, preprocess clip.load(ViT-B/32, devicedevice) image preprocess(Image.open(CLIP.png)).unsqueeze(0).to(device) text clip.tokenize([a diagram, a dog, a cat]).to(device) with torch.no_grad(): logits_per_image, _ model(image, text) probs logits_per_image.softmax(dim-1).cpu().numpy() print(probs) # 类似 [[0.9928 0.0042 0.003]]预期输出一个长度为 3 的概率数组第一项对应 a diagram接近 1说明图片被正确判定为示意图。若三项均分或报错检查模型是否加载成功。CLIP 零样本分类实战对一张本地图像在若干候选标签中选出最匹配项完整演示encode_image/encode_text与相似度计算import torch import clip from PIL import Image device cuda if torch.cuda.is_available() else cpu model, preprocess clip.load(ViT-B/32, devicedevice) image preprocess(Image.open(CLIP.png)).unsqueeze(0).to(device) labels [a diagram, a photo of a dog, a photo of a cat, a screenshot] text torch.cat([clip.tokenize(t) for t in labels]).to(device) with torch.no_grad(): img_f model.encode_image(image) txt_f model.encode_text(text) # L2 归一化后计算余弦相似度乘 100 后 softmax 取 top-1 img_f / img_f.norm(dim-1, keepdimTrue) txt_f / txt_f.norm(dim-1, keepdimTrue) sim (100.0 * img_f txt_f.T).softmax(dim-1) print(labels[sim.argmax().item()]) # a diagram延伸方向换用 RN50 等更小模型做批量检索或参考 Prompt_Engineering_for_ImageNet.ipynb 对比不同提示词对准确率的影响。CLIP 常见报错速查表错误现象原因解决ImportError: No module named torch未装 PyTorch 或版本不匹配按上文条件分支重装torch与torchvision首次运行长时间卡住模型权重正从远程下载配置网络代理或手动放置权重到~/.cache/clip/CUDA out of memory显存不足换更小模型如RN50或减小输入尺寸OSError: unable to open file (CLIP.png)图片路径不存在确认图片与脚本同目录或改用绝对路径pip install .报依赖缺失基础依赖未装全先执行pip install ftfy regex tqdm packagingCLIP 资源导航与进阶路径官方文档与 APIREADME.md核心加载 / 分词逻辑clip/clip.py模型结构与权重说明clip/model.py模型卡片数据与局限model-card.md交互示例图文打分notebooks/Interacting_with_CLIP.ipynb一致性测试tests/test_consistency.py建议下一步先复现 零样本预测 的 CIFAR-100 案例再逐步把标签换成自己的业务文本验证相似度排序是否符合预期。【免费下载链接】CLIPCLIP (Contrastive Language-Image Pretraining), Predict the most relevant text snippet given an image项目地址: https://gitcode.com/GitHub_Trending/cl/CLIP创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表