🚀 Long-CLIP微調項目
本項目是對Long-CLIP的微調版本,原模型為 BeichenZhang/LongCLIP-L。該微調項目旨在提升模型在特定任務上的性能,為圖像和文本的交互提供更強大的支持。
✨ 主要特性
- 數據集豐富:使用了
SPRIGHT-T2I/spright_coco
數據集進行微調,提升模型的泛化能力。
- 性能提升:微調後的模型在 ImageNet/ObjectNet 上的準確率達到了 0.89,相比原模型的約 0.81 有顯著提升。
- 自定義損失:採用了帶有標籤平滑的自定義損失函數,在不同規模數據集上都有良好表現。
- 幾何參數化:運用 Geometric Parametrization (GmP) 方法,優化模型的權重表示。
📦 安裝指南
文檔中未提及具體安裝步驟,可參考作者 GitHub 倉庫 https://github.com/zer0int/Long-CLIP 中的代碼進行安裝和微調。
💻 使用示例
基礎用法
以下是使用 HuggingFace Transformers 加載模型的示例:
model_id = "zer0int/LongCLIP-GmP-ViT-L-14"
model = CLIPModel.from_pretrained(model_id)
processor = CLIPProcessor.from_pretrained(model_id)
高級用法
處理 77 個 token 的情況
CLIPModel.from_pretrained(model_id, ignore_mismatched_sizes=True)
tensor([[0.16484, 0.0749, 0.1618, 0.0774]], device='cuda:0') 📉
處理 248 個 token 的情況(推薦)
model_id = ("zer0int/LongCLIP-GmP-ViT-L-14")
config = CLIPConfig.from_pretrained(model_id)
config.text_config.max_position_embeddings = 248
clip_model = CLIPModel.from_pretrained(model_id, torch_dtype=dtype, config=config)
clip_processor = CLIPProcessor.from_pretrained(model_id, padding="max_length", max_length=248)
pipe.tokenizer = clip_processor.tokenizer
pipe.text_encoder = clip_model.text_model
pipe.tokenizer_max_length = 248
pipe.text_encoder.dtype = torch.bfloat16
tensor([[0.2128, 0.0978, 0.1957, 0.1133]], device='cuda:0') ✅
📚 詳細文檔
使用 Long-CLIP 作為文本編碼器
若要將 Long-CLIP 作為 Flux.1、SDXL、Stable Diffusion 的文本編碼器,可從 https://github.com/SeaArtLab/ComfyUI-Long-CLIP 獲取 ComfyUI Long-CLIP 節點。若不使用 Comfy,該倉庫也可作為逆向工程和應用到自己代碼中的起點。
HuggingFace Transformers 加載注意事項
在使用 HuggingFace Transformers 加載模型時,會遇到與庫中定義的 77 個 token 不匹配的問題,可參考以下兩種解決方案:
- 方案一(簡單但效果較差):截斷到 77 個 token。
- 方案二(推薦):實現 248 個 token 的處理,具體實現可參考上述高級用法示例。
模型更新
2024 年 8 月 12 日更新:推出新的 BEST 模型,採用帶有標籤平滑的自定義損失函數。在多樣化、大規模高質量數據集上有小幅提升,在易過擬合的微調場景(如小批量、單 GPU、窄數據集,如 'sneakers' 等)中有較大相對提升。可使用提供的 GmP-Smooth 代碼在 https://github.com/zer0int/Long-CLIP 上微調模型。
🔧 技術細節
幾何參數化 (GmP)
本項目使用 Geometric Parametrization (GmP) 方法對模型的 MLP 層進行優化。傳統的 CLIP MLP 層使用線性變換,而 GmP 將權重分解為徑向分量 'r'(預訓練權重的範數)和角度分量 'theta'(歸一化方向),從而保留權重向量的方向性和大小。
"Normal" CLIP MLP (multi-layer perceptron):
(mlp): Sequential(
|-(c_fc): Linear(in_features=1024, out_features=4096, bias=True)
| (gelu): QuickGELU()
|-}-(c_proj): Linear(in_features=4096, out_features=1024, bias=True)
| |
| |-- visual.transformer.resblocks.0.mlp.c_fc.weight
| |-- visual.transformer.resblocks.0.mlp.c_fc.bias
|
|---- visual.transformer.resblocks.0.mlp.c_proj.weight
|---- visual.transformer.resblocks.0.mlp.c_proj.bias
GmP CLIP MLP:
Weight decomposition into:
- radial component 'r' as norm of pre-trained weights
- angular component 'theta' as normalized direction
-> preserves weight vectors' directionality and magnitude
(mlp): Sequential(
|-(c_fc): GeometricLinear()
| (gelu): QuickGELU()
|-}-(c_proj): GeometricLinear()
| |
| |-- visual.transformer.resblocks.0.mlp.c_fc.r
| |-- visual.transformer.resblocks.0.mlp.c_fc.theta
| |-- visual.transformer.resblocks.0.mlp.c_fc.bias
|
|---- visual.transformer.resblocks.0.mlp.c_proj.r
|---- visual.transformer.resblocks.0.mlp.c_proj.theta
|---- visual.transformer.resblocks.0.mlp.c_proj.bias
(Same thing for [text] transformer.resblocks)
📄 許可證
預訓練的 CLIP 模型由 OpenAI 提供,遵循 MIT License。
引用信息
@article{zhang2024longclip,
title={Long-CLIP: Unlocking the Long-Text Capability of CLIP},
author={Beichen Zhang and Pan Zhang and Xiaoyi Dong and Yuhang Zang and Jiaqi Wang},
journal={arXiv preprint arXiv:2403.15378},
year={2024}
}
支持與貢獻
重要提示
⚠️ 重要提示
在使用 HuggingFace Transformers 加載模型時,會遇到與庫中定義的 77 個 token 不匹配的問題,可參考上述提供的解決方案。
💡 使用建議
推薦使用 248 個 token 的處理方案,該方案在性能上表現更優。