BLIP-2 による 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:インストーラーによるインストール
- Python公式サイト(https://www.python.org/downloads/)にアクセスし、「Download Python 3.x.x」ボタンからWindows用インストーラーをダウンロードする。
- ダウンロードしたインストーラーを実行する。
- 初期画面の下部に表示される「Add python.exe to PATH」にチェックを入れてから「Customize installation」を選択する。このチェックを入れ忘れると、コマンドプロンプトから
pythonコマンドを実行できない。 - 「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 プログラムを解釈・実行するソフトウェア)を選択する必要がある.
- コマンドパレット(コマンド名で機能を呼び出す VS Code の入力欄)を開く(
Ctrl+Shift+P) Python: Select Interpreterと入力する
- 表示される一覧から,使用する Python(例:
C:\Program Files\Python312\python.exe)を選択する.
必要なライブラリをシステム領域にインストール
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
REM PyTorch をインストール(GPU対応版)
set "CUDA_TAG=cu128"
set "PYTHON_PATH=C:\Program Files\Python312"
"%PYTHON_PATH%\Scripts\pip" install --no-user -U numpy torch torchvision torchaudio --index-url https://download.pytorch.org/whl/%CUDA_TAG%
pip install transformers pillow opencv-python numpy
BLIP-2 による Visual Question Answering プログラム
概要
このプログラムは、画像の視覚的内容を理解し、それに基づいて自然言語(英語)による質問に適切に回答する[1]。
主要技術
- BLIP-2(Bootstrapping Language-Image Pre-training)
事前学習済みの画像エンコーダと大規模言語モデルを凍結した状態で、軽量なQuerying Transformer(Q-Former)のみを訓練することで視覚と言語のモダリティ結合を実現する。Q-Formerは188Mパラメータという少ない学習可能パラメータで視覚情報と言語情報の橋渡しを果たす[1]。
- Transformersライブラリによるマルチモーダル処理
テキスト、コンピュータビジョン、音声、動画、マルチモーダルモデルのための機械学習モデルのフレームワークとして機能し、事前学習済みモデルの推論と訓練の両方をサポートする[2]。
主要技術
[1] Li, J., Li, D., Savarese, S., & Hoi, S. (2023). BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models. arXiv preprint arXiv:2301.12597.
[2] Wolf, T., Debut, L., Sanh, V., Chaumond, J., Delangue, C., Moi, A., ... & Rush, A. M. (2020). Transformers: State-of-the-art natural language processing. In Proceedings of the 2020 conference on empirical methods in natural language processing: system demonstrations (pp. 38-45).
ソースコード
# BLIP-2による画像質問応答システム
# BLIP-2: Bootstrapping Language-Image Pre-training with Frozen Image Encoders and Large Language Models
# 出典: Li, J., Li, D., Savarese, S., & Hoi, S. (2023). arXiv preprint arXiv:2301.12597.
# 機能: 画像とテキストの理解により画像に関する質問に自然言語で回答
import torch
from transformers import Blip2Processor, Blip2ForConditionalGeneration
import cv2
import tkinter as tk
from tkinter import filedialog
import urllib.request
from PIL import Image
import os
import warnings
# モデル定義
MODELS = [
("Salesforce/blip2-opt-2.7b", "BLIP-2 with OPT-2.7B"),
("Salesforce/blip2-flan-t5-xl", "BLIP-2 with Flan-T5-XL")
]
DEFAULT_MODEL = "Salesforce/blip2-opt-2.7b"
MAX_SIZE = 384
# 生成パラメータ
MAX_NEW_TOKENS = 50
NUM_BEAMS = 5
REPETITION_PENALTY = 1.5
LENGTH_PENALTY = 1.0
NO_REPEAT_NGRAM_SIZE = 3
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 = [
"What is in this image?",
"Describe this image in detail.",
"What are the colors and atmosphere of this image?",
"What objects can you see in this image?",
"What is happening in this image?",
"Describe the main subject of this image."
]
print("BLIP-2 Visual Question Answering システム")
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"使用デバイス: {device}")
print("\n利用可能なBLIP-2モデル:")
for i, (model_name, description) in enumerate(MODELS, 1):
print(f"{i}. {description}")
print("Enter: デフォルト(BLIP-2 with OPT-2.7B)を使用")
choice = input("\nモデル番号 (Enterでデフォルト): ")
if choice == "":
model_name = DEFAULT_MODEL
model_description = "BLIP-2 with OPT-2.7B"
else:
try:
idx = int(choice) - 1
if 0 <= idx < len(MODELS):
model_name = MODELS[idx][0]
model_description = MODELS[idx][1]
else:
print("無効な選択です。デフォルトを使用します。")
model_name = DEFAULT_MODEL
model_description = "BLIP-2 with OPT-2.7B"
except ValueError:
print("無効な選択です。デフォルトを使用します。")
model_name = DEFAULT_MODEL
model_description = "BLIP-2 with OPT-2.7B"
print(f"\n選択されたモデル: {model_description}")
print("モデルを読み込み中...")
try:
warnings.filterwarnings("ignore", category=FutureWarning)
processor = Blip2Processor.from_pretrained(model_name)
if device == "cuda":
model = Blip2ForConditionalGeneration.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
model.eval()
else:
model = Blip2ForConditionalGeneration.from_pretrained(model_name)
model.to(device)
model.eval()
print("モデルの読み込み完了\n")
except Exception as e:
print(f"モデル読み込み中にエラーが発生しました: {e}")
exit(1)
results = []
def preprocess_image(image_input):
"""画像の前処理"""
if isinstance(image_input, str):
image = Image.open(image_input).convert('RGB')
else:
image = Image.fromarray(cv2.cvtColor(image_input, cv2.COLOR_BGR2RGB))
if max(image.size) > MAX_SIZE:
image.thumbnail((MAX_SIZE, MAX_SIZE), Image.Resampling.LANCZOS)
return image
def generate_answer(image, question):
"""BLIP-2による回答生成"""
try:
inputs = processor(image, question, return_tensors="pt")
if device == "cuda":
inputs = {k: v.to(device, torch.float16 if v.dtype == torch.float32 else v.dtype)
for k, v in inputs.items()}
else:
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=MAX_NEW_TOKENS,
min_length=5,
do_sample=False,
num_beams=NUM_BEAMS,
repetition_penalty=REPETITION_PENALTY,
length_penalty=LENGTH_PENALTY,
early_stopping=True,
no_repeat_ngram_size=NO_REPEAT_NGRAM_SIZE,
pad_token_id=processor.tokenizer.pad_token_id,
eos_token_id=processor.tokenizer.eos_token_id
)
generated_text = processor.decode(outputs[0], skip_special_tokens=True)
# プロンプト除去
if question in generated_text:
answer = generated_text.replace(question, "").strip()
else:
answer = generated_text.strip()
if len(answer.strip()) < 3:
answer = "I apologize, but I couldn't generate an appropriate response."
return answer
except Exception as e:
print(f"回答生成中にエラーが発生しました: {e}")
return "An error occurred. Please try with a different image or question."
def select_question():
"""質問の選択"""
print("\n質問を選択:")
for i, q in enumerate(QUESTIONS, 1):
print(f"{i}. {q}")
print(f"{len(QUESTIONS)+1}. 自由入力")
while True:
try:
q_choice = input("選択: ")
q_choice = int(q_choice)
if 1 <= q_choice <= len(QUESTIONS):
return QUESTIONS[q_choice-1]
elif q_choice == len(QUESTIONS)+1:
return input("Enter your question: ")
else:
print(f"1から{len(QUESTIONS)+1}の間で選択してください。")
except ValueError:
print("数字を入力してください。")
def process_image(img_input, img_path=None):
"""画像処理メイン"""
image = preprocess_image(img_path or img_input)
question = select_question()
print("回答生成中...")
answer = generate_answer(image, question)
result = f"\nQuestion: {question}\nAnswer: {answer}\n"
print(result)
results.append(result)
display_img = cv2.imread(img_path) if img_path else img_input
cv2.imshow('Image - Press any key to close', display_img)
cv2.waitKey(0)
def main():
print("\n画像入力方法を選択:")
print("0: 画像ファイル")
print("1: カメラ")
print("2: サンプル画像")
choice = input("選択: ")
if choice == '0':
root = tk.Tk()
root.withdraw()
file_paths = filedialog.askopenfilenames(
title="画像ファイルを選択",
filetypes=[("画像ファイル", "*.jpg *.jpeg *.png *.bmp *.tiff")]
)
root.destroy()
for file_path in file_paths:
print(f"\n処理中: {os.path.basename(file_path)}")
process_image(None, file_path)
elif choice == '1':
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("カメラを開けませんでした。")
return
print("\nスペースキー: 撮影して分析")
print("Qキー: 終了")
while True:
ret, frame = cap.read()
if not ret:
print("カメラからフレームを取得できませんでした。")
break
cv2.imshow('Camera - Press SPACE to capture, Q to quit', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord(' '):
print("\n画像を撮影しました")
process_image(frame.copy())
elif key == ord('q'):
break
cap.release()
elif choice == '2':
print("\nサンプル画像:")
sample_names = ["fruits.jpg", "messi5.jpg", "aero3.jpg", "Cat03.jpg"]
for i, name in enumerate(sample_names, 1):
print(f"{i}. {name}")
while True:
try:
sample_choice = int(input("選択: ")) - 1
if 0 <= sample_choice < len(sample_names):
break
else:
print(f"1から{len(sample_names)}の間で選択してください。")
except ValueError:
print("数字を入力してください。")
url = SAMPLE_URLS[sample_choice]
filename = f"sample_{sample_choice}.jpg"
print(f"サンプル画像をダウンロード中: {sample_names[sample_choice]}")
try:
urllib.request.urlretrieve(url, filename)
process_image(None, filename)
if os.path.exists(filename):
os.remove(filename)
except Exception as e:
print(f"サンプル画像のダウンロードに失敗しました: {e}")
cv2.destroyAllWindows()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\nプログラムが中断されました。")
except Exception as e:
print(f"予期しないエラーが発生しました: {e}")
# 結果保存
if results:
with open('result.txt', 'w', encoding='utf-8') as f:
f.write("=== BLIP-2 VQA 結果 ===\n")
f.write(f"使用モデル: {model_name}\n")
f.write(f"デバイス: {device}\n")
f.write("=" * 50 + "\n")
for result in results:
f.write(result)
print(f"\n結果をresult.txtに保存しました ({len(results)}件)")
print("\nプログラムを終了します")