Hugging Face LLaVA OneVision による Visual Question Answering (ソースコードと実行結果)

Python開発環境,ライブラリ類

ここでは、最低限の事前準備について説明する。機械学習や深層学習を行う場合は、NVIDIA CUDA、Visual Studio、Cursorなどを追加でインストールすると便利である。これらについては別ページ https://www.kkaneko.jp/cc/dev/aiassist.htmlで詳しく解説しているので、必要に応じて参照してください。

Python 3.12 のインストール

Pythonのインストールを行い、Pythonのプログラムを実行する環境を整える。扱う環境は、Windows搭載パソコンである。金子研究室では、Python 3.12.10を推奨する。

[Windows での Python 3.12 のインストール手順を見るには、ここをクリック]

Windows での Python 3.12 のインストール

以下のいずれかの方法でPython 3.12をインストールする。Pythonがインストール済みの場合、この手順は不要である。

方法 1:winget によるインストール

インストールコマンドの実行方法

管理者権限コマンドプロンプトを起動する(手順:Windowsキーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。そして、コマンド全体をコマンドプロンプトにコピー&ペーストする。

--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動するとPATHが反映される。

REM Python 3.12 をシステム領域にインストール
winget install --id Python.Python.3.12 -e --scope machine --silent --accept-source-agreements --accept-package-agreements --override "/quiet InstallAllUsers=1 PrependPath=1 Include_test=0 Include_pip=1 Include_launcher=1 InstallLauncherAllUsers=1 TargetDir=\"C:\Program Files\Python312\""

REM Python と Scripts を PATH 先頭に追加
powershell -NoProfile -Command "$p='C:\Program Files\Python312'; $s=\"$p\Scripts\"; $c=[Environment]::GetEnvironmentVariable('Path','Machine'); if((Test-Path $p) -and (';'+$c+';' -notlike \"*;$p;*\") -and (';'+$c+';' -notlike \"*;$s;*\")){[Environment]::SetEnvironmentVariable('Path',\"$p;$s;$c\",'Machine')}"

方法 2:インストーラーによるインストール

  1. Python公式サイト(https://www.python.org/downloads/)にアクセスし、「Download Python 3.x.x」ボタンからWindows用インストーラーをダウンロードする。
  2. ダウンロードしたインストーラーを実行する。
  3. 初期画面の下部に表示される「Add python.exe to PATH」にチェックを入れてから「Customize installation」を選択する。このチェックを入れ忘れると、コマンドプロンプトから python コマンドを実行できない。
  4. 「Install Python 3.xx for all users」にチェックを入れ、「Install」をクリックする。

インストールの確認

コマンドプロンプトで以下を実行する。

python --version

バージョン番号(例:Python 3.12.x)が表示されればインストール成功である。「'python' は、内部コマンドまたは外部コマンドとして認識されていません。」と表示される場合は、インストールが正常に完了していない。

Python の開発環境 Visual Studio Code のインストールと Python 用の設定

Python の開発環境Visual Studio Code(プログラムを編集するソフトウェア。以下、VS Code)を整える。

[Windows での Visual Studio Code のインストールと Python 用の設定手順を見るには、ここをクリック]

Windows での Visual Studio Code のインストールと Python 用の設定手順

1. VS Code と拡張機能のインストール

以下のコマンドにより,既存の VS Code を削除し,全ユーザー共有の設定で再インストールしたうえで,拡張機能(VS Code に機能を追加するソフトウェア)をまとめて導入する.

インストールコマンドの実行方法

管理者権限コマンドプロンプトを起動する(手順:Windows キーまたはスタートメニュー → cmd と入力 → 右クリック → 「管理者として実行」)。そして,コマンド全体をコマンドプロンプトにコピー&ペーストする。

インストールコマンド


REM ============================================================
REM Microsoft Visual Studio Code
REM ============================================================
winget uninstall -e --id Microsoft.VisualStudioCode --silent --disable-interactivity --accept-source-agreements
rmdir /s /q C:\ProgramData\vscode-extensions 2>nul
rmdir /s /q "%APPDATA%\Code" 2>nul
rmdir /s /q "%USERPROFILE%\.vscode" 2>nul
rmdir /s /q "%LOCALAPPDATA%\Microsoft\vscode-update" 2>nul

REM VS Code をシステム領域に新規インストール
winget install --scope machine --id Microsoft.VisualStudioCode -e --silent --accept-source-agreements --accept-package-agreements

REM 全ユーザー共有の拡張機能フォルダ
mkdir C:\ProgramData\vscode-extensions 2>nul
icacls "C:\ProgramData\vscode-extensions" /grant "Everyone:(OI)(CI)M" /T

REM スタートメニューのショートカットを --extensions-dir 付きで再作成
rmdir /s /q "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio Code" 2>nul
del "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio Code.lnk" 2>nul
powershell -NoProfile -Command "$s=New-Object -ComObject WScript.Shell; $lnk=$s.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio Code.lnk'); $lnk.TargetPath='C:\Program Files\Microsoft VS Code\Code.exe'; $lnk.Arguments='--extensions-dir \"C:\ProgramData\vscode-extensions\"'; $lnk.Save()"
REM ショートカットの検証
powershell -NoProfile -Command "$s=New-Object -ComObject WScript.Shell; $lnk=$s.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio Code.lnk'); Write-Host 'TargetPath:' $lnk.TargetPath; Write-Host 'Arguments:' $lnk.Arguments"

REM ファイル / フォルダ右クリックの「Code で開く」を登録
reg add "HKLM\SOFTWARE\Classes\*\shell\VSCode\command" /ve /d "\"C:\Program Files\Microsoft VS Code\Code.exe\" --extensions-dir \"C:\ProgramData\vscode-extensions\" \"%1\"" /f
reg add "HKLM\SOFTWARE\Classes\Directory\shell\VSCode\command" /ve /d "\"C:\Program Files\Microsoft VS Code\Code.exe\" --extensions-dir \"C:\ProgramData\vscode-extensions\" \"%1\"" /f
reg add "HKLM\SOFTWARE\Classes\Directory\Background\shell\VSCode\command" /ve /d "\"C:\Program Files\Microsoft VS Code\Code.exe\" --extensions-dir \"C:\ProgramData\vscode-extensions\" \"%V\"" /f

REM --extensions-dir 付きで起動する code.cmd ラッパを作成
REM (%* を echo で書くと対話的 cmd で失われるため、PowerShell で [char]37+'*' を書き出す)
powershell -NoProfile -Command "$pct=[char]37; $q=[char]34; $c='@echo off'+[char]13+[char]10+$q+'C:\Program Files\Microsoft VS Code\bin\code.cmd'+$q+' --extensions-dir '+$q+'C:\ProgramData\vscode-extensions'+$q+' '+$pct+'*'+[char]13+[char]10; [IO.File]::WriteAllText('C:\ProgramData\vscode-extensions\vscode.cmd',$c,[Text.Encoding]::ASCII)"

REM 拡張機能のインストール
set "CODE=C:\Program Files\Microsoft VS Code\bin\code.cmd"
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --uninstall-extension GitHub.copilot
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --uninstall-extension GitHub.copilot-chat
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension ms-python.python
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension ms-python.vscode-pylance
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension ms-python.debugpy
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension MS-CEINTL.vscode-language-pack-ja
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension saoudrizwan.claude-dev
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension rust-lang.rust-analyzer
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension tamasfe.even-better-toml
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension anthropic.claude-code
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --install-extension almenon.arepl
"%CODE%" --extensions-dir "C:\ProgramData\vscode-extensions" --list-extensions --show-versions
echo === セットアップ完了 ===

2. Python インタプリタの選択

同一マシンに複数の Python がインストールされている場合,VS Code で使用する Python 本体(インタプリタ:Python プログラムを解釈・実行するソフトウェア)を選択する必要がある.

  1. コマンドパレット(コマンド名で機能を呼び出す VS Code の入力欄)を開く(Ctrl+Shift+P
  2. Python: Select Interpreter と入力する
  3. 表示される一覧から,使用する Python(例:C:\Program Files\Python312\python.exe)を選択する.

必要なライブラリをシステム領域にインストール

管理者権限コマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。

pip install transformers pillow opencv-python torch torchvision accelerate

Hugging Face LLaVA OneVision Visual Question Answering プログラム

ソースコード

# プログラム名: Hugging Face LLaVA OneVision Visual Question Answering プログラム
# 特徴技術名: LLaVA OneVision (Large Language and Vision Assistant OneVision)
# 出典: Li, B., Zhang, Y., Guo, D., Zhang, R., Li, F., Zhang, H., Zhang, K., Li, Y., Liu, Z., & Li, C. (2024). LLaVA-OneVision: Easy Visual Task Transfer.
# 特徴機能: Vision-Language Understanding - 画像とテキストを同時に理解し、画像に関する質問に自然言語で回答
# 学習済みモデル:
#   - llava-hf/llava-onevision-qwen2-0.5b-ov-hf (0.5Bパラメータ、軽量版)
#   - llava-hf/llava-onevision-qwen2-7b-ov-hf (7Bパラメータ、標準版)
#   URL: https://huggingface.co/llava-hf
# 方式設計:
#   - 関連利用技術:
#     - Hugging Face Transformers: トランスフォーマーモデル実行フレームワーク (>= 4.45.0)
#     - Pillow: 画像処理ライブラリ
#     - PyTorch: 深層学習フレームワーク
#   - 入力と出力:
#     入力: 1つの静止画像,カメラ(ユーザは「0:画像ファイル,1:カメラ,2:サンプル画像」のメニューで選択.0:画像ファイルの場合はtkinterでファイル選択可能.1の場合はOpenCVでカメラが開き,スペースキーで撮影.2の場合はhttps://raw.githubusercontent.com/opencv/opencv/master/samples/data/fruits.jpg とhttps://raw.githubusercontent.com/opencv/opencv/master/samples/data/messi5.jpgとhttps://raw.githubusercontent.com/opencv/opencv/master/samples/data/aero3.jpgとhttps://upload.wikimedia.org/wikipedia/commons/3/3a/Cat03.jpgからinput()で選択)
#     出力: 処理結果をprint()で表示.プログラム終了時にprint()で表示した処理結果をresult.txtファイルに保存
#   - 処理手順:
#     1. Transformersライブラリのバージョン確認
#     2. GPUの自動検出とデバイス設定
#     3. LLaVA OneVisionモデルの選択とダウンロード
#     4. 画像の取得(ファイル/カメラ/サンプル)
#     5. プロセッサによる画像とテキストの前処理
#     6. モデルへの入力と推論実行
#     7. 生成された回答の表示と保存
#   - 前処理、後処理:
#     前処理: AutoProcessorによる画像とテキストの変換、apply_chat_templateの適用
#     後処理: トークンのデコード処理
#   - 追加処理: anyres_max_9技術での画像パッチ分割処理(最大2304x2304ピクセル対応)
#   - 調整を必要とする設定値: なし
# 将来方策: なし
# その他の重要事項: transformers >= 4.45.0が必要、GPUメモリに応じてモデルを選択
# 前準備: pip install transformers pillow opencv-python torch torchvision accelerate

import sys
import os
import cv2
import tkinter as tk
from tkinter import filedialog
import urllib.request
from PIL import Image
import torch
from transformers import LlavaOnevisionForConditionalGeneration, AutoProcessor
import transformers
import warnings
warnings.filterwarnings('ignore')

# Transformersライブラリのバージョン確認
def check_transformers_version():
    required_version = "4.45.0"
    current_version = transformers.__version__
    from packaging import version

    if version.parse(current_version) < version.parse(required_version):
        print(f"エラー: transformers {required_version} 以上が必要です。現在のバージョン: {current_version}")
        print(f"アップグレード: pip install transformers>={required_version}")
        exit()
    else:
        print(f"transformers バージョン確認OK: {current_version}")

# 定数定義
MODELS = [
    ("llava-hf/llava-onevision-qwen2-0.5b-ov-hf", "0.5B", "軽量版"),
    ("llava-hf/llava-onevision-qwen2-7b-ov-hf", "7B", "標準版")
]

DEFAULT_MODEL = "llava-hf/llava-onevision-qwen2-0.5b-ov-hf"  # デフォルトモデル

# 生成パラメータ設定(公式推奨値)
GENERATION_PARAMS = {
    "max_new_tokens": 512,
    "do_sample": True,
    "temperature": 0.7,
    "top_p": 0.9,
    "repetition_penalty": 1.2,
    "pad_token_id": None,
    "eos_token_id": None
}

# 画像処理パラメータ(LLaVA-OneVision公式仕様)
IMAGE_PARAMS = {
    "max_image_size": 2304,         # 最大解像度 2304x2304px
    "anyres_max_patches": 9,        # anyres_max_9モード: 最大9パッチ分割
    "video_frame_tokens": 196       # ビデオフレーム当たりのトークン数
}

# モデル設定
MODEL_CONFIGS = {
    "torch_dtype": torch.float16,
    "device_map": "auto",
    "context_window": 32768
}

SAMPLE_URLS = [
    "https://raw.githubusercontent.com/opencv/opencv/master/samples/data/fruits.jpg",
    "https://raw.githubusercontent.com/opencv/opencv/master/samples/data/messi5.jpg",
    "https://raw.githubusercontent.com/opencv/opencv/master/samples/data/aero3.jpg",
    "https://upload.wikimedia.org/wikipedia/commons/3/3a/Cat03.jpg"
]

QUESTIONS = [
    "この画像には何が写っていますか?日本語で回答してください",
    "この画像の詳細を説明してください。日本語で回答してください",
    "この画像の色や雰囲気について教えてください。日本語で回答してください"
]

print("Hugging Face LLaVA OneVision VQA システム")
print("画像を入力し、質問に対する回答を生成します")
print("言語モデル: Qwen2(日本語対応)")
print("\n注意: 0.5Bモデルは日本語生成能力が限定的です。")
print("より良い日本語出力には7Bモデルの使用を推奨します。\n")

# Transformersバージョン確認
check_transformers_version()

# GPU/CPU自動選択
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'デバイス: {str(device)}')
# GPU使用時の最適化
if device.type == 'cuda':
    torch.backends.cudnn.benchmark = True

if device.type == "cuda":
    gpu_memory = torch.cuda.get_device_properties(0).total_memory / 1024**3
    print(f"GPU メモリ: {gpu_memory:.1f}GB")
    if gpu_memory < 8:
        print("推奨: 0.5Bモデル(軽量版)- ただし日本語能力は限定的")
    else:
        print("推奨: 7Bモデル(標準版)- 良好な日本語生成")
else:
    print("CPUモードで実行します(処理速度が低下します)")
    print("推奨: 0.5Bモデル(軽量版)- ただし日本語能力は限定的")

# モデル選択
print("\n利用可能モデル:")
print("1. llava-hf/llava-onevision-qwen2-0.5b-ov-hf (0.5B) - 軽量版")
print("2. llava-hf/llava-onevision-qwen2-7b-ov-hf (7B) - 標準版")
print("Enter: デフォルト(0.5B)を使用")

choice = input("\nモデル番号 (Enterでデフォルト): ")
if choice == "":
    model_name = DEFAULT_MODEL
else:
    try:
        model_name = MODELS[int(choice) - 1][0]
    except:
        model_name = DEFAULT_MODEL

print(f"\n{model_name} をロード中...")

# モデルとプロセッサのロード
try:
    processor = AutoProcessor.from_pretrained(model_name)

    # メモリ効率化のための設定
    model_kwargs = {
        "torch_dtype": MODEL_CONFIGS["torch_dtype"],
        "device_map": MODEL_CONFIGS["device_map"] if device.type == "cuda" else None
    }

    # CPUの場合はfloat32を使用
    if device.type == "cpu":
        model_kwargs["torch_dtype"] = torch.float32

    model = LlavaOnevisionForConditionalGeneration.from_pretrained(
        model_name,
        **model_kwargs
    )

    model.to(device)
    # evalモードに設定(左パディング適用)
    model.eval()

    # 生成パラメータのtokenizer設定
    GENERATION_PARAMS["pad_token_id"] = processor.tokenizer.pad_token_id
    GENERATION_PARAMS["eos_token_id"] = processor.tokenizer.eos_token_id

    print(f"{model_name} ロード完了")
except Exception as e:
    print(f"モデルのロードに失敗しました: {e}")
    print("transformers >= 4.45.0 がインストールされているか確認してください")
    exit()

results = []  # 結果保存用

def process_image(img):
    if img is None:
        print("画像の読み込みに失敗しました")
        return

    # 画像サイズ確認(2304px制限内での処理)
    height, width = img.shape[:2]
    if width > IMAGE_PARAMS["max_image_size"] or height > IMAGE_PARAMS["max_image_size"]:
        max_dim = max(width, height)
        scale = IMAGE_PARAMS["max_image_size"] / max_dim
        new_width = int(width * scale)
        new_height = int(height * scale)
        img_resized = cv2.resize(img, (new_width, new_height))
        print(f"画像リサイズ: {width}x{height} -> {new_width}x{new_height}")
    else:
        img_resized = img

    # 質問選択
    print("\n質問:")
    for i, q in enumerate(QUESTIONS, 1):
        print(f"{i}. {q}")
    print("4. 自由入力")

    q_choice = int(input("選択: "))
    if q_choice <= 3:
        question = QUESTIONS[q_choice-1]
    else:
        question = input("質問: ") + " 必ず日本語で回答してください。"

    # OpenCV画像をPIL画像に変換
    pil_img = Image.fromarray(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB))

    # 会話形式の入力を作成(apply_chat_template使用)
    conversation = [
        {
            "role": "system",
            "content": "You are a helpful assistant. Please answer in Japanese language only. あなたの回答はすべて日本語にしてください。"
        },
        {
            "role": "user",
            "content": [
                {"type": "image"},
                {"type": "text", "text": question + "\n\n重要: あなたの回答はすべて日本語にしてください。"}
            ]
        }
    ]

    # プロセッサによる前処理(apply_chat_template適用)
    print("回答生成中...")
    try:
        prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
        inputs = processor(text=prompt, images=pil_img, return_tensors="pt")

        # デバイスに入力を移動
        inputs = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs.items()}

        # 推論実行
        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                **GENERATION_PARAMS
            )

        # 生成されたトークンのみを抽出してデコード
        generated_ids = outputs[:, inputs['input_ids'].shape[1]:]
        response_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]

        result = f"\n質問: {question}\n回答: {response_text}\n"
        print(result)
        results.append(result)
    except Exception as e:
        print(f"推論エラー: {e}")
        return

    cv2.imshow('Image', img)  # 元の画像を表示
    cv2.waitKey(0)

