BLIP-2 による Visual Question Answering(ソースコードと実行結果)
Python開発環境,ライブラリ類
ここでは、最低限の事前準備について説明する。機械学習や深層学習を行う場合は、NVIDIA CUDA、Visual Studio、Cursorなどを追加でインストールすると便利である。これらについては別ページ https://www.kkaneko.jp/cc/dev/aiassist.htmlで詳しく解説しているので、必要に応じて参照してください。
Python 3.12 のインストール(Windows 上) [クリックして展開]
以下のいずれかの方法で Python 3.12 をインストールする。Python がインストール済みの場合、この手順は不要である。
方法1:winget によるインストール
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
winget install --scope machine --id Python.Python.3.12 -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --override "/quiet InstallAllUsers=1 PrependPath=1 Include_pip=1 Include_test=0 Include_launcher=1 InstallLauncherAllUsers=1"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
方法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' は、内部コマンドまたは外部コマンドとして認識されていません。」と表示される場合は、インストールが正常に完了していない。
AIエディタ Windsurf のインストール(Windows 上) [クリックして展開]
Pythonプログラムの編集・実行には、AIエディタの利用を推奨する。ここでは、Windsurfのインストールを説明する。Windsurf がインストール済みの場合、この手順は不要である。
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
winget install --scope machine --id Codeium.Windsurf -e --silent --disable-interactivity --force --accept-source-agreements --accept-package-agreements --custom "/SP- /SUPPRESSMSGBOXES /NORESTART /CLOSEAPPLICATIONS /DIR=""C:\Program Files\Windsurf"" /MERGETASKS=!runcode,addtopath,associatewithfiles,!desktopicon"
powershell -Command "$env:Path=[System.Environment]::GetEnvironmentVariable('Path','Machine')+';'+[System.Environment]::GetEnvironmentVariable('Path','User'); windsurf --install-extension MS-CEINTL.vscode-language-pack-ja --force; windsurf --install-extension ms-python.python --force; windsurf --install-extension Codeium.windsurfPyright --force"
--scope machine を指定することで、システム全体(全ユーザー向け)にインストールされる。このオプションの実行には管理者権限が必要である。インストール完了後、コマンドプロンプトを再起動すると PATH が自動的に設定される。
【関連する外部ページ】
Windsurf の公式ページ: https://windsurf.com/
必要なライブラリをシステム領域にインストール
管理者権限のコマンドプロンプトで以下を実行する。管理者権限のコマンドプロンプトを起動するには、Windows キーまたはスタートメニューから「cmd」と入力し、表示された「コマンドプロンプト」を右クリックして「管理者として実行」を選択する。
REM PyTorch をインストール(GPU対応版)
set "CUDA_TAG=cu126"
set "PYTHON_PATH=C:\Program Files\Python312"
"%PYTHON_PATH%\Scripts\pip" install -U 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プログラムを終了します")