Downloads · 30 days
7.2K
1% of all-time downloads
SamLowe/roberta-base-go_emotions-onnx
roberta-base-go_emotions-onnx is a text classification model from SamLowe. Use it when you need a label for a piece of text. It is set up for transformers. The card lists the license as mit.
This model is the ONNX version of https://huggingface.co/SamLowe/roberta-base-goemotions.
Downloads · 30 days
7.2K
1% of all-time downloads
All-time downloads
1.3M
Public
Repo size
624 MB
Likes
27
Public
Click a slice to open those files.
.onnx624 MB · 99%
From the Hugging Face model README
This model is the ONNX version of https://huggingface.co/SamLowe/roberta-base-go_emotions.
onnx/model.onnx is the full precision ONNX version
Using a fixed threshold of 0.5 to convert the scores to binary predictions for each label:
See more details in the SamLowe/roberta-base-go_emotions model card for the increases possible through selecting label-specific thresholds to maximise F1 scores, or another metric.
onnx/model_quantized.onnx is the int8 quantized version
Using a fixed threshold of 0.5 to convert the scores to binary predictions for each label:
Note how the metrics are almost identical to the full precision metrics above.
See more details in the SamLowe/roberta-base-go_emotions model card for the increases possible through selecting label-specific thresholds to maximise F1 scores, or another metric.
Optimum library has equivalents (starting ORT) for the main Transformers classes, so these models can be used with the familiar constructs. The only extra property needed is file_name on the model creation, which in the below example specifies the quantized (INT8) model.
sentences = ["ONNX is seriously fast for small batches. Impressive"]
from transformers import AutoTokenizer, pipeline
from optimum.onnxruntime import ORTModelForSequenceClassification
model_id = "SamLowe/roberta-base-go_emotions-onnx"
file_name = "onnx/model_quantized.onnx"
model = ORTModelForSequenceClassification.from_pretrained(model_id, file_name=file_name)
tokenizer = AutoTokenizer.from_pretrained(model_id)
onnx_classifier = pipeline(
task="text-classification",
model=model,
tokenizer=tokenizer,
top_k=None,
function_to_apply="sigmoid", # optional as is the default for the task
)
model_outputs = onnx_classifier(sentences)
# gives a list of outputs, each a list of dicts (one per label)
print(model_outputs)
# E.g.
# [[{'label': 'admiration', 'score': 0.9203393459320068},
# {'label': 'approval', 'score': 0.0560273639857769},
# {'label': 'neutral', 'score': 0.04265536740422249},
# {'label': 'gratitude', 'score': 0.015126707963645458},
# ...
tokenizers library,from tokenizers import Tokenizer
import onnxruntime as ort
from os import cpu_count
import numpy as np # only used for the postprocessing sigmoid
sentences = ["hello world"] # for example a batch of 1
# labels as (ordered) list - from the go_emotions dataset
labels = ['admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion', 'curiosity', 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment', 'excitement', 'fear', 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism', 'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral']
tokenizer = Tokenizer.from_pretrained("SamLowe/roberta-base-go_emotions")
# Optional - set pad to only pad to longest in batch, not a fixed length.
# (without this, the model will run slower, esp for shorter input strings)
params = {**tokenizer.padding, "length": None}
tokenizer.enable_padding(**params)
tokens_obj = tokenizer.encode_batch(sentences)
def load_onnx_model(model_filepath):
_options = ort.SessionOptions()
_options.inter_op_num_threads, _options.intra_op_num_threads = cpu_count(), cpu_count()
_providers = ["CPUExecutionProvider"] # could use ort.get_available_providers()
return ort.InferenceSession(path_or_bytes=model_filepath, sess_options=_options, providers=_providers)
model = load_onnx_model("path_to_model_dot_onnx_or_model_quantized_dot_onnx")
output_names = [model.get_outputs()[0].name] # E.g. ["logits"]
input_feed_dict = {
"input_ids": [t.ids for t in tokens_obj],
"attention_mask": [t.attention_mask for t in tokens_obj]
}
logits = model.run(output_names=output_names, input_feed=input_feed_dict)[0]
# produces a numpy array, one row per input item, one col per label
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
# Post-processing. Gets the scores per label in range.
# Auto done by Transformers' pipeline, but we must do it manually with ORT.
model_outputs = sigmoid(logits)
# for example, just to show the top result per input item
for probas in model_outputs:
top_result_index = np.argmax(probas)
print(labels[top_result_index], "with score:", probas[top_result_index])
Notebook with more details to follow.