File size: 7,729 Bytes
ed51b39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
---
license: apache-2.0
library_name: litert
pipeline_tag: text-classification
language:
- en
tags:
- mediapipe
- litert
- tflite
- text-classification
- on-device
- android
- ios
- web
- python
- flutter
- nlp
- mobilebert
- sentiment-analysis
- sst-2
- dynamic-range
- arxiv:2004.02984
---

# MediaPipe Text Classifier (MobileBERT)

On-device English sentiment classification model (`bert_classifier.tflite`, 25.7 MB) trained on Stanford Sentiment Treebank (SST-2) movie reviews for the [MediaPipe Text Classifier (`TextClassifier`)](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier) task.

<div style="border: 1px solid rgba(59, 130, 246, 0.28); background-color: rgba(59, 130, 246, 0.06); border-radius: 8px; padding: 12px 16px; margin: 16px 0;">
  <p style="margin: 0 0 6px 0;"><strong>Documentation:</strong> <a href="https://developers.google.com/edge/mediapipe/solutions/text/text_classifier">MediaPipe Text Classifier Docs</a></p>
  <p style="margin: 0 0 6px 0;"><strong>Samples:</strong> <a href="https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/android">Android</a> · <a href="https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/ios">iOS</a> · <a href="https://colab.research.google.com/github/google-ai-edge/mediapipe-samples/blob/main/examples/text_classification/python/text_classifier.ipynb">Python Colab</a> · <a href="https://github.com/google-ai-edge/mediapipe-samples-web/blob/main/src/tasks/text-classifier.ts">Web</a> · <a href="https://github.com/google/flutter-mediapipe/tree/main/samples/text/text_classifier">Flutter</a></p>
  <p style="margin: 0;"><strong>Try live:</strong> <a href="https://google-ai-edge.github.io/mediapipe-samples-web/#/text/text_classifier">Web Demo</a></p>
</div>

MobileBERT sequence classifier fine-tuned on SST-2 (`negative`, `positive`), **dynamic-range quantized** with embedded BERT `vocab.txt` and `labels.txt` in `TFLITE_METADATA` (performing out-of-graph WordPiece tokenization into 128-token `word_ids`, `type_ids`, and `mask` inputs).

## Files

