Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LayoutLM #9476

Merged
merged 7 commits into from
Jan 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 24 additions & 37 deletions src/transformers/models/layoutlm/modeling_layoutlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,21 +738,18 @@ def forward(
>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> tokens = []
>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
... word_tokens = tokenizer.tokenize(word)
... tokens.extend(word_tokens)
... token_boxes.extend([box] * len(word_tokens))

>>> # add cls + sep tokens
>>> tokens = [tokenizer.cls_token] + tokens + [tokenizer.sep_token]
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> input_ids = torch.tensor(tokenizer.convert_tokens_to_ids(tokens)).unsqueeze(0) # batch size of 1
>>> bbox = torch.tensor(token_boxes).unsqueeze(0) # batch size of 1
>>> attention_mask = torch.tensor([1] * len(tokens)).unsqueeze(0) # batch size of 1
>>> token_type_ids = torch.tensor([0] * len(tokens)).unsqueeze(0) # batch size of 1
>>> encoding = tokenizer(' '.join(words), return_tensors="pt")
NielsRogge marked this conversation as resolved.
Show resolved Hide resolved
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = torch.tensor([token_boxes])

>>> outputs = model(input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids)

Expand Down Expand Up @@ -884,21 +881,18 @@ def forward(
>>> words = ["Hello", "[MASK]"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> tokens = []
>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
... word_tokens = tokenizer.tokenize(word)
... tokens.extend(word_tokens)
... token_boxes.extend([box] * len(word_tokens))

>>> # add cls + sep tokens
>>> tokens = [tokenizer.cls_token] + tokens + [tokenizer.sep_token]
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> input_ids = torch.tensor(tokenizer.convert_tokens_to_ids(tokens)).unsqueeze(0) # batch size of 1
>>> bbox = torch.tensor(token_boxes).unsqueeze(0) # batch size of 1
>>> attention_mask = torch.tensor([1] * len(tokens)).unsqueeze(0) # batch size of 1
>>> token_type_ids = torch.tensor([0] * len(tokens)).unsqueeze(0) # batch size of 1
>>> encoding = tokenizer(' '.join(words), return_tensors="pt")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = torch.tensor([token_boxes])

>>> labels = tokenizer("Hello world", return_tensors="pt")["input_ids"]

Expand Down Expand Up @@ -1002,22 +996,18 @@ def forward(
>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> tokens = []
>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
... word_tokens = tokenizer.tokenize(word)
... tokens.extend(word_tokens)
... token_boxes.extend([box] * len(word_tokens))

>>> # add cls + sep tokens
>>> tokens = [tokenizer.cls_token] + tokens + [tokenizer.sep_token]
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> input_ids = torch.tensor(tokenizer.convert_tokens_to_ids(tokens)).unsqueeze(0) # batch size of 1
>>> bbox = torch.tensor(token_boxes).unsqueeze(0) # batch size of 1
>>> attention_mask = torch.tensor([1] * len(tokens)).unsqueeze(0) # batch size of 1
>>> token_type_ids = torch.tensor([0] * len(tokens)).unsqueeze(0) # batch size of 1

>>> encoding = tokenizer(' '.join(words), return_tensors="pt")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = torch.tensor([token_boxes])
>>> sequence_label = torch.tensor([1])

>>> outputs = model(input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids,
Expand Down Expand Up @@ -1123,21 +1113,18 @@ def forward(
>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> tokens = []
>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
NielsRogge marked this conversation as resolved.
Show resolved Hide resolved
... word_tokens = tokenizer.tokenize(word)
... tokens.extend(word_tokens)
... token_boxes.extend([box] * len(word_tokens))

>>> # add cls + sep tokens
>>> tokens = [tokenizer.cls_token] + tokens + [tokenizer.sep_token]
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> input_ids = torch.tensor(tokenizer.convert_tokens_to_ids(tokens)).unsqueeze(0) # batch size of 1
>>> bbox = torch.tensor(token_boxes).unsqueeze(0) # batch size of 1
>>> attention_mask = torch.tensor([1] * len(tokens)).unsqueeze(0) # batch size of 1
>>> token_type_ids = torch.tensor([0] * len(tokens)).unsqueeze(0) # batch size of 1
>>> encoding = tokenizer(' '.join(words), return_tensors="pt")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = torch.tensor([token_boxes])
>>> token_labels = torch.tensor([1,1,0,0]).unsqueeze(0) # batch size of 1

>>> outputs = model(input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids,
Expand Down