HuggingFace Transformers 4.17 : Tutorials : 推論のためのパイプライン (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 04/11/2022 (v4.17.0)
* 本ページは、HuggingFace Transformers の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
- 株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション
- sales-info@classcat.com ; Web: www.classcat.com ; ClassCatJP
HuggingFace Transformers : Tutorials : 推論のためのパイプライン
pipeline() はテキスト生成, 画像セグメンテーション, そして音声分類のような様々なタスクに対して推論のためにモデルハブからの任意のモデルを使用することを単純にします。特定の様式での経験がなくてもあるいはモデルを駆動するコードを理解していなくても、依然として pipeline() でそれらを利用できます!このチュートリアルは以下を教えます :
- 推論のために pipeline() を使用する。
- 特定のトークナイザーやモデルを使用する。
- 音声とビジョンタスクのために pipeline() を使用する。
サポートされるタスクの完全なリストについては pipeline() ドキュメントを見てください。
パイプラインの使用方法
各タスクが関連する pipeline() を持つ一方で、一般的な pipeline() 抽象化を使用するのがより簡単です、これは特定のタスクのパイプライン総てを含みます。pipeline() は、タスクのために推論可能なデフォルトのモデルとトークナイザーを自動的にロードします。
- pipeline() を作成することから始めて推論タスクを指定します :
from transformers import pipeline generator = pipeline(task="text-generation")
- 入力テキストを pipeline() に渡します :
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, and Four for the Great-king for the Gods of darkness."\n\n"The first five ring is for the Elf-lords'}]
1 つ以上の入力を持つ場合には、入力をリストとして渡します :generator( [ "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone", "Nine for Mortal Men, doomed to die, One for the Dark Lord on his dark throne", ] )
タスクのための任意の追加のパラメータも pipeline() に含めることができます。テキスト生成タスクは出力を制御するための幾つかのパラメータを持つ generate() メソッドを持ちます。例えば、1 つ以上の出力を生成したい場合には、num_return_sequences パラメータを設定します :[[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone and Seven for the Elven-kings for the elves under the ground in the sky to create the great world of Elves.\n\n'}], [{'generated_text': "Nine for Mortal Men, doomed to die, One for the Dark Lord on his dark throne,\n\nThe Dark Lord's last gift.\n\nI love you, my Son!\n\nI love you, Lord\n\nLord, you will"}]]
generator( "Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone", num_return_sequences=2, )
[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone."\n\n"That\'s what I do," said Gandalf, placing a hand on his head as though to say, "you'}, {'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone and Seven for the Night Elves and so there are plenty of names that I can think of.\n\n"So this is where we'}]
モデルとトークナイザーを選択する
pipeline() は モデルハブ から任意のモデルを受け取ります。モデルハブにはタグがあります、これはタスクのために使用したいモデルをフィルタリングすることを可能にします。適切なモデルを選択すれば、対応する AutoModelFor と AutoTokenizer クラスでそれをロードします。例えば、因果言語モデリング・タスクのために AutoModelForCausalLM クラスをロードします :
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
model = AutoModelForCausalLM.from_pretrained("distilgpt2")
タスクのために pipeline() を作成して、ロードしたモデルとトークナイザーを指定します :
from transformers import pipeline
generator = pipeline(task="text-generation", model=model, tokenizer=tokenizer)
あるテキストを生成するために入力テキストを pipeline() に渡します :
generator("Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone")
[{'generated_text': 'Three Rings for the Elven-kings under the sky, Seven for the Dwarf-lords in their halls of stone, and the Iron-crown-crown-godden of a few thousand feet. To see how many of them have'}]
音声パイプライン
pipeline() の柔軟性はつまりそれは音声タスクにも拡張できます。
例えば、 John F. Kennedy の有名な “We choose to go to the Moon” スピーチの短いクリップから感情を分類しましょう。モデルハブで感情認識のための 音声分類 モデルを見つけてそれを pipeline() でロードします :
from transformers import pipeline
audio_classifier = pipeline(
task="audio-classification", model="ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition"
)
音声ファイルを pipeline() に渡します :
audio_classifier("jfk_moon_speech.wav")
[{'label': 'calm', 'score': 0.13856211304664612}, {'label': 'disgust', 'score': 0.13148026168346405}, {'label': 'happy', 'score': 0.12635163962841034}, {'label': 'angry', 'score': 0.12439591437578201}, {'label': 'fearful', 'score': 0.12404385954141617}]
ビジョン・パイプライン
最後に、ビジョンタスクのために pipeline() を使用することは実際上、同じです。
ビジョンタスクを指定して画像を分類器に渡します。画像はリンクか画像へのローカルパスであり得ます。例えば、何の種の猫が下で示されるでしょう?
from transformers import pipeline
vision_classifier = pipeline(task="image-classification")
vision_classifier(
images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
)
[{'label': 'lynx, catamount', 'score': 0.4403027892112732}, {'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor', 'score': 0.03433405980467796}, {'label': 'snow leopard, ounce, Panthera uncia', 'score': 0.032148055732250214}, {'label': 'Egyptian cat', 'score': 0.02353910356760025}, {'label': 'tiger cat', 'score': 0.023034192621707916}]
以上