![見出し画像](https://assets.st-note.com/production/uploads/images/129043517/rectangle_large_type_2_d3fd03940254af99bc61a229ca0018fb.png?width=800)
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」よりもわずかに優れたパフォーマンスを達成しています。
![](https://assets.st-note.com/img/1706424418803-ydVojWNQmV.png?width=800)
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) 左端のフォルダアイコンから画像をアップロード。
![](https://assets.st-note.com/img/1706423521262-YcIb7ebQwf.png?width=800)
(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.
【翻訳】
画像の中の人物は、ピンクの長い髪をした若い女性です。 彼女はピンクのシャツを着ており、ギターを弾いているように見えます。
![](https://assets.st-note.com/img/1706424287810-bDtKLsCwia.png?width=800)
メモリ消費量は、次のとおりです。
![](https://assets.st-note.com/img/1706424181306-KztCEicRuF.png?width=800)
この記事が気に入ったらサポートをしてみませんか?