![見出し画像](https://assets.st-note.com/production/uploads/images/119735074/rectangle_large_type_2_7a85a7a5b5c0332bfaa9ae9b9c2d770c.png?width=800)
Google Colab で Fuyu-8B を試す
「Google Colab」で「Fuyu-8B」を試したので、まとめました。
【注意】Google Colab Pro/Pro+ のA100で動作確認しています。
1. Fuyu-8B
「Fuyu」は、テキストと画像の入力からテキストを出力する軽量のマルチモーダルモデルです。
特徴は、次のとおりです。
(1) 他のマルチモーダルモデルよりもはるかにシンプルなアーキテクチャと学習手順があり、理解、拡張、展開が容易。
(2) デジタルエージェント向けにゼロから設計されているため、任意の画像解像度をサポートし、グラフや図に関する質問に答え、UIベースの質問に答え、画面画像のきめ細かなローカリゼーションを行うことができる。
(3) 高速。100ミリ秒未満で大きな画像の応答を得ることができる。
(4) ユースケースに最適化されているにもかかわらず、視覚的な質問回答や自然な画像キャプションなどの標準的な画像理解ベンチマークでうまく機能する。
2. Fuyu-8Bのモデル
現在リリースされているモデルはベースモデルのみです。冗長なキャプションやマルチモーダルチャットなどで利用するには、ユースケースに合わせてファインチューニングする必要があります。
・adept/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上に配置します。
![](https://assets.st-note.com/img/1698112941091-uZ689Nmqeo.png)
・bus.png
![](https://assets.st-note.com/img/1698112950544-ZkLt18iOlL.png?width=800)
(5) プロンプトと入力画像の準備。
from PIL import Image
# プロンプトと入力画像の準備
text_prompt = "Generate a coco-style caption.\n" # cocoスタイルのキャプションを生成してください
image_path = "bus.png" # 画像を配置しておく
image = Image.open(image_path)
cocoスタイルのキャプションを生成してください
(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
A bus parked on the side of a road.
4. グラフの例
(1) 画像の配置。
提供されているサンプル画像(https://huggingface.co/adept/fuyu-8b/blob/main/chart.png)をダウンロードし、Colab上に配置します。
・chart.png
![](https://assets.st-note.com/img/1698113602681-QicbmfXt0j.png?width=800)
(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
2018年の男性の出生時の平均余命は80.7歳です。
この記事が気に入ったらサポートをしてみませんか?