Community影像github.com

wshobson/vision-sft

使用監督式學習對影像+文字資料進行視覺語言模型(VLM)微調。適用於將 VLM 適應到特定視覺領域或任務、配置凍結視覺塔的 LoRA,或除錯訓練但無學習成效的 VLM 微調。

vision-sft 是什麼?

此技能專門處理視覺語言模型(VLM)的監督式微調(SFT),以影像+文字示範資料作為輸入,而非偏好對或可驗證的獎勵訊號。它基於「finetuning-method-selection」路由而來,並針對視覺塔(vision tower)和投影層(projector)擴充了「lora-qlora-recipes」中僅限文字的部分。技能的核心共識是:預設凍結視覺塔與投影層,僅對 LLM 部分應用低秩適應(LoRA),秩通常設為 8-16。當遇到視覺領域轉移(如衛星影像、醫學掃描)時,則斟酌解凍最後六層 ViT,同時將視覺學習率降低 5-10 倍。技能還揭示了兩個「無聲殺手」:影像標記/計數不匹配以及 `min_pixels`/`max_pixels` 解析度預算設定不當,兩者都會導致訓練看似正常但模型毫無學習成效。此外,也提供了 Unsloth 框架的特定配置指引,例如搭配 `fast_inference=True` 時必須設定 `finetune_vision_layers=False`。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/wshobson/agents/tree/main/plugins/llm-finetuning/skills/vision-sft

Installed? Explore more 影像 skills: steipete/songsee, affaan-m/frontend-design, mattpocock/improve-codebase-architecture · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

視覺語言 SFT

此技能假設 finetuning-method-selection 已路由至此:資料格式為 影像+文字示範,非偏好對 或可驗證的獎勵訊號,且基礎模型為 視覺語言模型而非純文字模型。lora-qlora-recipes 涵蓋了本技能針對視覺塔與投影層專門化的純文字 LoRA/QLoRA 方法; 若對 LoRA 基礎(秩、alpha、目標模組)不熟悉,請先閱讀該技能。

輸入: 一個影像+文字資料集以及一個 已從模型目錄選定的 VLM 基礎模型。 輸出格式: 一個經過驗證的適配器配置—— 哪些元件被凍結、LoRA 目標模組、 以及 min_pixels/max_pixels 預算—— 供 llm-finetuning-training-engineer 在產生可執行腳本時直接使用。

快速參考

情境預設值
在熟悉影像上調整行為凍結塔+投影層,LoRA r=8–16,α=16–32
視覺領域轉移解凍最後 6 層 ViT,視覺學習率低 5–10 倍
目標秩下無法裝入 bf16QLoRA——僅凍結視覺塔
fast_inference=Truefinetune_vision_layers=False
損失正常,評估未改善首先檢查下方「兩個無聲殺手」

共識方法

凍結視覺塔與投影層。僅對 LLM 應用 LoRA, 所有線性層(與純文字 SFT 相同的注意力 + MLP 目標清單—— 請參閱 lora-qlora-recipes), 設定 r=8–16,α=16–32。這是調整 VLM 行為 而不干擾其視覺能力的既定預設值。

  • 視覺塔與投影層預設保持凍結。 它們已編碼了通用的視覺表示;重新訓練它們 通常非必要,且對大多數任務而言增加風險卻未增加能力。
  • LoRA 秩低於純文字通用預設值(此處 r=8–16 對比純文字 SFT 的 r=16–32),因為僅 LLM 的適配器 是在調整行為,而非注入新的視覺知識。
  • QLoRA 僅允許搭配凍結的視覺塔使用。 量化基礎模型的同時又解凍並訓練視覺層是 不受支援且不穩定的——請將其視為嚴格的配對規則, 而非可調參數。如果需要解凍視覺塔, 請放棄 QLoRA 並改用 bf16 LoRA。
# 凍結塔 + 投影層;僅對 LLM 應用 LoRA
for name, param in model.named_parameters():
    if "vision_tower" in name or "projector" in name:
        param.requires_grad = False

target_modules = [
    "q_proj", "k_proj", "v_proj", "o_proj",
    "gate_proj", "up_proj", "down_proj",
]  # 僅 LLM,所有線性層——r=8-16, alpha=16-32

何時解凍

