0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

📄AIモデルレポーティング:Microsoft製 OCR付AI?

0
Posted at

1. 概要

Florence-2はMicrosoftが公開している、プロンプトベースで様々な視覚・視覚言語タスクを処理できる統一視覚基盤モデルです。Florence-2はキャプション生成、物体検出、セグメンテーションなど幅広いタスクをシンプルなテキストプロンプトで実行でき、1億2600万枚の画像と54億件のアノテーションを含むFLD-5Bデータセットを活用してマルチタスク学習を行っています。

Florence-2-base-ft はこのシリーズの中で、baseサイズをdownstreamタスク群でfine-tuningした「generalistモデル」です。複数の下流タスクでfine-tuningされた結果、Florence-2-base-ftとFlorence-2-large-ftという2つのgeneralistモデルが生まれ、幅広い下流タスクをこなせるようになっています。

2. モデル系列(4種類)

モデル サイズ 説明
Florence-2-base 0.23B FLD-5Bで事前学習のみ
Florence-2-large 0.77B FLD-5Bで事前学習のみ
Florence-2-base-ft 0.23B 下流タスク群でfine-tuning済み
Florence-2-large-ft 0.77B 下流タスク群でfine-tuning済み

上記のモデル一覧はHugging Faceのモデルカードに記載されているものです。base-ftのパラメータ数は約0.2Bで、Hugging Face上のモデルサイズ表示も0.2Bパラメータ・F16テンソル型となっています。

3. 技術的な出典・ライセンス

  • 論文: Florence-2: Advancing a unified representation for a variety of vision tasks(arXiv:2311.06242、Xiao et al., 2023)
  • ライセンス: MIT(モデルカードのタグ表示より)
  • 実装: Hugging FaceのtransformersライブラリによるMicrosoft製Florence-2モデルの実装がこのリポジトリに含まれています
  • 公開時期: Florence-2は2024年6月にMicrosoftからMITライセンスでオープンソース公開された、軽量な基盤視覚言語モデルです

4. 対応タスク(プロンプトトークン)

モデルカードに明記されている対応タスクは以下の通りです。

  • <CAPTION> : 簡易キャプション
  • <DETAILED_CAPTION> : 詳細キャプション
  • <MORE_DETAILED_CAPTION> : さらに詳細なキャプション
  • <CAPTION_TO_PHRASE_GROUNDING> : キャプション文中のフレーズを画像領域に対応付け(テキスト入力が別途必要)
  • <OD> : 物体検出(Object Detection)
  • <DENSE_REGION_CAPTION> : 密な領域キャプション
  • <REGION_PROPOSAL> : 領域候補提案
  • <OCR> : 文字認識
  • <OCR_WITH_REGION> : 領域付き文字認識

公式ノートブックと相違がある点

公式ノートブックや他のFlorence-2派生モデルでは<REFERRING_EXPRESSION_SEGMENTATION><REGION_TO_SEGMENTATION>といった追加タスクトークンが紹介されている場合もあるようです。

5. 使い方(公式サンプルコードの構成)

公式モデルカードのサンプルは概ね以下の流れです。

## @brief transformersを用いてFlorence-2-base-ftをロードし物体検出(<OD>)を実行する
#  @details trust_remote_code=True が必須
import torch
import requests
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32

## モデルとプロセッサをロード
model = AutoModelForCausalLM.from_pretrained(
    "microsoft/Florence-2-base-ft",
    torch_dtype=torch_dtype,
    trust_remote_code=True
).to(device)
processor = AutoProcessor.from_pretrained(
    "microsoft/Florence-2-base-ft",
    trust_remote_code=True
)

prompt = "<OD>"
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)

inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
    input_ids=inputs["input_ids"],
    pixel_values=inputs["pixel_values"],
    max_new_tokens=1024,
    do_sample=False,
    num_beams=3
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(
    generated_text, task="<OD>", image_size=(image.width, image.height)
)
print(parsed_answer)

この実行例はモデルカードに掲載されている公式サンプルコードに基づいています。
なお、モデルカードにはpipeline経由の簡易な使い方(pipeline("image-text-to-text", ...))や、vLLM・SGLangでのサーブ方法も掲載されていましたが、これらはFlorence-2固有のタスク切り替え(post_process_generationなど)には対応していない可能性があり。

6. ベンチマーク(fine-tuned generalistモデルとして)

COCO Caption Karpathy testでCIDErスコア140.0、NoCaps val CIDEr 116.7、TextCaps val CIDEr 143.9、VQAv2 test-dev精度79.7、TextVQA test-dev精度63.6、VizWiz VQA test-dev精度63.6という結果が報告されています。

また物体検出・領域参照タスクではCOCO Det. val2017 mAP 41.4、Flickr30k test R@1 84.0、RefCOCO系のAccuracyは概ね82〜95%の範囲、RefCOCO RES val mIoU 78.0という数値が示されています。

これらはgeneralist(汎用)モデルとしての性能であり、同記事内のspecialistモデル(タスクごとに個別fine-tuning)と比べるとやや劣る場合がある点も、モデルカードの説明通り明記すると正確です。

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?