Norm commited on
Commit
bf6623b
1 Parent(s): 50fd90a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +26 -5
README.md CHANGED
@@ -10,21 +10,40 @@ license: afl-3.0
10
  **A Quick Example**
11
  ```python
12
  from networks.modeling_erine_layout import ErnieLayoutConfig, ErnieLayoutForQuestionAnswering
 
13
  from networks.tokenizer import ErnieLayoutTokenizer
 
 
14
 
15
 
16
  pretrain_torch_model_or_path = "path/to/pretrained-model"
17
 
18
  # initialize tokenizer
19
  tokenizer = ErnieLayoutTokenizer.from_pretrained(pretrained_model_name_or_path=pretrain_torch_model_or_path)
20
- encodings = tokenizer.encode_plus(text="Question", text_pair="Answer", add_special_tokens=True,
21
- max_length=512, truncation="only_second",
22
- return_offsets_mapping=True, return_attention_mask=True,
23
- return_overflowing_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # initialize config
26
  config = ErnieLayoutConfig.from_pretrained(pretrained_model_name_or_path=pretrain_torch_model_or_path)
27
- config.num_classes = 2
28
 
29
  # initialize ERNIE for VQA
30
  model = ErnieLayoutForQuestionAnswering.from_pretrained(
@@ -32,4 +51,6 @@ model = ErnieLayoutForQuestionAnswering.from_pretrained(
32
  config=config,
33
  )
34
 
 
 
35
  ```
 
10
  **A Quick Example**
11
  ```python
12
  from networks.modeling_erine_layout import ErnieLayoutConfig, ErnieLayoutForQuestionAnswering
13
+ from networks.feature_extractor import ErnieFeatureExtractor
14
  from networks.tokenizer import ErnieLayoutTokenizer
15
+ from networks.model_util import ernie_qa_tokenize, prepare_context_info
16
+ from PIL import Image
17
 
18
 
19
  pretrain_torch_model_or_path = "path/to/pretrained-model"
20
 
21
  # initialize tokenizer
22
  tokenizer = ErnieLayoutTokenizer.from_pretrained(pretrained_model_name_or_path=pretrain_torch_model_or_path)
23
+ context = ['This is an example document', 'All ocr boxes are inserted into this list']
24
+ layout = [[381, 91, 505, 115], [738, 96, 804, 122]]
25
+
26
+ # intialize feature extractor
27
+ feature_extractor = ErnieFeatureExtractor()
28
+
29
+ # open the image of the document
30
+ pil_image = Image.open("/path/to/image").convert("RGB")
31
+
32
+ # Process image
33
+ tokenized_res['pixel_values'] = feature_extractor(pil_image)
34
+
35
+ # Tokenize context & questions
36
+ context_encodings, = prepare_context_info(tokenizer, context, layout)
37
+ question = "what is it?"
38
+ tokenized_res = ernie_qa_tokenize(tokenizer, question, context_encodings)
39
+
40
+ # answer start && end index
41
+ tokenized_res['start_positions'] = 6
42
+ tokenized_res['end_positions'] = 12
43
 
44
  # initialize config
45
  config = ErnieLayoutConfig.from_pretrained(pretrained_model_name_or_path=pretrain_torch_model_or_path)
46
+ config.num_classes = 2 # start and end
47
 
48
  # initialize ERNIE for VQA
49
  model = ErnieLayoutForQuestionAnswering.from_pretrained(
 
51
  config=config,
52
  )
53
 
54
+ output = model(**tokenized_res)
55
+
56
  ```