パイプラインのタスクと評価

Hugging Face で学ぶマルチモーダルモデル

James Chapman

Curriculum Manager, DataCamp

パイプライン vs. モデル構成要素

従来の方法

from transformers import BlipProcessor, BlipForConditionalGeneration
checkpoint = "Salesforce/blip-image-captioning-base"
processor = BlipProcessor.from_pretrained(checkpoint)
model = BlipForConditionalGeneration.from_pretrained(checkpoint)

Pipelines

from transformers import pipeline
pipe = pipeline("image-to-text", model=checkpoint)
Hugging Face で学ぶマルチモーダルモデル

例の比較

前処理器とモデルを直接使用

inputs = processor(images=image, 
                   return_tensors="pt")
gen = model.generate(**inputs)
processor.decode(gen[0])

パイプライン

pipe(image)
[{'generated_text': 
'a man wearing a black shirt'}]

datapoint5

Hugging Face で学ぶマルチモーダルモデル

モデルとタスクの見つけ方

APIでパイプライン用のモデルを探す:

from huggingface_hub import HfApi
model = list(api.list_models(task="text-to-image", limit=5))
pipe = pipeline("text-to-image", model[0].id)

Hugging Face のモデルホームページのスクリーンショット

Hugging Face で学ぶマルチモーダルモデル

モデルへのオプション指定

  • 内部では MusicgenForConditionalGeneration を使用
pipe = pipeline(task="text-to-audio", 
                model="facebook/musicgen-small", framework="pt")
generate_kwargs = {"temperature": 0.8, "max_new_tokens": 20}

outputs = pipe("Classic rock riff", generate_kwargs=generate_kwargs)
  • temperature (0-1): 乱数性・創造性の度合い
  • max_new_tokens: 生成トークン数の上限
Hugging Face で学ぶマルチモーダルモデル

パイプライン性能の評価

  • Accuracy: 正解分類の割合
  • Precision: 予測が正しい頻度
  • Recall: 実際の正例をどれだけ当てたか
  • F1 Score: precision と recall の調和平均
from evaluate import evaluator

task_evaluator = evaluator("image-classification")
metrics_dict = { "precision": "precision", "recall": "recall", "f1": "f1", }
label_map = pipe.model.config.label2id
Hugging Face で学ぶマルチモーダルモデル

パイプライン性能の評価

eval_results = task_evaluator.compute(
  model_or_pipeline=pipe,

data=dataset,
metric=evaluate.combine(metrics_dict),
label_mapping=label_map)
print(eval_results)
{'precision': 0.999001923076923, 
'recall': 0.999, 
'f1': 0.9989999609405906, ...}
pipe = pipeline(task="image-classification",
model="ideepankarsharma2003/AI_ImageClassi
fication_MidjourneyV6_SDXL"
)
dataset = load_dataset("ideepankarsharma2003/
Midjourney_v6_Classification_small_shuffled")

データセット中の水中の犬(AI生成画像)の例

Hugging Face で学ぶマルチモーダルモデル

練習してみましょう!

Hugging Face で学ぶマルチモーダルモデル

Preparing Video For Download...