Update from Minghao Li
This commit is contained in:
parent
5e384e4da3
commit
cbb9551979
|
@ -0,0 +1,58 @@
|
|||
---
|
||||
tags:
|
||||
- trocr
|
||||
---
|
||||
|
||||
# TrOCR (small-sized model, fine-tuned on IAM)
|
||||
|
||||
TrOCR model fine-tuned on the [IAM dataset](https://fki.tic.heia-fr.ch/databases/iam-handwriting-database). It was introduced in the paper [TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models](https://arxiv.org/abs/2109.10282) by Li et al. and first released in [this repository](https://github.com/microsoft/unilm/tree/master/trocr).
|
||||
|
||||
|
||||
## Model description
|
||||
|
||||
The TrOCR model is an encoder-decoder model, consisting of an image Transformer as encoder, and a text Transformer as decoder. The image encoder was initialized from the weights of DeiT, while the text decoder was initialized from the weights of UniLM.
|
||||
|
||||
Images are presented to the model as a sequence of fixed-size patches (resolution 16x16), which are linearly embedded. One also adds absolute position embeddings before feeding the sequence to the layers of the Transformer encoder. Next, the Transformer text decoder autoregressively generates tokens.
|
||||
|
||||
## Intended uses & limitations
|
||||
|
||||
You can use the raw model for optical character recognition (OCR) on single text-line images. See the [model hub](https://huggingface.co/models?search=microsoft/trocr) to look for fine-tuned versions on a task that interests you.
|
||||
|
||||
### How to use
|
||||
|
||||
Here is how to use this model in PyTorch:
|
||||
|
||||
```python
|
||||
from transformers import TrOCRProcessor, VisionEncoderDecoderModel, AutoFeatureExtractor, XLMRobertaTokenizer
|
||||
from PIL import Image
|
||||
import requests
|
||||
|
||||
# load image from the IAM database
|
||||
url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
|
||||
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
|
||||
|
||||
# For the time being, TrOCRProcessor does not support the small models, so the following temporary solution can be adopted
|
||||
# processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-handwritten')
|
||||
feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-handwritten')
|
||||
tokenizer = XLMRobertaTokenizer.from_pretrained('microsoft/trocr-small-handwritten')
|
||||
model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-handwritten')
|
||||
# pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
||||
pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
|
||||
|
||||
generated_ids = model.generate(pixel_values)
|
||||
# generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
||||
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
||||
```
|
||||
|
||||
### BibTeX entry and citation info
|
||||
|
||||
```bibtex
|
||||
@misc{li2021trocr,
|
||||
title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models},
|
||||
author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei},
|
||||
year={2021},
|
||||
eprint={2109.10282},
|
||||
archivePrefix={arXiv},
|
||||
primaryClass={cs.CL}
|
||||
}
|
||||
```
|
|
@ -1,5 +1,4 @@
|
|||
{
|
||||
"_name_or_path": "trocr-small-handwritten",
|
||||
"architectures": [
|
||||
"VisionEncoderDecoderModel"
|
||||
],
|
||||
|
@ -73,7 +72,7 @@
|
|||
"top_p": 1.0,
|
||||
"torch_dtype": null,
|
||||
"torchscript": false,
|
||||
"transformers_version": "4.16.0.dev0",
|
||||
"transformers_version": "4.14.1",
|
||||
"use_bfloat16": false,
|
||||
"use_cache": false,
|
||||
"use_learned_position_embeddings": true,
|
||||
|
@ -148,7 +147,7 @@
|
|||
"top_p": 1.0,
|
||||
"torch_dtype": null,
|
||||
"torchscript": false,
|
||||
"transformers_version": "4.16.0.dev0",
|
||||
"transformers_version": "4.14.1",
|
||||
"use_bfloat16": false
|
||||
},
|
||||
"eos_token_id": 2,
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"crop_size": 224,
|
||||
"do_center_crop": false,
|
||||
"do_normalize": true,
|
||||
"do_resize": true,
|
||||
"feature_extractor_type": "DeiTFeatureExtractor",
|
||||
"image_mean": [
|
||||
0.5,
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"image_std": [
|
||||
0.5,
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"resample": 3,
|
||||
"size": 384
|
||||
}
|
BIN
pytorch_model.bin (Stored with Git LFS)
BIN
pytorch_model.bin (Stored with Git LFS)
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "sep_token": "</s>", "pad_token": "<pad>", "cls_token": "<s>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": true}}
|
|
@ -0,0 +1 @@
|
|||
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "sep_token": "</s>", "cls_token": "<s>", "pad_token": "<pad>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "sp_model_kwargs": {}, "tokenizer_class": "XLMRobertaTokenizer"}
|
Loading…
Reference in New Issue