解凍視覺層是經過深思熟慮的升級動作, 而非預設決定——僅在領域轉移是視覺上 而非文字上時才考慮使用。

  • 僅在視覺領域轉移時解凍。 如果任務是在塔已理解的影像(圖表、日常照片)上 教授新行為,上述凍結塔方法 就已足夠。當視覺領域本身對塔而言 不熟悉時——衛星影像、醫學掃描、 密集的技術圖表——且凍結塔方法達到瓶頸時,才解凍。
  • 最後 6 層 ViT 是最佳甜蜜點。 解凍最後六個視覺變換器層(而非整個塔) 相較凍結基準測得 DocVQA 提升 +1.7pt, 訓練成本約增加 1.75 倍。 將六層視為值得付出的上限;進一步解凍 只會消耗計算資源而無匹配的結果。
  • 解凍時,視覺學習率必須比 LLM 學習率低 5–10 倍。 視覺塔的預訓練表示比 LLM 的適配器更脆弱; 對兩者使用相同的學習率可能導致視覺表示 被覆蓋的速度快於 LLM 適配器能補償的速度。
  • 在 patch embedding 層上使用高 LoRA 秩有 NaN 風險。 如果 patch embedding 屬於解凍集合, 請保持其低秩並密切監控早期步驟的損失—— 這是在 VLM 中應用 LoRA 最脆弱的位置之一。

兩個無聲殺手

兩者都會導致訓練在無錯誤的情況下進行,卻毫無學習成效:損失曲線看似正常, 模型沒有改善,且兩者都不會拋出例外—— 兩者都需要明確的訓練前檢查,而不僅僅是乾淨的訓練日誌。

  • 影像標記/計數不匹配。 模板化文字中的每個影像佔位符記號 必須與實際傳遞給 collator 的媒體項目 一一對應。不匹配(一個佔位符卻附加零或兩個影像; 或一個影像沒有佔位符)在多數 collator 中不會報錯—— 它會無聲地使影像與文字對齊錯誤,且模型 「訓練但無所學習」。在訓練開始前, 針對每個範例(不僅是樣本)驗證 一一對應的佔位符到媒體映射。 完整驗證檢查清單詳情: references/collators-and-pitfalls.md。
  • min_pixels/max_pixels 解析度預算。 這組參數對 VLM SFT 的品質和記憶體影響最大—— 超過秩、alpha 或學習率。設定太低會 無聲地將影像降採樣到低於任務所需的解析度 (即使訓練「成功」,小文件文字也會變得無法閱讀); 設定太高會耗盡激活記憶體預算,或迫使批次 過小而無法穩定訓練。請根據每個資料集 審慎設定,不要保留框架預設值。

Unsloth 專用事項

  • UnslothVisionDataCollator 是 Unsloth 用於 VLM SFT 的 collator—— 它處理 references/collators-and-pitfalls.md 中描述的 影像標記對齊和按架構的處理器合約。 請勿用純文字 collator 替代 VLM 資料。
  • finetune_vision_layers=False 是使用 fast_inference=True 時的必要設定。 vLLM 無法在視覺層上提供 LoRA 適配器服務,因此 同時啟用快速推論與解凍視覺層的設定 即使訓練成功,在服務階段也會失敗。如果方法要求 解凍最後 6 層 ViT(請參閱上方「何時解凍」), 則該次執行不能使用快速推論——請二擇一, 不可兩者並用。

模型選擇

基礎 VLM 的選擇不在本技能範圍內—— 它統一放在 finetuning-method-selection 的 references/model-catalog.md 模型目錄中。 本技能及其參考文件僅按架構系列描述方法, 絕不推薦特定模型。

VLM 強化學習(VLM-GRPO)在本外掛中僅供參考—— 關於 VLM-RL 特有的碎片化工具與獎勵駭客失敗模式 已在 grpo-rlvr-training 中涵蓋, 非此處討論範圍。本技能的範圍止於監督式微調。

失敗模式

上述每個章節中反覆出現的錯誤是: 將乾淨的損失曲線視為訓練健康的證據。 一條正常的曲線可能對應執行成功的訓練, 也可能對應任一無聲殺手,因為模型無論如何 都在某個東西上訓練——只是當無聲殺手存在時, 並非對齊的影像-文字訊號。 如果評估分數持平而損失曲線正常, 在調整任何超參數之前,請先重新執行 references/collators-and-pitfalls.md 中的檢查清單。

參考文件

  • references/collators-and-pitfalls.md——按架構的 collator 表、含影像佔位符的資料集格式範例、訓練前驗證檢查清單,以及作為進階模式的兩階段投影層對齊方法。

相關技能:finetuning-method-selection 路由至此;lora-qlora-recipes 涵蓋 本技能專門化的純文字 LoRA 基礎; grpo-rlvr-training 涵蓋 VLM-RL(僅供參考); dataset-curation 涵蓋本技能未涉及的 影像+文字資料集準備。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

wshobson/accessibility-compliance

Implement WCAG 2.2 compliant interfaces with mobile accessibility, inclusive design patterns, and assistive technology support. Use when auditing accessibility, implementing ARIA patterns, building for screen readers, or ensuring inclusive user experiences.

