-
Notifications
You must be signed in to change notification settings - Fork 26.3k
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
Add LayoutLM Model #7064
Add LayoutLM Model #7064
Conversation
Codecov Report
@@ Coverage Diff @@
## master #7064 +/- ##
==========================================
- Coverage 80.32% 80.23% -0.10%
==========================================
Files 174 177 +3
Lines 33446 33878 +432
==========================================
+ Hits 26867 27181 +314
- Misses 6579 6697 +118
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR is overall in a good shape! Thanks for your work!
@sgugger @patrickvonplaten Interested in this?
if bbox is None: | ||
bbox = torch.zeros(tuple(list(input_shape) + [4]), dtype=torch.long, device=device) | ||
|
||
extended_attention_mask = attention_mask.unsqueeze(1).unsqueeze(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you may find torch.view()
better here.
if head_mask.dim() == 1: | ||
head_mask = head_mask.unsqueeze(0).unsqueeze(0).unsqueeze(-1).unsqueeze(-1) | ||
head_mask = head_mask.expand(self.config.num_hidden_layers, -1, -1, -1, -1) | ||
elif head_mask.dim() == 2: | ||
head_mask = head_mask.unsqueeze(1).unsqueeze(-1).unsqueeze(-1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for adding this model! Before we can merge, there is a tiny bit of work to do to make the modeling file independent from bert_modeling.py
. Could you copy the classes you use from that file to the layeroutlm_modeling file (and rename them from BertXxx
to LayeroutLMXxx
), you can look at the files for GPT and GPT-2 as example. The idea is that this way, any researcher can directly tweak the model by copying its file without having to worry about extra stuff.
Otherwise, all look good to me apart form a few nits.
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Hi, I have moved and renamed the classes from modeling_bert to modeling_layoutlm and the code is ready for review and merge. @JetRunner @sgugger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the copied code. This all looks good to me and ready to merge, with just one nit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exceptionally clean! Great job!
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
@JetRunner @sgugger @LysandreJik The new suggestions have been adopted. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks a lot @liminghao1630
* first version * finish test docs readme model/config/tokenization class * apply make style and make quality * fix layoutlm GitHub link * fix conflict in index.rst and add layoutlm to pretrained_models.rst * fix bug in test_parents_and_children_in_mappings * reformat modeling_auto.py and tokenization_auto.py * fix bug in test_modeling_layoutlm.py * Update docs/source/model_doc/layoutlm.rst Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Update docs/source/model_doc/layoutlm.rst Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * remove inh, add tokenizer fast, and update some doc * copy and rename necessary class from modeling_bert to modeling_layoutlm * Update src/transformers/configuration_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/configuration_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/configuration_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/configuration_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/modeling_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/modeling_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * Update src/transformers/modeling_layoutlm.py Co-authored-by: Lysandre Debut <lysandre@huggingface.co> * add mish to activations.py, import ACT2FN and import logging from utils Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> Co-authored-by: Lysandre Debut <lysandre@huggingface.co>
This reverts commit 9beb08c.
Introduction
This pull request implements the LayoutLM model, as defined in the paper:
LayoutLM is a simple but effective pre-training method of text and layout for document image understanding and information extraction tasks, such as form understanding and receipt understanding. LayoutLM archives the SOTA results on multiple datasets.
Typical workflow for including a model
Here an overview of the general workflow:
Let's detail what should be done at each step.
Adding model/configuration/tokenization classes
Here is the workflow for adding model/configuration/tokenization classes:
xxx
with your modelname.
XXX
(with various casing) with your model name.configuration_...
file.modeling_...
files (PyTorch and TF 2.0).tokenization_...
file.Adding conversion scripts
Here is the workflow for the conversion scripts:
convert_...
) from the present folder to the main folder.Adding tests:
Here is the workflow for the adding tests:
tests
sub-folder of the present folder to thetests
subfolder of the mainfolder and rename them, replacing
xxx
with your model name.XXX
(with various casing) with your model name.Documenting your model:
Here is the workflow for documentation:
XXX_START_DOCSTRING
contains an introduction to the model you're adding and a link to the originalarticle and that
XXX_INPUTS_DOCSTRING
contains all the inputs of your model.xxx.rst
in the folderdocs/source/model_doc
and add this file indocs/source/index.rst
.Make sure to check you have no sphinx warnings when building the documentation locally and follow our
documentaiton guide.
Final steps
You can then finish the addition step by adding imports for your classes in the common files:
__init__.py
.configuration_auto.py
.modeling_auto.py
andmodeling_tf_auto.py
.tokenization_auto.py
.commands/convert.py
)convert_pytorch_checkpoint_to_tf2.py
file.
README.md
and the documentation itselfin
docs/source/index.rst
anddocs/source/pretrained_models.rst
.model sharing documentation.