Google Colab で Fuyu-8B を試す
「Google Colab」で「Fuyu-8B」を試したので、まとめました。
1. Fuyu-8B
「Fuyu」は、テキストと画像の入力からテキストを出力する軽量のマルチモーダルモデルです。
特徴は、次のとおりです。
2. Fuyu-8Bのモデル
現在リリースされているモデルはベースモデルのみです。冗長なキャプションやマルチモーダルチャットなどで利用するには、ユースケースに合わせてファインチューニングする必要があります。
3. Colabでの実行
Colabでの実行手順は、次のとおりです。
(1) Colabのノートブックを開き、メニュー「編集 → ノートブックの設定」で「GPU」の「A100」を選択。
(2) パッケージのインストール。
!pip install git+https://github.com/huggingface/transformers
!pip install accelerate
(3) プロセッサとモデルの準備。
from transformers import FuyuProcessor, FuyuForCausalLM
# プロセッサとモデルの読み込み
processor = FuyuProcessor.from_pretrained(
"adept/fuyu-8b"
)
model = FuyuForCausalLM.from_pretrained(
"adept/fuyu-8b",
device_map="cuda:0"
)
(4) 画像の配置。
提供されているサンプル画像(https://huggingface.co/adept/fuyu-8b/blob/main/bus.png)をダウンロードし、Colab上に配置します。
・bus.png
(5) プロンプトと入力画像の準備。
from PIL import Image
# プロンプトと入力画像の準備
text_prompt = "Generate a coco-style caption.\n" # cocoスタイルのキャプションを生成してください
image_path = "bus.png" # 画像を配置しておく
image = Image.open(image_path)
(6) 推論の実行。
1.26秒で推論完了しました。
%%time
# 推論の実行
inputs = processor(
text=text_prompt,
images=image,
return_tensors="pt"
)
for k, v in inputs.items():
inputs[k] = v.to("cuda:0")
generation_output = model.generate(**inputs, max_new_tokens=7)
generation_text = processor.batch_decode(generation_output[:, -7:], skip_special_tokens=True)
print(generation_text)
['A bus parked on the side of a road.']
CPU times: user 1.28 s, sys: 73.1 ms, total: 1.36 s
Wall time: 1.26 s
4. グラフの例
(1) 画像の配置。
提供されているサンプル画像(https://huggingface.co/adept/fuyu-8b/blob/main/chart.png)をダウンロードし、Colab上に配置します。
・chart.png
(2) プロンプトと入力画像の準備。
from PIL import Image
# プロンプトと入力画像の準備
text_prompt = "What is the highest life expectancy at birth of male?\n" # 男性の出生時の平均余命の最長はどれくらいですか?
image_path = "chart.png" # 画像を配置しておく
image = Image.open(image_path)
(3) 推論の実行。
%%time
# 推論の実行
inputs = processor(
text=text_prompt,
images=image,
return_tensors="pt"
)
for k, v in inputs.items():
inputs[k] = v.to("cuda:0")
generation_output = model.generate(**inputs, max_new_tokens=16)
generation_text = processor.batch_decode(generation_output[:, -16:], skip_special_tokens=True)
print(generation_text)
['The life expectancy at birth of males in 2018 is 80.7.\n']
CPU times: user 1.95 s, sys: 84.2 ms, total: 2.04 s
Wall time: 1.94 s
この記事が気に入ったらサポートをしてみませんか?