From 87da59576a3929cc3e397e2036a9c9781437d038 Mon Sep 17 00:00:00 2001 From: ydshieh Date: Tue, 29 Mar 2022 16:44:22 +0200 Subject: [PATCH 1/3] add code samples --- src/transformers/utils/doc.py | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/transformers/utils/doc.py b/src/transformers/utils/doc.py index 394d2aaa2fed..f395f8d4fb80 100644 --- a/src/transformers/utils/doc.py +++ b/src/transformers/utils/doc.py @@ -794,6 +794,52 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): ``` """ +TF_VISION_BASE_MODEL_SAMPLE = r""" + Example: + + ```python + >>> from transformers import {processor_class}, {model_class} + >>> from datasets import load_dataset + + >>> dataset = load_dataset("huggingface/cats-image") + >>> image = dataset["test"]["image"][0] + + >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}") + >>> model = {model_class}.from_pretrained("{checkpoint}") + + >>> inputs = feature_extractor(image, return_tensors="tf") + >>> outputs = model(**inputs) + + >>> last_hidden_states = outputs.last_hidden_state + >>> list(last_hidden_states.shape) + {expected_output} + ``` +""" + +TF_VISION_SEQ_CLASS_SAMPLE = r""" + Example: + + ```python + >>> from transformers import {processor_class}, {model_class} + >>> import tensorflow as tf + >>> from datasets import load_dataset + + >>> dataset = load_dataset("huggingface/cats-image") + >>> image = dataset["test"]["image"][0] + + >>> feature_extractor = {processor_class}.from_pretrained("{checkpoint}") + >>> model = {model_class}.from_pretrained("{checkpoint}") + + >>> inputs = feature_extractor(image, return_tensors="tf") + >>> logits = model(**inputs).logits + + >>> # model predicts one of the 1000 ImageNet classes + >>> predicted_label = int(tf.math.argmax(logits, axis=-1)) + >>> print(model.config.id2label[predicted_label]) + {expected_output} + ``` +""" + TF_SAMPLE_DOCSTRINGS = { "SequenceClassification": TF_SEQUENCE_CLASSIFICATION_SAMPLE, "QuestionAnswering": TF_QUESTION_ANSWERING_SAMPLE, @@ -802,6 +848,8 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): "MaskedLM": TF_MASKED_LM_SAMPLE, "LMHead": TF_CAUSAL_LM_SAMPLE, "BaseModel": TF_BASE_MODEL_SAMPLE, + "VisionBaseModel": TF_VISION_BASE_MODEL_SAMPLE, + "ImageClassification": TF_VISION_SEQ_CLASS_SAMPLE, } From bfe6a4316415ca08e1d187f9dda5e7d9dc3c70bc Mon Sep 17 00:00:00 2001 From: Yih-Dar <2521628+ydshieh@users.noreply.github.com> Date: Tue, 29 Mar 2022 17:44:49 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: NielsRogge <48327001+NielsRogge@users.noreply.github.com> --- src/transformers/utils/doc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/utils/doc.py b/src/transformers/utils/doc.py index f395f8d4fb80..f60d0eb977d7 100644 --- a/src/transformers/utils/doc.py +++ b/src/transformers/utils/doc.py @@ -816,7 +816,7 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): ``` """ -TF_VISION_SEQ_CLASS_SAMPLE = r""" +TF_IMAGE_CLASSIFICATION_SAMPLE = r""" Example: ```python @@ -849,7 +849,7 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): "LMHead": TF_CAUSAL_LM_SAMPLE, "BaseModel": TF_BASE_MODEL_SAMPLE, "VisionBaseModel": TF_VISION_BASE_MODEL_SAMPLE, - "ImageClassification": TF_VISION_SEQ_CLASS_SAMPLE, + "ImageClassification": TF_IMAGE_CLASSIFICATION_SAMPLE, } From 36b2e8280491b8735e4413ec587812ab7e51c73d Mon Sep 17 00:00:00 2001 From: ydshieh Date: Tue, 29 Mar 2022 17:58:17 +0200 Subject: [PATCH 3/3] revert to TF_VISION_SEQ_CLASS_SAMPLE --- src/transformers/utils/doc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transformers/utils/doc.py b/src/transformers/utils/doc.py index f60d0eb977d7..f395f8d4fb80 100644 --- a/src/transformers/utils/doc.py +++ b/src/transformers/utils/doc.py @@ -816,7 +816,7 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): ``` """ -TF_IMAGE_CLASSIFICATION_SAMPLE = r""" +TF_VISION_SEQ_CLASS_SAMPLE = r""" Example: ```python @@ -849,7 +849,7 @@ def _prepare_output_docstrings(output_type, config_class, min_indent=None): "LMHead": TF_CAUSAL_LM_SAMPLE, "BaseModel": TF_BASE_MODEL_SAMPLE, "VisionBaseModel": TF_VISION_BASE_MODEL_SAMPLE, - "ImageClassification": TF_IMAGE_CLASSIFICATION_SAMPLE, + "ImageClassification": TF_VISION_SEQ_CLASS_SAMPLE, }