什麼是 VLM 適配器?為何要自己訓練?

視覺語言模型(Vision-Language Model,簡稱 VLM)是一種能夠同時理解圖像和文字的 AI 模型。傳統上,訓練一個完整的 VLM 需要大量 GPU 資源和時間。但現在有一個更聰明的方法:將小型語言模型微調為 VLM 的適配器

這種方法的核心概念是冻结大型視覺編碼器(Vision Encoder),只針對連接圖像和文字的適配器層進行訓練。這樣做有幾個優勢:

  • 訓練成本大幅降低:只需訓練少量參數,顯卡需求從數十GB降至 8-16GB
  • 速度快:本地端訓練可在數小時內完成
  • 靈活性高:可根據特定任務客製化模型能力

本地端環境準備與需求

在開始訓練前,你需要確保本地環境滿足以下基本需求:

硬體需求

  • 顯示卡:NVIDIA RTX 3090/4090 或同等級以上,建議 16GB VRAM
  • 記憶體:至少 32GB RAM
  • 儲存空間:至少 100GB SSD 空間(用於模型和數據集)

軟體環境

  • Python 3.10+
  • CUDA 12.1+
  • PyTorch 2.0+
  • Transformers 庫

安裝關鍵依賴的指令:

pip install torch transformers accelerate peft bitsandbytes
pip install pillow pandas datasets

選擇基礎模型:小型語言模型的選型考量

選擇合適的小型語言模型是成功的關鍵。以下是幾個推薦的開源選項:

  • LLaMA 3 8B:效能與資源消耗的絕佳平衡
  • Qwen 1.8B/7B:對中文支援優異
  • Phi-3 Mini:微軟出品,體積極小但能力強

搭配的視覺編碼器建議使用:

  • CLIP ViT-L/14:穩定可靠,開源生態完善
  • SigLIP:更新穎,表現有時更佳

實務上,建議從 Qwen2-VL 2B + LLaMA 3 8B 的組合開始,這個配置在 16GB 顯卡上可以順利訓練。

訓練流程:五個步驟完成適配器微調

步驟一:準備數據集

建立一個 JSON 格式的訓練數據集,包含圖像路徑和對應的描述文字:

[
  {"image": "path/to/image1.jpg", "caption": "一隻可愛的橘貓在曬太陽"},
  {"image": "path/to/image2.jpg", "caption": "辦公室會議室的白板上有圖表"}
]

步驟二:載入預訓練模型

from transformers import AutoModelForVision2Seq, AutoProcessor

model = AutoModelForVision2Seq.from_pretrained(
    "Qwen/Qwen2-VL-2B-Instruct",
    torch_dtype=torch.float16,
    device_map="auto"
)

processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")

步驟三:設定 LoRA 適配器

from peft import LoraConfig, get_peft_model

lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

model = get_peft_model(model, lora_config)
model.print_trainable_parameters()
# 輸出:trainable params: 4,194,304 || all params: 7,743,365,632 || trainable%: 0.05

步驟四:訓練配置

from transformers import TrainingArguments

training_args = TrainingArguments(
    output_dir="./vlm-adapter",
    num_train_epochs=3,
    per_device_train_batch_size=2,
    gradient_accumulation_steps=4,
    learning_rate=2e-4,
    fp16=True,
    logging_steps=10,
    save_strategy="epoch",
    report_to="none"
)

步驟五:開始訓練與推理

使用 Trainer 開始訓練,訓練完成後載入模型進行推理:

from transformers import Trainer
from datasets import Dataset

trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=your_dataset
)
trainer.train()

# 推理範例
inputs = processor(images=image, return_tensors="pt").to("cuda")
output = model.generate(**inputs, max_new_tokens=100)
print(processor.decode(output[0]))

常見問題與解決方案

  • OOM(記憶體不足):降低 batch size,使用 4-bit 量化(bitsandbytes)
  • 訓練不收斂:檢查學習率,嘗試調整 LoRA 的 r 值和 alpha
  • 輸出品質差:增加訓練數據量,確保數據品質多樣性

透過這個方法,你可以在有限的本地資源下,打造出專屬的視覺語言理解系統!