Instructions to use microsoft/trocr-small-stage1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/trocr-small-stage1 with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "image-to-text" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("image-to-text", model="microsoft/trocr-small-stage1")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("microsoft/trocr-small-stage1") model = AutoModelForMultimodalLM.from_pretrained("microsoft/trocr-small-stage1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit ·
b5bae50
1
Parent(s): 200cf24
Update code example
Browse files
README.md
CHANGED
|
@@ -23,7 +23,7 @@ You can use the raw model for optical character recognition (OCR) on single text
|
|
| 23 |
Here is how to use this model in PyTorch:
|
| 24 |
|
| 25 |
```python
|
| 26 |
-
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 27 |
from PIL import Image
|
| 28 |
import requests
|
| 29 |
import torch
|
|
@@ -32,13 +32,11 @@ import torch
|
|
| 32 |
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
|
| 33 |
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-stage1')
|
| 37 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-stage1')
|
| 38 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-stage1')
|
| 39 |
|
| 40 |
# training
|
| 41 |
-
pixel_values =
|
| 42 |
decoder_input_ids = torch.tensor([[model.config.decoder.decoder_start_token_id]])
|
| 43 |
outputs = model(pixel_values=pixel_values, decoder_input_ids=decoder_input_ids)
|
| 44 |
```
|
|
|
|
| 23 |
Here is how to use this model in PyTorch:
|
| 24 |
|
| 25 |
```python
|
| 26 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
| 27 |
from PIL import Image
|
| 28 |
import requests
|
| 29 |
import torch
|
|
|
|
| 32 |
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
|
| 33 |
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
| 34 |
|
| 35 |
+
processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-stage1')
|
|
|
|
|
|
|
| 36 |
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-stage1')
|
| 37 |
|
| 38 |
# training
|
| 39 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values # Batch size 1
|
| 40 |
decoder_input_ids = torch.tensor([[model.config.decoder.decoder_start_token_id]])
|
| 41 |
outputs = model(pixel_values=pixel_values, decoder_input_ids=decoder_input_ids)
|
| 42 |
```
|