wshobson/airflow-dag-patterns

Build production Apache Airflow DAGs with best practices for operators, sensors, testing, and deployment. Use when creating data pipelines, orchestrating workflows, or scheduling batch jobs.

wshobson/angular-migration

Migrate from AngularJS to Angular using hybrid mode, incremental component rewriting, and dependency injection updates. Use when upgrading AngularJS applications, planning framework migrations, or modernizing legacy Angular code.

wshobson/anti-reversing-techniques

Understand anti-reversing, obfuscation, and protection techniques encountered during software analysis. Use this skill when analyzing malware evasion techniques, when implementing anti-debugging protections for CTF challenges, when reverse engineering packed binaries, or when building security research tools that need to detect virtualized environments.

wshobson/api-design-principles

Master REST and GraphQL API design principles to build intuitive, scalable, and maintainable APIs that delight developers. Use when designing new APIs, reviewing API specifications, or establishing API design standards.

wshobson/architecture-decision-records

Write and maintain Architecture Decision Records (ADRs) following best practices for technical decision documentation. Use when documenting significant technical decisions, reviewing past architectural choices, or establishing decision processes.

wshobson/architecture-patterns

Implement proven backend architecture patterns including Clean Architecture, Hexagonal Architecture, and Domain-Driven Design. Use this skill when designing clean architecture for a new microservice, when refactoring a monolith to use bounded contexts, when implementing hexagonal or onion architecture patterns, or when debugging dependency cycles between application layers.

wshobson/async-python-patterns

Master Python asyncio, concurrent programming, and async/await patterns for high-performance applications. Use when building async APIs, concurrent systems, or I/O-bound applications requiring non-blocking operations.

wshobson/attack-tree-construction

Build comprehensive attack trees to visualize threat paths. Use when mapping attack scenarios, identifying defense gaps, or communicating security risks to stakeholders.

wshobson/auth-implementation-patterns

Master authentication and authorization patterns including JWT, OAuth2, session management, and RBAC to build secure, scalable access control systems. Use when implementing auth systems, securing APIs, or debugging security issues.

wshobson/avoid-ai-writing

Audit and rewrite prose so it stops reading as machine-generated. Use this skill when asked to remove AI-isms, clean up AI writing, edit a draft for AI tells, audit a README, changelog, release note, PR description, or blog post for machine-sounding prose, or make text sound less like AI. Supports a detect-only mode, a rewrite mode, and an edit-in-place mode, with optional voice and context profiles.

wshobson/backtesting-frameworks

Build robust backtesting systems for trading strategies with proper handling of look-ahead bias, survivorship bias, and transaction costs. Use when developing trading algorithms, validating strategies, or building backtesting infrastructure.

wshobson/bash-defensive-patterns

Master defensive Bash programming techniques for production-grade scripts. Use when writing robust shell scripts, CI/CD pipelines, or system utilities requiring fault tolerance and safety.

wshobson/bats-testing-patterns

Master Bash Automated Testing System (Bats) for comprehensive shell script testing. Use when writing tests for shell scripts, CI/CD pipelines, or requiring test-driven development of shell utilities.

wshobson/bazel-build-optimization

Optimize Bazel builds for large-scale monorepos. Use when configuring Bazel, implementing remote execution, or optimizing build performance for enterprise codebases.

wshobson/before-you-build

Pre-build product and feature risk review for founders, product managers, and AI-assisted builders. Use this skill when the user is about to build a landing page, MVP, SaaS product, internal tool, agent workflow, or major feature and needs to check demand, positioning, monetization, retention, trust, distribution, and adoption risk before implementation starts.

wshobson/billing-automation

Build automated billing systems for recurring payments, invoicing, subscription lifecycle, and dunning management. Use when implementing subscription billing, automating invoicing, or managing recurring payment systems.

wshobson/binary-analysis-patterns

Master binary analysis patterns including disassembly, decompilation, control flow analysis, and code pattern recognition. Use when analyzing executables, understanding compiled code, or performing static analysis on binaries.

wshobson/block-no-verify-hook

Configure a PreToolUse hook to prevent AI agents from skipping git pre-commit hooks with --no-verify and other bypass flags. Use when setting up Claude Code projects that enforce commit quality gates.

wshobson/brand-landingpage

Brand-first landing page designer — runs a brand-identity interview (colors, typography, shape language), then generates and iterates on a polished landing page via Stitch with deployment-ready HTML. Use when the user asks to create, design, or build a landing page, homepage, or marketing page and has no established visual direction. Skip when they have a design mockup, need a dashboard or app UI, are working at component level, building a multi-page app, or restyling with known design tokens — use frontend-design instead.

相關技能