Text Classification GoEmotions

This a ONNX quantized model and is fined-tuned version of MiniLMv2-L6-H384 on the on the go_emotions dataset using tasinho/text-classification-goemotions as teacher model. The original model can be found here

Optimum

Installation

Install from source:

python -m pip install optimum[onnxruntime]@git+https://github.com/huggingface/optimum.git

Run the Model

from optimum.onnxruntime import ORTModelForSequenceClassification
from transformers import AutoTokenizer, pipeline

model = ORTModelForSequenceClassification.from_pretrained('minuva/MiniLMv2-goemotions-v2-onnx', provider="CPUExecutionProvider")
tokenizer = AutoTokenizer.from_pretrained('minuva/MiniLMv2-goemotions-v2-onnx', use_fast=True, model_max_length=256, truncation=True, padding='max_length')

pipe = pipeline(task='text-classification', model=model, tokenizer=tokenizer, )
texts = ["that's wrong", "can you please answer me?"]
pipe(texts)
# [{'label': 'anger', 'score': 0.9727636575698853},
# {'label': 'love', 'score': 0.9874765276908875}]

ONNX Runtime only

A lighter solution for deployment

Installation

pip install tokenizers
pip install onnxruntime
git clone https://huggingface.co/minuva/MiniLMv2-goemotions-v2-onnx

Run the Model

import os
import numpy as np
import json

from tokenizers import Tokenizer
from onnxruntime import InferenceSession


model_name = "minuva/MiniLMv2-goemotions-v2-onnx"

tokenizer = Tokenizer.from_pretrained(model_name)
tokenizer.enable_padding(
    pad_token="<pad>",
    pad_id=1,
)
tokenizer.enable_truncation(max_length=256)
batch_size = 16

texts = ["I am angry", "I feel in love"]
outputs = []
model = InferenceSession("MiniLMv2-goemotions-v2-onnx/model_optimized_quantized.onnx", providers=['CUDAExecutionProvider'])

with open(os.path.join("MiniLMv2-goemotions-v2-onnx", "config.json"), "r") as f:
            config = json.load(f)

output_names = [output.name for output in model.get_outputs()]
input_names = [input.name for input in model.get_inputs()]

for subtexts in np.array_split(np.array(texts), len(texts) // batch_size + 1):
            encodings = tokenizer.encode_batch(list(subtexts))
            inputs = {
                "input_ids": np.vstack(
                    [encoding.ids for encoding in encodings],
                ),
                "attention_mask": np.vstack(
                    [encoding.attention_mask for encoding in encodings],
                ),
                "token_type_ids": np.vstack(
                    [encoding.type_ids for encoding in encodings],
                ),
            }

            for input_name in input_names:
                if input_name not in inputs:
                    raise ValueError(f"Input name {input_name} not found in inputs")

            inputs = {input_name: inputs[input_name] for input_name in input_names}
            output = np.squeeze(
                np.stack(
                    model.run(output_names=output_names, input_feed=inputs)
                ),
                axis=0,
            )
            outputs.append(output)

outputs = np.concatenate(outputs, axis=0)
scores = 1 / (1 + np.exp(-outputs))
results = []
for item in scores:
    labels = []
    scores = []
    for idx, s in enumerate(item):
        labels.append(config["id2label"][str(idx)])
        scores.append(float(s))
    results.append({"labels": labels, "scores": scores})


res = []

for result in results:
    joined = list(zip(result['labels'], result['scores']))
    max_score = max(joined, key=lambda x: x[1])    
    res.append(max_score)

res

# [('anger', 0.9745745062828064), ('love', 0.9884329438209534)]

Training hyperparameters

The following hyperparameters were used during training:

  • learning_rate: 6e-05
  • train_batch_size: 64
  • eval_batch_size: 64
  • seed: 42
  • optimizer: Adam with betas=(0.9,0.999) and epsilon=1e-08
  • lr_scheduler_type: linear

Metrics (comparison with teacher model)

Teacher (params) Student (params) Set Score (teacher) Score (student)
tasinhoque/text-classification-goemotions (355M) MiniLMv2-goemotions-v2-onnx (30M) Validation 0.514252 0.4780
tasinhoque/text-classification-goemotions (335M) MiniLMv2-goemotions-v2-onnx (30M) Test 0.501937 0.482

Deployment

Check out our fast-nlp-text-emotion repository for a FastAPI based server to easily deploy this model on CPU devices.

Downloads last month
69
Inference Examples
Inference API (serverless) has been turned off for this model.

Dataset used to train minuva/MiniLMv2-goemotions-v2-onnx

Collection including minuva/MiniLMv2-goemotions-v2-onnx

Evaluation results