liminghao1630 commited on
Commit
b5bae50
1 Parent(s): 200cf24

Update code example

Browse files
Files changed (1) hide show
  1. README.md +3 -5
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, AutoFeatureExtractor, XLMRobertaTokenizer
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
- # For the time being, TrOCRProcessor does not support the small models, so the following temporary solution can be adopted
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 = feature_extractor(image, return_tensors="pt").pixel_values # Batch size 1
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
  ```