Transformers_image_example.ipynb Open in SWAN Download

Transformers image processing examples

Transformers provides APIs and tools to easily download and train state-of-the-art pretrained models. Credits: Huggingface documentation

Image classification

In [1]:
from transformers import pipeline
In [2]:
# Create the image classification pipeline with GPU
vision_classifier = pipeline(model="google/vit-base-patch16-224", device=0)  # Specify the GPU device index
Downloading (…)lve/main/config.json:   0%|          | 0.00/69.7k [00:00<?, ?B/s]
Downloading pytorch_model.bin:   0%|          | 0.00/346M [00:00<?, ?B/s]
Downloading (…)rocessor_config.json:   0%|          | 0.00/160 [00:00<?, ?B/s]
In [3]:
image_url="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
In [4]:
from IPython.display import Image
Image(url=image_url)
Out[4]:
No description has been provided for this image
In [5]:
preds = vision_classifier(image_url)
preds = [{"score": round(pred["score"], 4), "label": pred["label"]} for pred in preds]
preds
Out[5]:
[{'score': 0.4335, 'label': 'lynx, catamount'},
 {'score': 0.0348,
  'label': 'cougar, puma, catamount, mountain lion, painter, panther, Felis concolor'},
 {'score': 0.0324, 'label': 'snow leopard, ounce, Panthera uncia'},
 {'score': 0.0239, 'label': 'Egyptian cat'},
 {'score': 0.0229, 'label': 'tiger cat'}]
In [ ]: