From 69715f2ee0ddee807498e8d2ea432d2579c5f5c0 Mon Sep 17 00:00:00 2001 From: Nicolas Patry Date: Wed, 16 Nov 2022 17:14:48 +0100 Subject: [PATCH] Adding doctest for `zero-shot-image-classification` pipeline. (#20272) * Adding doctest for `zero-shot-image-classification` pipeline. * Remove nested_simplify. --- .../zero_shot_image_classification.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/transformers/pipelines/zero_shot_image_classification.py b/src/transformers/pipelines/zero_shot_image_classification.py index 0256f00a3f..f9ebf7bb8e 100644 --- a/src/transformers/pipelines/zero_shot_image_classification.py +++ b/src/transformers/pipelines/zero_shot_image_classification.py @@ -33,6 +33,27 @@ class ZeroShotImageClassificationPipeline(ChunkPipeline): Zero shot image classification pipeline using `CLIPModel`. This pipeline predicts the class of an image when you provide an image and a set of `candidate_labels`. + Example: + + ```python + >>> from transformers import pipeline + + >>> classifier = pipeline(model="openai/clip-vit-large-patch14") + >>> classifier( + ... "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png", + ... candidate_labels=["animals", "humans", "landscape"], + ... ) + [{'score': 0.965, 'label': 'animals'}, {'score': 0.03, 'label': 'humans'}, {'score': 0.005, 'label': 'landscape'}] + + >>> classifier( + ... "https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png", + ... candidate_labels=["black and white", "photorealist", "painting"], + ... ) + [{'score': 0.996, 'label': 'black and white'}, {'score': 0.003, 'label': 'photorealist'}, {'score': 0.0, 'label': 'painting'}] + ``` + + [Learn more about the basics of using a pipeline in the [pipeline tutorial]](../pipeline_tutorial) + This image classification pipeline can currently be loaded from [`pipeline`] using the following task identifier: `"zero-shot-image-classification"`.