見出し画像

Google Colab で imp-v1-3B を試す

「Google Colab」で「imp-v1-3B」を試したので、まとめました。

1. imp-v1-3B

imp-v1-3b」は、3Bの強力なMSLM (Multimodal Small Language Model) です。小型ながら強力なSLM「Phi-2」(2.7B) と強力なビジュアルエンコーダ「SigLIP」 (0.4B) に基づいて構築され、「LLaVA-v1.5」の学習セットで学習しています。

同様のモデルサイズよりも大幅に優れたパフォーマンスを示し、さまざまなマルチモーダルベンチマークで強力な「LLaVA-7B」よりもわずかに優れたパフォーマンスを達成しています。

2. Colabでの実行

Colabでの実行手順は、次のとおりです。

(1) パッケージのインストール。

# パッケージのインストール
!pip install -U transformers
!pip install -q pillow accelerate einops

(2) モデルとトークナイザーの準備。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image

torch.set_default_device("cuda")

# モデルとトークナイザーの準備
model = AutoModelForCausalLM.from_pretrained(
    "MILVLG/imp-v1-3b", 
    torch_dtype=torch.float16, 
    device_map="auto",
    trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
    "MILVLG/imp-v1-3b", 
    trust_remote_code=True
)

(3) 左端のフォルダアイコンから画像をアップロード。

(4) プロンプトと画像の準備。

# プロンプトと画像の準備
text = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\nDescribe the person in the image. ASSISTANT:"
image = Image.open("bocchi.jpg")

今回のプロンプトは、次のとおりです。

A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. USER: <image>\nDescribe the person in the image. ASSISTANT:

【翻訳】
好奇心旺盛なユーザーと人工知能アシスタントとのチャット。アシスタントは、ユーザーの質問に対して、親切かつ詳細かつ丁寧に回答します。USER: <image>\n画像内の人物について説明してください。 ASSISTANT:

(5) 推論の実行。

# 推論の実行
input_ids = tokenizer(text, return_tensors="pt").input_ids
image_tensor = model.image_preprocess(image)
output_ids = model.generate(
    input_ids,
    max_new_tokens=100,
    images=image_tensor,
    use_cache=True)[0]
print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())

The person in the image is a young woman with long, pink hair. She is wearing a pink shirt and appears to be playing a guitar.

【翻訳】
画像の中の人物は、ピンクの長い髪をした若い女性です。 彼女はピンクのシャツを着ており、ギターを弾いているように見えます。

メモリ消費量は、次のとおりです。



この記事が気に入ったらサポートをしてみませんか?