print("\n0: 画像ファイル")
print("1: カメラ")
print("2: サンプル画像")

choice = input("選択: ")

if choice == '0':
    root = tk.Tk()
    root.withdraw()
    paths = filedialog.askopenfilenames()
    if not paths:
        exit()
    for path in paths:
        process_image(cv2.imread(path))
elif choice == '1':
    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    print("\nスペースキー: 撮影, Qキー: 終了")
    try:
        while True:
            cap.grab()
            ret, frame = cap.retrieve()
            if not ret:
                break
            cv2.imshow('Camera', frame)
            key = cv2.waitKey(1) & 0xFF
            if key == ord(' '):
                process_image(frame)
            elif key == ord('q'):
                break
    finally:
        cap.release()
elif choice == '2':
    print("\nサンプル画像:")
    print("1. fruits.jpg")
    print("2. messi5.jpg")
    print("3. aero3.jpg")
    print("4. Cat03.jpg")

    idx = int(input("選択: ")) - 1
    filename = f"sample_{idx}.jpg"
    try:
        urllib.request.urlretrieve(SAMPLE_URLS[idx], filename)
        process_image(cv2.imread(filename))
        os.remove(filename)
    except Exception as e:
        print(f"画像のダウンロードに失敗しました: {e}")
        exit()

cv2.destroyAllWindows()

# 結果保存
if results:
    with open('result.txt', 'w', encoding='utf-8') as f:
        f.writelines(results)
    print("\nresult.txtに保存しました")