Minghao Li commited on
Commit
fb17309
1 Parent(s): 608b226

Update from Minghao Li

Browse files
README.md ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - trocr
4
+ ---
5
+
6
+ # TrOCR (small-sized model, pre-trained only)
7
+
8
+ TrOCR pre-trained only model. 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-stage1')
36
+ feature_extractor = AutoFeatureExtractor.from_pretrained('microsoft/trocr-small-stage1')
37
+ model = VisionEncoderDecoderModel.from_pretrained('microsoft/trocr-small-stage1')
38
+
39
+ # training
40
+ pixel_values = feature_extractor(image, return_tensors="pt").pixel_values # Batch size 1
41
+ decoder_input_ids = torch.tensor([[model.config.decoder.decoder_start_token_id]])
42
+ outputs = model(pixel_values=pixel_values, decoder_input_ids=decoder_input_ids)
43
+ ```
44
+
45
+ ### BibTeX entry and citation info
46
+
47
+ ```bibtex
48
+ @misc{li2021trocr,
49
+ title={TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models},
50
+ author={Minghao Li and Tengchao Lv and Lei Cui and Yijuan Lu and Dinei Florencio and Cha Zhang and Zhoujun Li and Furu Wei},
51
+ year={2021},
52
+ eprint={2109.10282},
53
+ archivePrefix={arXiv},
54
+ primaryClass={cs.CL}
55
+ }
56
+ ```
config.json ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "VisionEncoderDecoderModel"
4
+ ],
5
+ "decoder": {
6
+ "_name_or_path": "",
7
+ "activation_dropout": 0.0,
8
+ "activation_function": "relu",
9
+ "add_cross_attention": true,
10
+ "architectures": null,
11
+ "attention_dropout": 0.0,
12
+ "bad_words_ids": null,
13
+ "bos_token_id": 0,
14
+ "chunk_size_feed_forward": 0,
15
+ "classifier_dropout": 0.0,
16
+ "cross_attention_hidden_size": 384,
17
+ "d_model": 256,
18
+ "decoder_attention_heads": 8,
19
+ "decoder_ffn_dim": 1024,
20
+ "decoder_layerdrop": 0.0,
21
+ "decoder_layers": 6,
22
+ "decoder_start_token_id": 2,
23
+ "diversity_penalty": 0.0,
24
+ "do_sample": false,
25
+ "dropout": 0.1,
26
+ "early_stopping": false,
27
+ "encoder_no_repeat_ngram_size": 0,
28
+ "eos_token_id": 2,
29
+ "finetuning_task": null,
30
+ "forced_bos_token_id": null,
31
+ "forced_eos_token_id": null,
32
+ "id2label": {
33
+ "0": "LABEL_0",
34
+ "1": "LABEL_1"
35
+ },
36
+ "init_std": 0.02,
37
+ "is_decoder": true,
38
+ "is_encoder_decoder": false,
39
+ "label2id": {
40
+ "LABEL_0": 0,
41
+ "LABEL_1": 1
42
+ },
43
+ "layernorm_embedding": true,
44
+ "length_penalty": 1.0,
45
+ "max_length": 20,
46
+ "max_position_embeddings": 512,
47
+ "min_length": 0,
48
+ "model_type": "trocr",
49
+ "no_repeat_ngram_size": 0,
50
+ "num_beam_groups": 1,
51
+ "num_beams": 1,
52
+ "num_return_sequences": 1,
53
+ "output_attentions": false,
54
+ "output_hidden_states": false,
55
+ "output_scores": false,
56
+ "pad_token_id": 1,
57
+ "prefix": null,
58
+ "problem_type": null,
59
+ "pruned_heads": {},
60
+ "remove_invalid_values": false,
61
+ "repetition_penalty": 1.0,
62
+ "return_dict": true,
63
+ "return_dict_in_generate": false,
64
+ "scale_embedding": true,
65
+ "sep_token_id": null,
66
+ "task_specific_params": null,
67
+ "temperature": 1.0,
68
+ "tie_encoder_decoder": false,
69
+ "tie_word_embeddings": false,
70
+ "tokenizer_class": null,
71
+ "top_k": 50,
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,
79
+ "vocab_size": 64044
80
+ },
81
+ "encoder": {
82
+ "_name_or_path": "",
83
+ "add_cross_attention": false,
84
+ "architectures": null,
85
+ "attention_probs_dropout_prob": 0.0,
86
+ "bad_words_ids": null,
87
+ "bos_token_id": null,
88
+ "chunk_size_feed_forward": 0,
89
+ "cross_attention_hidden_size": null,
90
+ "decoder_start_token_id": null,
91
+ "diversity_penalty": 0.0,
92
+ "do_sample": false,
93
+ "early_stopping": false,
94
+ "encoder_no_repeat_ngram_size": 0,
95
+ "eos_token_id": null,
96
+ "finetuning_task": null,
97
+ "forced_bos_token_id": null,
98
+ "forced_eos_token_id": null,
99
+ "hidden_act": "gelu",
100
+ "hidden_dropout_prob": 0.0,
101
+ "hidden_size": 384,
102
+ "id2label": {
103
+ "0": "LABEL_0",
104
+ "1": "LABEL_1"
105
+ },
106
+ "image_size": 384,
107
+ "initializer_range": 0.02,
108
+ "intermediate_size": 1536,
109
+ "is_decoder": false,
110
+ "is_encoder_decoder": false,
111
+ "label2id": {
112
+ "LABEL_0": 0,
113
+ "LABEL_1": 1
114
+ },
115
+ "layer_norm_eps": 1e-12,
116
+ "length_penalty": 1.0,
117
+ "max_length": 20,
118
+ "min_length": 0,
119
+ "model_type": "deit",
120
+ "no_repeat_ngram_size": 0,
121
+ "num_attention_heads": 6,
122
+ "num_beam_groups": 1,
123
+ "num_beams": 1,
124
+ "num_channels": 3,
125
+ "num_hidden_layers": 12,
126
+ "num_return_sequences": 1,
127
+ "output_attentions": false,
128
+ "output_hidden_states": false,
129
+ "output_scores": false,
130
+ "pad_token_id": null,
131
+ "patch_size": 16,
132
+ "prefix": null,
133
+ "problem_type": null,
134
+ "pruned_heads": {},
135
+ "qkv_bias": true,
136
+ "remove_invalid_values": false,
137
+ "repetition_penalty": 1.0,
138
+ "return_dict": true,
139
+ "return_dict_in_generate": false,
140
+ "sep_token_id": null,
141
+ "task_specific_params": null,
142
+ "temperature": 1.0,
143
+ "tie_encoder_decoder": false,
144
+ "tie_word_embeddings": true,
145
+ "tokenizer_class": null,
146
+ "top_k": 50,
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,
154
+ "is_encoder_decoder": true,
155
+ "model_type": "vision-encoder-decoder",
156
+ "tie_word_embeddings": false,
157
+ "torch_dtype": "float32",
158
+ "transformers_version": null
159
+ }
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 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:35ee4bee78f7686e15e5eaf9de5b0788ae28c53fa756883afd3405924f6c49c5
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"}