Minghao Li commited on
Commit
cbb9551
1 Parent(s): 5e384e4

Update from Minghao Li

Browse files
README.md ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - trocr
4
+ ---
5
+
6
+ # TrOCR (small-sized model, fine-tuned on IAM)
7
+
8
+ 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).
9
+
10
+
11
+ ## Model description
12
+
13
+ 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.
14
+
15
+ 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.
16
+
17
+ ## Intended uses & limitations
18
+
19
+ 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.
20
+
21
+ ### How to use
22
+
23
+ Here is how to use this model in PyTorch:
24
+
25
+ ```python
26
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel, AutoFeatureExtractor, XLMRobertaTokenizer
27
+ from PIL import Image
28
+ import requests
29
+
30
+ # load image from the IAM database
31
+ url = 'https://fki.tic.heia-fr.ch/static/img/a01-122-02-00.jpg'
32
+ image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
33
+
34
+ # For the time being, TrOCRProcessor does not support the small models, so the following temporary solution can be adopted
35
+ # processor = TrOCRProcessor.from_pretrained('microsoft/trocr-small-handwritten')
36
+ feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-handwritten')
37
+ tokenizer = XLMRobertaTokenizer.from_pretrained('microsoft/trocr-small-handwritten')
38
+ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-handwritten')
39
+ # pixel_values = processor(images=image, return_tensors="pt").pixel_values
40
+ pixel_values = feature_extractor(images=image, return_tensors="pt").pixel_values
41
+
42
+ generated_ids = model.generate(pixel_values)
43
+ # generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
44
+ generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
45
+ ```
46
+
47
+ ### BibTeX entry and citation info
48
+
49
+ ```bibtex
50
+ @misc{li2021trocr,
51
+ title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models},
52
+ author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei},
53
+ year={2021},
54
+ eprint={2109.10282},
55
+ archivePrefix={arXiv},
56
+ primaryClass={cs.CL}
57
+ }
58
+ ```
config.json CHANGED
@@ -1,5 +1,4 @@
1
  {
2
- "_name_or_path": "trocr-small-handwritten",
3
  "architectures": [
4
  "VisionEncoderDecoderModel"
5
  ],
@@ -73,7 +72,7 @@
73
  "top_p": 1.0,
74
  "torch_dtype": null,
75
  "torchscript": false,
76
- "transformers_version": "4.16.0.dev0",
77
  "use_bfloat16": false,
78
  "use_cache": false,
79
  "use_learned_position_embeddings": true,
@@ -148,7 +147,7 @@
148
  "top_p": 1.0,
149
  "torch_dtype": null,
150
  "torchscript": false,
151
- "transformers_version": "4.16.0.dev0",
152
  "use_bfloat16": false
153
  },
154
  "eos_token_id": 2,
1
  {
 
2
  "architectures": [
3
  "VisionEncoderDecoderModel"
4
  ],
72
  "top_p": 1.0,
73
  "torch_dtype": null,
74
  "torchscript": false,
75
+ "transformers_version": "4.14.1",
76
  "use_bfloat16": false,
77
  "use_cache": false,
78
  "use_learned_position_embeddings": true,
147
  "top_p": 1.0,
148
  "torch_dtype": null,
149
  "torchscript": false,
150
+ "transformers_version": "4.14.1",
151
  "use_bfloat16": false
152
  },
153
  "eos_token_id": 2,
preprocessor_config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "crop_size": 224,
3
+ "do_center_crop": false,
4
+ "do_normalize": true,
5
+ "do_resize": true,
6
+ "feature_extractor_type": "DeiTFeatureExtractor",
7
+ "image_mean": [
8
+ 0.5,
9
+ 0.5,
10
+ 0.5
11
+ ],
12
+ "image_std": [
13
+ 0.5,
14
+ 0.5,
15
+ 0.5
16
+ ],
17
+ "resample": 3,
18
+ "size": 384
19
+ }
pytorch_model.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:fa3bbdc41b09808b91a8dd3b81c9aa263e6b631a57b334fbea1faa707ce0be21
3
- size 246525165
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b83102cbc1520dee1c3937ac334da83da18cf4683d46ebd1bd4e93ebe584dd7
3
+ size 245933041
sentencepiece.bpe.model ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6f5e2fefcf793761a76a6bfb8ad35489f9c203b25557673284b6d032f41043f4
3
+ size 1356293
special_tokens_map.json ADDED
@@ -0,0 +1 @@
 
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}}
tokenizer_config.json ADDED
@@ -0,0 +1 @@
 
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"}