diff --git a/docs/source/ja/_toctree.yml b/docs/source/ja/_toctree.yml index 455207ccc0..a8a01dbd9c 100644 --- a/docs/source/ja/_toctree.yml +++ b/docs/source/ja/_toctree.yml @@ -29,8 +29,6 @@ - sections: - isExpanded: false sections: - - local: tasks/sequence_classification - title: テキストの分類 - local: tasks/token_classification title: トークンの分類 - local: tasks/question_answering diff --git a/docs/source/ja/model_doc/albert.md b/docs/source/ja/model_doc/albert.md index 00403ea537..a77e17a640 100644 --- a/docs/source/ja/model_doc/albert.md +++ b/docs/source/ja/model_doc/albert.md @@ -47,7 +47,7 @@ ALBERTモデルは、「[ALBERT: A Lite BERT for Self-supervised Learning of Lan ## 参考資料 -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問応答タスクガイド](../tasks/question_answering) - [マスクされた言語モデルタスクガイド](../tasks/masked_language_modeling) diff --git a/docs/source/ja/model_doc/bart.md b/docs/source/ja/model_doc/bart.md index 5c71da37f0..5c25d6a0c7 100644 --- a/docs/source/ja/model_doc/bart.md +++ b/docs/source/ja/model_doc/bart.md @@ -129,7 +129,7 @@ BART を始めるのに役立つ公式 Hugging Face およびコミュニティ - [翻訳タスクガイド](../tasks/translation) 以下も参照してください。 -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) - [抽出されたチェックポイント](https://huggingface.co/models?search=distilbart) は、この [論文](https://arxiv.org/abs/2010.13002) で説明されています。 diff --git a/docs/source/ja/model_doc/bert.md b/docs/source/ja/model_doc/bert.md index d34df9c0d5..6e6947bd04 100644 --- a/docs/source/ja/model_doc/bert.md +++ b/docs/source/ja/model_doc/bert.md @@ -76,7 +76,7 @@ BERT を始めるのに役立つ公式 Hugging Face およびコミュニティ - [`BertForSequenceClassification`] は、この [サンプル スクリプト](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification) および [ノートブック](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)。 - [`TFBertForSequenceClassification`] は、この [サンプル スクリプト](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification) および [ノートブック](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)。 - [`FlaxBertForSequenceClassification`] は、この [サンプル スクリプト](https://github.com/huggingface/transformers/tree/main/examples/flax/text-classification) および [ノートブック](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification_flax.ipynb)。 -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) diff --git a/docs/source/ja/model_doc/big_bird.md b/docs/source/ja/model_doc/big_bird.md index 4c0dabbebb..960d19146c 100644 --- a/docs/source/ja/model_doc/big_bird.md +++ b/docs/source/ja/model_doc/big_bird.md @@ -58,7 +58,7 @@ BigBird は、質問応答や要約などのさまざまな NLP タスクのパ ## ドキュメント リソース -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) diff --git a/docs/source/ja/model_doc/bigbird_pegasus.md b/docs/source/ja/model_doc/bigbird_pegasus.md index e0132b4b5f..5314aed1bc 100644 --- a/docs/source/ja/model_doc/bigbird_pegasus.md +++ b/docs/source/ja/model_doc/bigbird_pegasus.md @@ -58,7 +58,7 @@ BigBird は、質問応答や要約などのさまざまな NLP タスクのパ ## ドキュメント リソース -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) - [翻訳タスクガイド](../tasks/translation) diff --git a/docs/source/ja/model_doc/bloom.md b/docs/source/ja/model_doc/bloom.md index 1ac9396fa8..f7cb66ab95 100644 --- a/docs/source/ja/model_doc/bloom.md +++ b/docs/source/ja/model_doc/bloom.md @@ -39,7 +39,7 @@ BLOOM を使い始めるのに役立つ公式 Hugging Face およびコミュニ 以下も参照してください。 - [因果言語モデリング タスク ガイド](../tasks/language_modeling) -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) diff --git a/docs/source/ja/model_doc/camembert.md b/docs/source/ja/model_doc/camembert.md index db8e0aa936..4f59700954 100644 --- a/docs/source/ja/model_doc/camembert.md +++ b/docs/source/ja/model_doc/camembert.md @@ -46,7 +46,7 @@ Bi-direction Encoders for Transformers (BERT) のフランス語版である Cam ## Resources -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) diff --git a/docs/source/ja/model_doc/canine.md b/docs/source/ja/model_doc/canine.md index 18af699967..b45f1e4f7e 100644 --- a/docs/source/ja/model_doc/canine.md +++ b/docs/source/ja/model_doc/canine.md @@ -98,7 +98,7 @@ CANINE は生の文字で動作するため、**トークナイザーなし**で ## Resources -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [多肢選択タスク ガイド](../tasks/multiple_choice) diff --git a/docs/source/ja/model_doc/convbert.md b/docs/source/ja/model_doc/convbert.md index 5d15f86c51..7d790e4069 100644 --- a/docs/source/ja/model_doc/convbert.md +++ b/docs/source/ja/model_doc/convbert.md @@ -53,7 +53,7 @@ ConvBERT トレーニングのヒントは BERT のヒントと似ています ## Resources -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [マスクされた言語モデリング タスク ガイド](../tasks/masked_lang_modeling) diff --git a/docs/source/ja/model_doc/ctrl.md b/docs/source/ja/model_doc/ctrl.md index f93345d30e..e20e49d918 100644 --- a/docs/source/ja/model_doc/ctrl.md +++ b/docs/source/ja/model_doc/ctrl.md @@ -61,7 +61,7 @@ CTRL モデルは、Nitish Shirish Keskar*、Bryan McCann*、Lav R. Varshney、C ## Resources -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) ## CTRLConfig diff --git a/docs/source/ja/model_doc/data2vec.md b/docs/source/ja/model_doc/data2vec.md index 78ae71e694..c16d913881 100644 --- a/docs/source/ja/model_doc/data2vec.md +++ b/docs/source/ja/model_doc/data2vec.md @@ -58,7 +58,7 @@ Data2Vec の使用を開始するのに役立つ公式 Hugging Face およびコ - カスタム データセットで [`TFData2VecVisionForImageClassification`] を微調整するには、[このノートブック](https://colab.research.google.com/github/sayakpaul/TF-2.0-Hacks/blob/master/data2vec_vision_image_classification.ipynb) を参照してください。 )。 **Data2VecText ドキュメント リソース** -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [因果言語モデリング タスク ガイド](../tasks/language_modeling) diff --git a/docs/source/ja/model_doc/deberta-v2.md b/docs/source/ja/model_doc/deberta-v2.md index 35da9d0a1d..bdbaf3c21b 100644 --- a/docs/source/ja/model_doc/deberta-v2.md +++ b/docs/source/ja/model_doc/deberta-v2.md @@ -61,7 +61,7 @@ v2 の新機能: [kamalkraj](https://huggingface.co/kamalkraj) による投稿。元のコードは [こちら](https://github.com/microsoft/DeBERTa) にあります。 ## Resources -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) - [トークン分類タスクガイド](../tasks/token_classification) - [質問回答タスク ガイド](../tasks/question_answering) - [マスク言語モデリング タスク ガイド](../tasks/masked_language_modeling) diff --git a/docs/source/ja/model_doc/deberta.md b/docs/source/ja/model_doc/deberta.md index f7e00ad3b2..ef55a64581 100644 --- a/docs/source/ja/model_doc/deberta.md +++ b/docs/source/ja/model_doc/deberta.md @@ -52,7 +52,7 @@ DeBERTa を使い始めるのに役立つ公式 Hugging Face およびコミュ - DeBERTa による [機械学習によるスーパーチャージされた顧客サービス](https://huggingface.co/blog/supercharge-customer-service-with-machine-learning) に関するブログ投稿。 - [`DebertaForSequenceClassification`] は、この [サンプル スクリプト](https://github.com/huggingface/transformers/tree/main/examples/pytorch/text-classification) および [ノートブック](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification.ipynb)。 - [`TFDebertaForSequenceClassification`] は、この [サンプル スクリプト](https://github.com/huggingface/transformers/tree/main/examples/tensorflow/text-classification) および [ノートブック](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/text_classification-tf.ipynb)。 -- [テキスト分類タスクガイド](../tasks/sequence_classification) +- [テキスト分類タスクガイド(英語版)](../../en/tasks/sequence_classification) diff --git a/docs/source/ja/tasks/sequence_classification.md b/docs/source/ja/tasks/sequence_classification.md deleted file mode 100644 index ba2e39282b..0000000000 --- a/docs/source/ja/tasks/sequence_classification.md +++ /dev/null @@ -1,604 +0,0 @@ - - -# Sequence classification - -[[open-in-colab]] - - - -セマンティック セグメンテーションでは、画像の個々のピクセルにラベルまたはクラスを割り当てます。セグメンテーションにはいくつかのタイプがありますが、セマンティック セグメンテーションの場合、同じオブジェクトの一意のインスタンス間の区別は行われません。両方のオブジェクトに同じラベルが付けられます (たとえば、「car-1」と「car-2」の代わりに「car」)。セマンティック セグメンテーションの一般的な現実世界のアプリケーションには、歩行者や重要な交通情報を識別するための自動運転車のトレーニング、医療画像内の細胞と異常の識別、衛星画像からの環境変化の監視などが含まれます。 - -このガイドでは、次の方法を説明します。 - -1. [SceneParse150](https://huggingface.co/datasets/scene_parse_150) データセットの [SegFormer](https://huggingface.co/docs/transformers/main/en/model_doc/segformer#segformer) を微調整します。 -2. 微調整したモデルを推論に使用します。 - - - -このタスクと互換性のあるすべてのアーキテクチャとチェックポイントを確認するには、[タスクページ](https://huggingface.co/tasks/text-classification) を確認することをお勧めします。 - - - -始める前に、必要なライブラリがすべてインストールされていることを確認してください。 - -```bash -pip install -q datasets transformers evaluate -``` - -モデルをアップロードしてコミュニティと共有できるように、Hugging Face アカウントにログインすることをお勧めします。プロンプトが表示されたら、トークンを入力してログインします。 - -```py ->>> from huggingface_hub import notebook_login - ->>> notebook_login() -``` - -## Load SceneParse150 dataset - - -まず、SceneParse150 データセットの小さいサブセットを 🤗 データセット ライブラリから読み込みます。これにより、完全なデータセットのトレーニングにさらに時間を費やす前に、実験してすべてが機能することを確認する機会が得られます。 - -```py ->>> from datasets import load_dataset - ->>> ds = load_dataset("scene_parse_150", split="train[:50]") -``` - -[`~datasets.Dataset.train_test_split`] メソッドを使用して、データセットの `train` 分割をトレイン セットとテスト セットに分割します。 - -```py ->>> ds = ds.train_test_split(test_size=0.2) ->>> train_ds = ds["train"] ->>> test_ds = ds["test"] -``` - -次に、例を見てみましょう。 - -```py ->>> train_ds[0] -{'image': , - 'annotation': , - 'scene_category': 368} -``` - -- `image`: シーンの PIL イメージ。 -- `annotation`: セグメンテーション マップの PIL イメージ。モデルのターゲットでもあります。 -- `scene_category`: 「キッチン」や「オフィス」などの画像シーンを説明するカテゴリ ID。このガイドでは、「image」と「annotation」のみが必要になります。どちらも PIL イメージです。 - -また、ラベル ID をラベル クラスにマップする辞書を作成することもできます。これは、後でモデルを設定するときに役立ちます。ハブからマッピングをダウンロードし、`id2label` および `label2id` ディクショナリを作成します。 - -```py ->>> import json ->>> from pathlib import Path ->>> from huggingface_hub import hf_hub_download - ->>> repo_id = "huggingface/label-files" ->>> filename = "ade20k-id2label.json" ->>> id2label = json.loads(Path(hf_hub_download(repo_id, filename, repo_type="dataset")).read_text()) ->>> id2label = {int(k): v for k, v in id2label.items()} ->>> label2id = {v: k for k, v in id2label.items()} ->>> num_labels = len(id2label) -``` - -## Preprocess - -次のステップでは、SegFormer 画像プロセッサをロードして、モデルの画像と注釈を準備します。このデータセットのような一部のデータセットは、バックグラウンド クラスとしてゼロインデックスを使用します。ただし、実際には背景クラスは 150 個のクラスに含まれていないため、`do_reduce_labels=True`を設定してすべてのラベルから 1 つを引く必要があります。ゼロインデックスは `255` に置き換えられるため、SegFormer の損失関数によって無視されます。 - -```py ->>> from transformers import AutoImageProcessor - ->>> checkpoint = "nvidia/mit-b0" ->>> image_processor = AutoImageProcessor.from_pretrained(checkpoint, do_reduce_labels=True) -``` - - - - -モデルを過学習に対してより堅牢にするために、画像データセットにいくつかのデータ拡張を適用するのが一般的です。このガイドでは、[torchvision](https://pytorch.org) の [`ColorJitter`](https://pytorch.org/vision/stable/generated/torchvision.transforms.ColorJitter.html) 関数を使用します。 /vision/stable/index.html) を使用して画像の色のプロパティをランダムに変更しますが、任意の画像ライブラリを使用することもできます。 - -```py ->>> from torchvision.transforms import ColorJitter - ->>> jitter = ColorJitter(brightness=0.25, contrast=0.25, saturation=0.25, hue=0.1) -``` - -次に、モデルの画像と注釈を準備するための 2 つの前処理関数を作成します。これらの関数は、画像を`pixel_values`に変換し、注釈を`labels`に変換します。トレーニング セットの場合、画像を画像プロセッサに提供する前に`jitter`が適用されます。テスト セットの場合、テスト中にデータ拡張が適用されないため、画像プロセッサは`images`を切り取って正規化し、`labels` のみを切り取ります。 - -```py ->>> def train_transforms(example_batch): -... images = [jitter(x) for x in example_batch["image"]] -... labels = [x for x in example_batch["annotation"]] -... inputs = image_processor(images, labels) -... return inputs - - ->>> def val_transforms(example_batch): -... images = [x for x in example_batch["image"]] -... labels = [x for x in example_batch["annotation"]] -... inputs = image_processor(images, labels) -... return inputs -``` - -データセット全体に`jitter`を適用するには、🤗 Datasets [`~datasets.Dataset.set_transform`] 関数を使用します。変換はオンザフライで適用されるため、高速で消費するディスク容量が少なくなります。 - -```py ->>> train_ds.set_transform(train_transforms) ->>> test_ds.set_transform(val_transforms) -``` - - - - - - - -モデルを過学習に対してより堅牢にするために、画像データセットにいくつかのデータ拡張を適用するのが一般的です。 -このガイドでは、[`tf.image`](https://www.tensorflow.org/api_docs/python/tf/image) を使用して画像の色のプロパティをランダムに変更しますが、任意のプロパティを使用することもできます。画像 -好きな図書館。 -2 つの別々の変換関数を定義します。 -- 画像拡張を含むトレーニング データ変換 -- 🤗 Transformers のコンピューター ビジョン モデルはチャネル優先のレイアウトを想定しているため、画像を転置するだけの検証データ変換 - -```py ->>> import tensorflow as tf - - ->>> def aug_transforms(image): -... image = tf.keras.utils.img_to_array(image) -... image = tf.image.random_brightness(image, 0.25) -... image = tf.image.random_contrast(image, 0.5, 2.0) -... image = tf.image.random_saturation(image, 0.75, 1.25) -... image = tf.image.random_hue(image, 0.1) -... image = tf.transpose(image, (2, 0, 1)) -... return image - - ->>> def transforms(image): -... image = tf.keras.utils.img_to_array(image) -... image = tf.transpose(image, (2, 0, 1)) -... return image -``` - -次に、モデルの画像と注釈のバッチを準備する 2 つの前処理関数を作成します。これらの機能が適用されます -画像変換を行い、以前にロードされた `image_processor` を使用して画像を `pixel_values` に変換し、 -`labels`への注釈。 `ImageProcessor` は、画像のサイズ変更と正規化も処理します。 - -```py ->>> def train_transforms(example_batch): -... images = [aug_transforms(x.convert("RGB")) for x in example_batch["image"]] -... labels = [x for x in example_batch["annotation"]] -... inputs = image_processor(images, labels) -... return inputs - - ->>> def val_transforms(example_batch): -... images = [transforms(x.convert("RGB")) for x in example_batch["image"]] -... labels = [x for x in example_batch["annotation"]] -... inputs = image_processor(images, labels) -... return inputs -``` - -データセット全体に前処理変換を適用するには、🤗 Datasets [`~datasets.Dataset.set_transform`] 関数を使用します。 -変換はオンザフライで適用されるため、高速で消費するディスク容量が少なくなります。 - -```py ->>> train_ds.set_transform(train_transforms) ->>> test_ds.set_transform(val_transforms) -``` - - - -## Evaluate - -トレーニング中にメトリクスを含めると、多くの場合、モデルのパフォーマンスを評価するのに役立ちます。 🤗 [Evaluate](https://huggingface.co/docs/evaluate/index) ライブラリを使用して、評価メソッドをすばやくロードできます。このタスクでは、[Mean Intersection over Union](https://huggingface.co/spaces/evaluate-metric/accuracy) (IoU) メトリックをロードします (🤗 Evaluate [クイック ツアー](https://huggingface.co) を参照してください) /docs/evaluate/a_quick_tour) を参照して、メトリクスをロードして計算する方法の詳細を確認してください)。 - -```py ->>> import evaluate - ->>> metric = evaluate.load("mean_iou") -``` - -次に、メトリクスを [`~evaluate.EvaluationModule.compute`] する関数を作成します。予測を次のように変換する必要があります -最初にロジットを作成し、次に [`~evaluate.EvaluationModule.compute`] を呼び出す前にラベルのサイズに一致するように再形成します。 - - - - -```py ->>> import numpy as np ->>> import torch ->>> from torch import nn - ->>> def compute_metrics(eval_pred): -... with torch.no_grad(): -... logits, labels = eval_pred -... logits_tensor = torch.from_numpy(logits) -... logits_tensor = nn.functional.interpolate( -... logits_tensor, -... size=labels.shape[-2:], -... mode="bilinear", -... align_corners=False, -... ).argmax(dim=1) - -... pred_labels = logits_tensor.detach().cpu().numpy() -... metrics = metric.compute( -... predictions=pred_labels, -... references=labels, -... num_labels=num_labels, -... ignore_index=255, -... reduce_labels=False, -... ) -... for key, value in metrics.items(): -... if type(value) is np.ndarray: -... metrics[key] = value.tolist() -... return metrics -``` - - - - - - - - -```py ->>> def compute_metrics(eval_pred): -... logits, labels = eval_pred -... logits = tf.transpose(logits, perm=[0, 2, 3, 1]) -... logits_resized = tf.image.resize( -... logits, -... size=tf.shape(labels)[1:], -... method="bilinear", -... ) - -... pred_labels = tf.argmax(logits_resized, axis=-1) -... metrics = metric.compute( -... predictions=pred_labels, -... references=labels, -... num_labels=num_labels, -... ignore_index=-1, -... reduce_labels=image_processor.do_reduce_labels, -... ) - -... per_category_accuracy = metrics.pop("per_category_accuracy").tolist() -... per_category_iou = metrics.pop("per_category_iou").tolist() - -... metrics.update({f"accuracy_{id2label[i]}": v for i, v in enumerate(per_category_accuracy)}) -... metrics.update({f"iou_{id2label[i]}": v for i, v in enumerate(per_category_iou)}) -... return {"val_" + k: v for k, v in metrics.items()} -``` - - - - -これで`compute_metrics`関数の準備が整いました。トレーニングをセットアップするときにこの関数に戻ります。 - -## Train - - - - -[`Trainer`] を使用したモデルの微調整に慣れていない場合は、[こちら](../training#finetune-with-trainer) の基本的なチュートリアルをご覧ください。 - - - - -これでモデルのトレーニングを開始する準備が整いました。 [`AutoModelForSemanticSegmentation`] を使用して SegFormer をロードし、ラベル ID とラベル クラス間のマッピングをモデルに渡します。 - -```py ->>> from transformers import AutoModelForSemanticSegmentation, TrainingArguments, Trainer - ->>> model = AutoModelForSemanticSegmentation.from_pretrained(checkpoint, id2label=id2label, label2id=label2id) -``` - -この時点で残っている手順は次の 3 つだけです。 - -1. [`TrainingArguments`] でトレーニング ハイパーパラメータを定義します。 `image` 列が削除されるため、未使用の列を削除しないことが重要です。 `image` 列がないと、`pixel_values` を作成できません。この動作を防ぐには、`remove_unused_columns=False`を設定してください。他に必要なパラメータは、モデルの保存場所を指定する `output_dir` だけです。 `push_to_hub=True`を設定して、このモデルをハブにプッシュします (モデルをアップロードするには、Hugging Face にサインインする必要があります)。各エポックの終了時に、[`Trainer`] は IoU メトリックを評価し、トレーニング チェックポイントを保存します。 -2. トレーニング引数を、モデル、データセット、トークナイザー、データ照合器、および `compute_metrics` 関数とともに [`Trainer`] に渡します。 -3. [`~Trainer.train`] を呼び出してモデルを微調整します。 - - -```py ->>> training_args = TrainingArguments( -... output_dir="segformer-b0-scene-parse-150", -... learning_rate=6e-5, -... num_train_epochs=50, -... per_device_train_batch_size=2, -... per_device_eval_batch_size=2, -... save_total_limit=3, -... eval_strategy="steps", -... save_strategy="steps", -... save_steps=20, -... eval_steps=20, -... logging_steps=1, -... eval_accumulation_steps=5, -... remove_unused_columns=False, -... push_to_hub=True, -... ) - ->>> trainer = Trainer( -... model=model, -... args=training_args, -... train_dataset=train_ds, -... eval_dataset=test_ds, -... compute_metrics=compute_metrics, -... ) - ->>> trainer.train() -``` - -トレーニングが完了したら、 [`~transformers.Trainer.push_to_hub`] メソッドを使用してモデルをハブに共有し、誰もがモデルを使用できるようにします。 - -```py ->>> trainer.push_to_hub() -``` - - - - - - - -Keras を使用したモデルの微調整に慣れていない場合は、まず [基本チュートリアル](./training#train-a-tensorflow-model-with-keras) を確認してください。 - - - -TensorFlow でモデルを微調整するには、次の手順に従います。 -1. トレーニングのハイパーパラメータを定義し、オプティマイザーと学習率スケジュールを設定します。 -2. 事前トレーニングされたモデルをインスタンス化します。 -3. 🤗 データセットを `tf.data.Dataset` に変換します。 -4. モデルをコンパイルします。 -5. コールバックを追加してメトリクスを計算し、モデルを 🤗 Hub にアップロードします -6. `fit()` メソッドを使用してトレーニングを実行します。 - -まず、ハイパーパラメーター、オプティマイザー、学習率スケジュールを定義します。 - - -```py ->>> from transformers import create_optimizer - ->>> batch_size = 2 ->>> num_epochs = 50 ->>> num_train_steps = len(train_ds) * num_epochs ->>> learning_rate = 6e-5 ->>> weight_decay_rate = 0.01 - ->>> optimizer, lr_schedule = create_optimizer( -... init_lr=learning_rate, -... num_train_steps=num_train_steps, -... weight_decay_rate=weight_decay_rate, -... num_warmup_steps=0, -... ) -``` - -次に、ラベル マッピングとともに [`TFAutoModelForSemanticSegmentation`] を使用して SegFormer をロードし、それをコンパイルします。 -オプティマイザ。 Transformers モデルにはすべてデフォルトのタスク関連の損失関数があるため、次の場合を除き、損失関数を指定する必要はないことに注意してください。 - -```py ->>> from transformers import TFAutoModelForSemanticSegmentation - ->>> model = TFAutoModelForSemanticSegmentation.from_pretrained( -... checkpoint, -... id2label=id2label, -... label2id=label2id, -... ) ->>> model.compile(optimizer=optimizer) # No loss argument! -``` - -[`~datasets.Dataset.to_tf_dataset`] と [`DefaultDataCollat​​or`] を使用して、データセットを `tf.data.Dataset` 形式に変換します。 - -```py ->>> from transformers import DefaultDataCollator - ->>> data_collator = DefaultDataCollator(return_tensors="tf") - ->>> tf_train_dataset = train_ds.to_tf_dataset( -... columns=["pixel_values", "label"], -... shuffle=True, -... batch_size=batch_size, -... collate_fn=data_collator, -... ) - ->>> tf_eval_dataset = test_ds.to_tf_dataset( -... columns=["pixel_values", "label"], -... shuffle=True, -... batch_size=batch_size, -... collate_fn=data_collator, -... ) -``` - -予測から精度を計算し、モデルを 🤗 ハブにプッシュするには、[Keras callbacks](../main_classes/keras_callbacks) を使用します。 -`compute_metrics` 関数を [`KerasMetricCallback`] に渡します。 -そして [`PushToHubCallback`] を使用してモデルをアップロードします。 - -```py ->>> from transformers.keras_callbacks import KerasMetricCallback, PushToHubCallback - ->>> metric_callback = KerasMetricCallback( -... metric_fn=compute_metrics, eval_dataset=tf_eval_dataset, batch_size=batch_size, label_cols=["labels"] -... ) - ->>> push_to_hub_callback = PushToHubCallback(output_dir="scene_segmentation", image_processor=image_processor) - ->>> callbacks = [metric_callback, push_to_hub_callback] -``` - -ついに、モデルをトレーニングする準備が整いました。`fit()`トレーニングおよび検証データセット、エポック数、 -モデルを微調整するためのコールバック: - -```py ->>> model.fit( -... tf_train_dataset, -... validation_data=tf_eval_dataset, -... callbacks=callbacks, -... epochs=num_epochs, -... ) -``` - -おめでとう!モデルを微調整し、🤗 Hub で共有しました。これで推論に使用できるようになりました。 - - - - - -## Inference - -モデルを微調整したので、それを推論に使用できるようになりました。 - -推論のために画像をロードします。 - -```py ->>> image = ds[0]["image"] ->>> image -``` - -
- Image of bedroom -
- - - - -推論用に微調整されたモデルを試す最も簡単な方法は、それを [`pipeline`] で使用することです。モデルを使用して画像セグメンテーション用の `pipeline` をインスタンス化し、それに画像を渡します。 - -```py ->>> from transformers import pipeline - ->>> segmenter = pipeline("image-segmentation", model="my_awesome_seg_model") ->>> segmenter(image) -[{'score': None, - 'label': 'wall', - 'mask': }, - {'score': None, - 'label': 'sky', - 'mask': }, - {'score': None, - 'label': 'floor', - 'mask': }, - {'score': None, - 'label': 'ceiling', - 'mask': }, - {'score': None, - 'label': 'bed ', - 'mask': }, - {'score': None, - 'label': 'windowpane', - 'mask': }, - {'score': None, - 'label': 'cabinet', - 'mask': }, - {'score': None, - 'label': 'chair', - 'mask': }, - {'score': None, - 'label': 'armchair', - 'mask': }] -``` - -必要に応じて、`pipeline` の結果を手動で複製することもできます。画像プロセッサで画像を処理し、`pixel_values`を GPU に配置します。 - -```py ->>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # use GPU if available, otherwise use a CPU ->>> encoding = image_processor(image, return_tensors="pt") ->>> pixel_values = encoding.pixel_values.to(device) -``` - -入力をモデルに渡し、「logits」を返します。 - -```py ->>> outputs = model(pixel_values=pixel_values) ->>> logits = outputs.logits.cpu() -``` - -次に、ロジットを元の画像サイズに再スケールします。 - - -```py ->>> upsampled_logits = nn.functional.interpolate( -... logits, -... size=image.size[::-1], -... mode="bilinear", -... align_corners=False, -... ) - ->>> pred_seg = upsampled_logits.argmax(dim=1)[0] -``` - - - - - - - -画像プロセッサをロードして画像を前処理し、入力を TensorFlow テンソルとして返します。 - -```py ->>> from transformers import AutoImageProcessor - ->>> image_processor = AutoImageProcessor.from_pretrained("MariaK/scene_segmentation") ->>> inputs = image_processor(image, return_tensors="tf") -``` - -入力をモデルに渡し、`logits`を返します。 - -```py ->>> from transformers import TFAutoModelForSemanticSegmentation - ->>> model = TFAutoModelForSemanticSegmentation.from_pretrained("MariaK/scene_segmentation") ->>> logits = model(**inputs).logits -``` - -次に、ロジットを元の画像サイズに再スケールし、クラス次元に argmax を適用します。 - -```py ->>> logits = tf.transpose(logits, [0, 2, 3, 1]) - ->>> upsampled_logits = tf.image.resize( -... logits, -... # We reverse the shape of `image` because `image.size` returns width and height. -... image.size[::-1], -... ) - ->>> pred_seg = tf.math.argmax(upsampled_logits, axis=-1)[0] -``` - - - - -結果を視覚化するには、[データセット カラー パレット](https://github.com/tensorflow/models/blob/3f1ca33afe3c1631b733ea7e40c294273b9e406d/research/deeplab/utils/get_dataset_colormap.py#L51) を、それぞれをマップする `ade_palette()` としてロードします。クラスを RGB 値に変換します。次に、画像と予測されたセグメンテーション マップを組み合わせてプロットできます。 - -```py ->>> import matplotlib.pyplot as plt ->>> import numpy as np - ->>> color_seg = np.zeros((pred_seg.shape[0], pred_seg.shape[1], 3), dtype=np.uint8) ->>> palette = np.array(ade_palette()) ->>> for label, color in enumerate(palette): -... color_seg[pred_seg == label, :] = color ->>> color_seg = color_seg[..., ::-1] # convert to BGR - ->>> img = np.array(image) * 0.5 + color_seg * 0.5 # plot the image with the segmentation map ->>> img = img.astype(np.uint8) - ->>> plt.figure(figsize=(15, 10)) ->>> plt.imshow(img) ->>> plt.show() -``` - -
- Image of bedroom overlaid with segmentation map -
diff --git a/docs/source/ja/tasks_explained.md b/docs/source/ja/tasks_explained.md index 7c027e7a73..bdfb3ec0ac 100644 --- a/docs/source/ja/tasks_explained.md +++ b/docs/source/ja/tasks_explained.md @@ -221,7 +221,7 @@ Transformerは最初に機械翻訳のために設計され、それ以降、ほ 事前訓練済みモデルをテキスト分類に使用するには、ベースのBERTモデルの上にシーケンス分類ヘッドを追加します。シーケンス分類ヘッドは最終的な隠れた状態を受け入れ、それらをロジットに変換するための線形層です。クロスエントロピー損失は、ロジットとターゲット間で最も可能性の高いラベルを見つけるために計算されます。 -テキスト分類を試してみる準備はできましたか?DistilBERTを微調整し、推論に使用する方法を学ぶために、完全な[テキスト分類ガイド](tasks/sequence_classification)をチェックしてみてください! +テキスト分類を試してみる準備はできましたか?DistilBERTを微調整し、推論に使用する方法を学ぶために、完全な[テキスト分類ガイド(英語版)](../en/tasks/sequence_classification)をチェックしてみてください! ### Token classification