模型概述
模型特點
模型能力
使用案例
🚀 CLIP - convnext_large_d_320.laion2B - s29B - b131K - ft模型卡
CLIP - convnext_large_d_320.laion2B - s29B - b131K - ft是基於Transformer架構的零樣本圖像分類模型,可用於圖像和文本檢索、圖像生成引導等任務,為研究人員提供了探索零樣本圖像分類的工具。
🚀 快速開始
本模型主要用於零樣本圖像分類、圖像和文本檢索等任務。如果你想使用該模型進行零樣本圖像分類,可以參考以下步驟:
- 確保你已經安裝了
open_clip
庫。 - 加載模型並進行推理。
以下是一個簡單的示例代碼:
import open_clip
import torch
from PIL import Image
# 加載模型和預處理函數
model, _, preprocess = open_clip.create_model_and_transforms('convnext_large_d_320.laion2B-s29B-b131K-ft')
tokenizer = open_clip.get_tokenizer('convnext_large_d_320.laion2B-s29B-b131K-ft')
# 加載圖像
image = preprocess(Image.open("your_image.jpg")).unsqueeze(0)
text = tokenizer(["a photo of a cat", "a photo of a dog"])
# 進行推理
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)
logits_per_image, logits_per_text = model(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
print("Label probs:", probs)
✨ 主要特性
- 多模態處理:能夠處理圖像和文本兩種模態的數據,實現零樣本圖像分類和圖像文本檢索等任務。
- 高效性能:在320x320分辨率下,ConvNext - Large - D模型比OpenAI微調的L/14 - 336模型更高效,具有更少的GMAC、激活值和參數。
- 廣泛適用性:可用於多種下游任務,如圖像分類微調、線性探針圖像分類、圖像生成引導等。
📦 安裝指南
要使用本模型,你需要安裝open_clip
庫。可以使用以下命令進行安裝:
pip install open_clip_torch
💻 使用示例
基礎用法
以下是一個使用本模型進行零樣本圖像分類的基礎示例:
import open_clip
import torch
from PIL import Image
# 加載模型和預處理函數
model, _, preprocess = open_clip.create_model_and_transforms('convnext_large_d_320.laion2B-s29B-b131K-ft')
tokenizer = open_clip.get_tokenizer('convnext_large_d_320.laion2B-s29B-b131K-ft')
# 加載圖像
image = preprocess(Image.open("your_image.jpg")).unsqueeze(0)
text = tokenizer(["a photo of a cat", "a photo of a dog"])
# 進行推理
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)
logits_per_image, logits_per_text = model(image, text)
probs = logits_per_image.softmax(dim=-1).cpu().numpy()
print("Label probs:", probs)
高級用法
如果你想使用本模型進行圖像和文本檢索,可以參考以下示例:
import open_clip
import torch
from PIL import Image
import numpy as np
# 加載模型和預處理函數
model, _, preprocess = open_clip.create_model_and_transforms('convnext_large_d_320.laion2B-s29B-b131K-ft')
tokenizer = open_clip.get_tokenizer('convnext_large_d_320.laion2B-s29B-b131K-ft')
# 加載圖像數據集
image_paths = ["image1.jpg", "image2.jpg", "image3.jpg"]
image_features_list = []
for path in image_paths:
image = preprocess(Image.open(path)).unsqueeze(0)
with torch.no_grad():
image_features = model.encode_image(image)
image_features /= image_features.norm(dim=-1, keepdim=True)
image_features_list.append(image_features.cpu().numpy())
image_features_matrix = np.concatenate(image_features_list, axis=0)
# 輸入查詢文本
query_text = tokenizer(["a photo of a beautiful landscape"])
with torch.no_grad():
text_features = model.encode_text(query_text)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_features = text_features.cpu().numpy()
# 計算相似度
similarities = np.dot(image_features_matrix, text_features.T).flatten()
sorted_indices = np.argsort(similarities)[::-1]
# 輸出檢索結果
for i in sorted_indices:
print(f"Image: {image_paths[i]}, Similarity: {similarities[i]}")
📚 詳細文檔
模型詳情
本模型是一系列基於CLIP的ConvNeXt - Large模型,使用OpenCLIP在LAION - 2B(英語)子集上進行訓練。具體特性如下:
- 圖像塔:使用[timm](https://github.com/rwightman/pytorch - image - models)的ConvNeXt - Large模型(
convnext_large
)。 - 視覺塔頭部:採用MLP(
fc - gelu - drop - fc
)頭部,而非其他CLIP模型的單一投影。 - 文本塔:寬度與ViT - L / RN50x16模型相同,但深度增加4層(深度為16,嵌入維度為768)。
本320x320分辨率的模型是對[CLIP - convnext_large_d.laion2B - s26B - b102K - augreg](https://huggingface.co/laion/CLIP - convnext_large_d.laion2B - s26B - b102K - augreg)的高分辨率微調版本。它在原始256x256訓練運行的最終檢查點基礎上,使用額外的約25億個樣本和較低的學習率進行微調。
在320x320分辨率下,ConvNext - Large - D模型比OpenAI微調的336x336的L/14模型更高效。L/14 - 336模型的GMAC是其2.5倍,激活值是2.8倍,參數是1.22倍。
模型 | 數據集 | 分辨率 | AugReg | ImageNet零樣本Top - 1準確率(%) |
---|---|---|---|---|
[convnext_large_d.laion2b_s26b_b102k - augreg](https://huggingface.co/laion/CLIP - convnext_large_d.laion2B - s26B - b102K - augreg) | LAION - 2B | 256x256 | RRC (0.33, 1.0), RE (0.35), SD (0.1), D(0.1) | 75.9 |
[convnext_large_d_320.laion2b_s29b_b131k - ft](https://huggingface.co/laion/CLIP - convnext_large_d_320.laion2B - s29B - b131K - ft) | LAION - 2B | 320x320 | RRC (0.5, 1.0), RE (0.4), SD (0.1), D(0.0) | 76.6 |
[convnext_large_d_320.laion2b_s29b_b131k - ft - soup](https://huggingface.co/laion/CLIP - convnext_large_d_320.laion2B - s29B - b131K - ft - soup) | LAION - 2B | 320x320 | RRC (0.5, 1.0), RE (0.4), SD (0.1), D(0.0) | 76.9 |
RRC = 隨機調整裁剪(裁剪百分比),RE = 隨機擦除(概率),SD = 隨機深度(概率) - 僅圖像塔,D = 丟棄(概率) - 僅圖像塔頭部。
LAION - A是LAION - 2B的約9億樣本子集,經過pHash去重和美學分數過濾。
模型訓練由Ross Wightman在stability.ai集群上完成。
使用場景
直接使用
- 零樣本圖像分類
- 圖像和文本檢索
下游使用
- 圖像分類和其他圖像任務微調
- 線性探針圖像分類
- 圖像生成引導和條件控制
不適用場景
- 部署使用:目前模型的任何部署用例(無論商業與否)都超出了適用範圍。除非對模型進行特定、固定類別的徹底領域測試,否則不建議在受限環境中進行圖像搜索等非部署用例。因為我們的安全評估表明,考慮到CLIP在不同類別分類法下性能的可變性,需要進行特定任務測試。未經測試和無約束地部署模型在任何用例中目前都可能有害。
- 監控和人臉識別:涉及監控和人臉識別領域的用例始終不在適用範圍內。因為目前缺乏確保公平使用的測試規範和檢查,使用人工智能進行此類任務可能為時過早。
- 非英語語言:由於模型僅在英語上進行訓練和評估,其使用應限於英語用例。
訓練詳情
訓練數據
本模型使用LAION - 2B進行訓練,它是LAION - 5B(https://laion.ai/blog/laion - 5b/)的20億樣本英語子集。
重要提示:創建該數據集的目的是推動大規模多模態模型訓練和處理從公共互聯網爬取的未整理大規模數據集的研究和實驗。因此,建議將數據集用於研究目的。請注意,該大規模數據集未經過整理,收集的鏈接可能會指向令人極度不適和不安的內容。因此,請謹慎使用演示鏈接並自行承擔風險。可以通過基於安全標籤過濾樣本(使用我們構建的自定義訓練的NSFW分類器)提取“安全”子集。雖然這大大降低了查看時遇到潛在有害內容的可能性,但我們不能完全排除安全模式下仍存在有害內容的可能性,因此警告仍然適用。我們認為,向廣泛的研究和其他感興趣的社區公開提供數據集,將有助於透明地研究訓練大規模模型帶來的好處以及使用封閉大型數據集時可能未報告或未注意到的陷阱和危險。然而,我們不建議使用該數據集創建現成的工業產品,因為關於此類大規模模型的一般屬性和安全性的基礎研究仍在進行中。
訓練過程
所有320x320模型的微調都使用全局批次大小為131072,在10 - 16個檢查點間隔(每個間隔2.037億個樣本)內進行,微調期間總共處理約20 - 30億個樣本。
對於320x320模型,在64個8 - GPU(A100 40GB)節點(Stability)上使用以下slurm腳本(帶srun):
/opt/slurm/sbin/srun --cpu_bind=v --accel-bind=gn python -m training.main \
--save-frequency 1 \
--name "convnext_large_320" \
--pretrained ""/runs/convnext_large_256/epoch_128.pt" \
--resume 'latest' \
--train-data="pipe:aws s3 cp s3://mybucket/path/{laion{00000..xxxxx}.tar -" \
--train-num-samples 203666042 \
--dataset-type webdataset \
--precision amp_bfloat16 \
--beta2 0.98 \
--warmup 2000 \
--batch-size=256 \
--epochs=12 \
--dataset-resampled \
--aug-cfg use_timm=True scale='(0.5, 1.0)' re_prob=0.4 \
--clip-grad-norm 5.0 \
--lr 5e-5 \
--workers=6 \
--model "convnext_large_d_320" \
--seed 0 \
--ddp-static-graph \
--local-loss \
--gather-with-grad \
--grad-checkpointing
評估
測試數據、因素和指標
測試數據
分類測試使用VTAB +(VTAB(https://arxiv.org/abs/1910.04867)與額外魯棒性數據集的組合),檢索測試使用COCO和Flickr。
結果
模型在ImageNet - 1k上的零樣本Top - 1準確率在75.9%至76.9%之間。
原始從頭開始的256x256訓練的零樣本曲線:
已在更廣泛的數據集上進行了初步基準測試,可在https://github.com/LAION - AI/CLIP_benchmark/blob/main/benchmark/results.ipynb查看。
🔧 技術細節
本模型基於CLIP架構,結合了ConvNeXt - Large模型的優勢。在圖像塔中使用了timm庫的ConvNeXt - Large模型,並採用MLP頭部進行特徵提取。文本塔在寬度不變的情況下增加了深度,以提高文本特徵的表示能力。在訓練過程中,使用了大規模的LAION - 2B數據集,並採用了隨機調整裁剪、隨機擦除等數據增強技術。在微調過程中,使用了較低的學習率和較大的全局批次大小,以提高模型的性能。
📄 許可證
本模型使用MIT許可證。
致謝
感謝stability.ai提供訓練本模型所需的計算資源。
引用
BibTeX:
LAION - 5B
@inproceedings{schuhmann2022laionb,
title={{LAION}-5B: An open large-scale dataset for training next generation image-text models},
author={Christoph Schuhmann and
Romain Beaumont and
Richard Vencu and
Cade W Gordon and
Ross Wightman and
Mehdi Cherti and
Theo Coombes and
Aarush Katta and
Clayton Mullis and
Mitchell Wortsman and
Patrick Schramowski and
Srivatsa R Kundurthy and
Katherine Crowson and
Ludwig Schmidt and
Robert Kaczmarczyk and
Jenia Jitsev},
booktitle={Thirty-sixth Conference on Neural Information Processing Systems Datasets and Benchmarks Track},
year={2022},
url={https://openreview.net/forum?id=M3Y74vmsMcY}
}
OpenCLIP軟件
@software{ilharco_gabriel_2021_5143773,
author = {Ilharco, Gabriel and
Wortsman, Mitchell and
Wightman, Ross and
Gordon, Cade and
Carlini, Nicholas and
Taori, Rohan and
Dave, Achal and
Shankar, Vaishaal and
Namkoong, Hongseok and
Miller, John and
Hajishirzi, Hannaneh and
Farhadi, Ali and
Schmidt, Ludwig},
title = {OpenCLIP},
month = jul,
year = 2021,
note = {If you use this software, please cite it as below.},
publisher = {Zenodo},
version = {0.1},
doi = {10.5281/zenodo.5143773},
url = {https://doi.org/10.5281/zenodo.5143773}
}
OpenAI CLIP論文
@inproceedings{Radford2021LearningTV,
title={Learning Transferable Visual Models From Natural Language Supervision},
author={Alec Radford and Jong Wook Kim and Chris Hallacy and A. 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},
booktitle={ICML},
year={2021}
}
@Article{liu2022convnet,
author = {Zhuang Liu and Hanzi Mao and Chao-Yuan Wu and Christoph Feichtenhofer and Trevor Darrell and Saining Xie},
title = {A ConvNet for the 2020s},
journal = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
year = {2022},
}
@misc{rw2019timm,
author = {Ross Wightman},
title = {PyTorch Image Models},
year = {2019},
publisher = {GitHub},
journal = {GitHub repository},
doi = {10.5281/zenodo.4414861},
howpublished = {\url{https://github.com/rwightman/pytorch-image-models}}
}









