This lesson is being piloted (Beta version)

Using Pre-trained model from HuggingFace

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • How to use pre-trained model already available from Hugging Face hub

Objectives
  • To master the usage of pre-trained deep learning model from Hugging Face

Hugging Face hub

Transformers library

Model task

The screenshot below describes the model task from Hugging Face that covers many different aspecs from Computer Vision to NLP, Audio or Reinforcement Learning image

Pipeline for inference

Pipeline for NLP Sentiment Analysis

from transformers import pipeline

classifier = pipeline("sentiment-analysis")
classifier("I am so excited to use the new SuperPOD from NVIDIA")

[{'label': 'POSITIVE', 'score': 0.9995261430740356}]
classifier(
    ["I am so excited to use the new SuperPOD from NVIDIA", "I hate running late"])

[{'label': 'POSITIVE', 'score': 0.9995261430740356},
 {'label': 'NEGATIVE', 'score': 0.9943193197250366}]

Pipeline Text Generation

from transformers import pipeline
generator = pipeline("text-generation")
generator("Using SMU latest HPC cluster NVIDIA SuperPOD,  you will be able to")

[{'generated_text': 'Using SMU latest HPC cluster NVIDIA SuperPOD,  you will be able to connect to other SSE nodes such as the following and use them as a HPC node:\n\n[CPU: CPU1, GIGABYTE'}]

Pipeline for Mask filling

from transformers import pipeline

unmasker = pipeline("fill-mask")
unmasker("This course will teach you all about <mask> models.", top_k=2)

[{'score': 0.19619698822498322,
  'token': 30412,
  'token_str': ' mathematical',
  'sequence': 'This course will teach you all about mathematical models.'},
 {'score': 0.04052705690264702,
  'token': 38163,
  'token_str': ' computational',
  'sequence': 'This course will teach you all about computational models.'}]

Pipeline for Name Entity Recognition

from transformers import pipeline

ner = pipeline("ner", grouped_entities=True)
ner("My name is Tue Vu and I work at SMU in Dallas")

[{'entity_group': 'PER',
  'score': 0.9868829,
  'word': 'Tue Vu',
  'start': 11,
  'end': 17},
 {'entity_group': 'ORG',
  'score': 0.9965092,
  'word': 'SMU',
  'start': 32,
  'end': 35},
 {'entity_group': 'LOC',
  'score': 0.9950755,
  'word': 'Dallas',
  'start': 39,
  'end': 45}]

Pipeline for Question Answering

from transformers import pipeline

question_answerer = pipeline("question-answering")
question_answerer(
    question="Where do I work?",
    context="My name is Tue Vu and I work at SMU in Dallas",
)

{'score': 0.3651700019836426, 'start': 32, 'end': 35, 'answer': 'SMU'}

Pipeline for Conversational

from transformers import pipeline, Conversation
converse = pipeline("conversational")

conversation_1 = Conversation("What do you think about using HPC SuperPOD")
conversation_2 = Conversation("Do you believe in God?")
converse([conversation_1, conversation_2])

Answer:

[Conversation id: 44cf473c-29f2-4b44-be6c-15352dab13a2 
 user >> What do you think about using HPC SuperPOD 
 bot >> I think it's a good idea, but I don't think it's a good idea to use it for a lot of things. ,
 Conversation id: 489d923c-f127-4847-8cde-972c77470230 
 user >> What do you do to optimize the Python workflow? 
 bot >> I believe in the power of love.]

Pipeline for Computer Vision - Image Classification

from transformers import pipeline
clf = pipeline("image-classification")

Display the image:

import urllib.request
from io import BytesIO

url = 'https://t4.ftcdn.net/jpg/02/66/72/41/360_F_266724172_Iy8gdKgMa7XmrhYYxLCxyhx6J7070Pr8.jpg'
with urllib.request.urlopen(url) as url:
    img = Image.open(BytesIO(url.read()))
img

360_F_266724172_Iy8gdKgMa7XmrhYYxLCxyhx6J7070Pr8

Model Inference

clf(img)

[{'score': 0.49216628074645996, 'label': 'Egyptian cat'},
 {'score': 0.41306015849113464, 'label': 'tabby, tabby cat'},
 {'score': 0.050162095576524734, 'label': 'tiger cat'},
 {'score': 0.012556081637740135, 'label': 'lynx, catamount'},
 {'score': 0.00524393143132329, 'label': 'ping-pong ball'}]

Key Points

  • Hugging Face, pre-trained, pipeline