| File | Precision | Size | SHA-256 |
|---|---|---:|---|
| [`bert_classifier.tflite`](https://huggingface.co/litert-community/MediaPipe-TextClassifier-MobileBERT/blob/main/bert_classifier.tflite) | `dynamic-range` | 25.71 MB (25,707,538 B) | `9b45012ab143d88d61e10ea501d6c8763f7202b86fa987711519d89bfa2a88b1` |

### Download

```bash
pip install -U huggingface_hub
hf download litert-community/MediaPipe-TextClassifier-MobileBERT bert_classifier.tflite --local-dir .
```

## Inputs, Outputs, and Model Specifications

### `bert_classifier.tflite`

- Embedded `TFLITE_METADATA`: name `MobileBert text classifier` · version `1` · author `MediaPipe` · packed associated files: `vocab.txt`, `labels.txt`

| Direction | Tensor | Shape | Type | Notes |
|---|---|---|---|---|
| Input | `serving_default_input_word_ids:0` | `[1, 128]` | `int32` | ids — Tokenized ids of input text as concatenated query and passage. |
| Input | `serving_default_input_type_ids:0` | `[1, 128]` | `int32` | segment_ids — 0 for query and 1 for passage tokens. |
| Input | `serving_default_input_mask:0` | `[1, 128]` | `int32` | mask — Mask with 1 for real tokens and 0 for padding tokens. |
| Output | `StatefulPartitionedCall:0` | `[1, 2]` | `float32` | probability — Probabilities of labels respectively. — Labels: `labels.txt` |

## Use with MediaPipe Tasks

### Python Quickstart

```bash
pip install -U mediapipe huggingface_hub
```

```python
import mediapipe as mp
from huggingface_hub import hf_hub_download

model_path = hf_hub_download(repo_id="litert-community/MediaPipe-TextClassifier-MobileBERT", filename="bert_classifier.tflite")

BaseOptions = mp.tasks.BaseOptions
TextClassifier = mp.tasks.text.TextClassifier
TextClassifierOptions = mp.tasks.text.TextClassifierOptions

options = TextClassifierOptions(base_options=BaseOptions(model_asset_path=model_path))
with TextClassifier.create_from_options(options) as classifier:
    result = classifier.classify("An unforgettable and genuinely moving film!")
    for cat in result.classifications[0].categories:
        print(f"{cat.category_name}: {cat.score:.3f}")
```

### Cross-Platform Guides & Samples

- **Task overview & options:** [Text Classifier documentation](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier)
- **Platform setup guides:** [Python](https://developers.google.com/edge/mediapipe/solutions/setup_python) · [Web / JavaScript](https://developers.google.com/edge/mediapipe/solutions/setup_web) · [Android (Kotlin / Java)](https://developers.google.com/edge/mediapipe/solutions/setup_android) · [iOS (Swift)](https://developers.google.com/edge/mediapipe/solutions/setup_ios) · [Flutter (Dart)](https://developers.google.com/edge/mediapipe/solutions/setup_flutter)
- **Runnable sample apps:** [Web Demo](https://google-ai-edge.github.io/mediapipe-samples-web/#/text/text_classifier) · [Web Source](https://github.com/google-ai-edge/mediapipe-samples-web/blob/main/src/tasks/text-classifier.ts) · [Android](https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/android) · [iOS](https://github.com/google-ai-edge/mediapipe-samples/tree/main/examples/text_classification/ios) · [Python Notebook](https://colab.research.google.com/github/google-ai-edge/mediapipe-samples/blob/main/examples/text_classification/python/text_classifier.ipynb) · [Flutter](https://github.com/google/flutter-mediapipe/tree/main/samples/text/text_classifier)

## Latency Benchmarks

End-to-end MediaPipe Task pipeline average latency reported by the [Text Classifier documentation](https://developers.google.com/edge/mediapipe/solutions/text/text_classifier#benchmarks):

| Model Variant | Device | CPU Latency | GPU Latency |
|---|---|---:|---:|
| MobileBERT | Pixel 6 | 57.68 ms | — |

## Intended Use, Limitations, and Fairness

- **Intended applications:** Binary English sentiment analysis (positive vs. negative) on short text snippets such as reviews and feedback.
- **Out-of-scope & limitations:**
  - **English only:** Not suitable for non-English text.
  - **Domain shift:** Trained on SST-2 movie reviews (`https://nlp.stanford.edu/sentiment/index.html`); accuracy may degrade on unrelated domains, sarcasm, or multi-sentence documents.
  - No standalone Google fairness PDF is published for this model on the MediaPipe documentation page.

## Provenance

Every model binary in this repository is byte-identical (unmodified FlatBuffer weights and embedded `TFLITE_METADATA` bytes) to the official artifact served by Google from `storage.googleapis.com/mediapipe-models`.

| Repo File | Upstream URL | GCS Generation | Upstream `Last-Modified` | Pinned Bucket Version |
|---|---|---|---|---|
| `bert_classifier.tflite` | [`bert_classifier.tflite`](https://storage.googleapis.com/mediapipe-models/text_classifier/bert_classifier/float32/latest/bert_classifier.tflite) | `1682480022043445` | Wed, 26 Apr 2023 03:33:42 GMT | `/1/` |

## License and Citation

- License: `apache-2.0` — Stated as Apache License, Version 2.0 (`http://www.apache.org/licenses/LICENSE-2.0`) in the embedded `TFLITE_METADATA` block of the `.tflite` model file(s).

### Citation

```bibtex
@inproceedings{sun2020mobilebert,
  title     = {MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices},
  author    = {Sun, Zhiqing and Yu, Hongkun and Song, Xiaodan and Liu, Renjie and Yang, Yiming and Zhou, Denny},
  booktitle = {Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics (ACL)},
  year      = {2020},
  url       = {https://arxiv.org/abs/2004.02984}
}
```