Skip to content

add multi-modal (vision + language) transformers#2936

Closed
ahatamiz wants to merge 20 commits into
Project-MONAI:devfrom
ahatamiz:2775_multimodality_v1
Closed

add multi-modal (vision + language) transformers#2936
ahatamiz wants to merge 20 commits into
Project-MONAI:devfrom
ahatamiz:2775_multimodality_v1

Conversation

@ahatamiz

@ahatamiz ahatamiz commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

Signed-off-by: ahatamizadeh ahatamizadeh@nvidia.com

Description

This pull request adds the full pipeline and support for multimodal (vision + language ) transformers. The transformers implementation follow Huggingface repository.

Status

Ready

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@wyli wyli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, please some initial comments inline

Comment thread monai/networks/nets/vltransformer.py Outdated
Comment thread monai/networks/nets/vltransformer.py Outdated
num_mixed_layers: number of mixed transformer layers.
"""
super().__init__()
bert_config = type(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make it a module-level variable `, like

efficientnet_params = {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue here is that module-level variable does not work properly when trying to initialize model weights from bert checkpoint in huggingface library.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to replicate this issue, could you please elaborate?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you can make the dictionary (in line 232-255) a global variable as the default,

def __init__(
        self,
        num_language_layers: int = 2,
        num_vision_layers: int = 2,
        num_mixed_layers: int = 2,
        bert_config: Dict = BERT_CONFIG,
    )

it'll leave some flexibility for tuning the parameters for the end users.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way is to simply change the object-based definition to a dictionary-based config for BERT model, but class BertPreTrainedModel is not compatible with this. I am using this class directly from Huggingface with very minor modifications.

For your suggestion to give more flexibility, I think we need to keep the original BETR configurations to be able to use the pre-trained BERT weights (at least for this release). Pre-training a BERT type model on text data from scratch could be costly. Any suggestions ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure that we need to keep exactly this version of config in order to load the pretrained?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, I first tried with different configurations. But the model will not be initialized from the BERT pre-trained checkpoint,so takes longer for training.

@wyli wyli Sep 13, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my concern is mainly about the hard-coded config. for example, as a user if I want to set 'attention_probs_dropout_prob': 0.2 and "transformers_version": "4.10.1" for fine-tuning of the pretrained model, how can I set this MultiModal class without rewriting everything from scratch?

Comment thread setup.cfg Outdated
@ahatamiz ahatamiz linked an issue Sep 13, 2021 that may be closed by this pull request
@ahatamiz ahatamiz removed a link to an issue Sep 13, 2021
@ahatamiz

Copy link
Copy Markdown
Contributor Author

@wyli it seems like some check fails due to the following which is not related to multi-modality ?

/home/runner/work/MONAI/MONAI/monai/data/grid_dataset.py:144:13: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/monai/data/utils.py:286:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/monai/data/utils.py:297:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/monai/utils/aliases.py:74:13: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/monai/transforms/inverse_batch_transform.py:102:13: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/monai/_extensions/loader.py:37:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
/home/runner/work/MONAI/MONAI/tests/utils.py:77:9: B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.
7     B904 Within an `except` clause, raise exceptions with `raise ... from err` or `raise ... from None` to distinguish them from errors in exception handling.  See https://docs.python.org/3/tutorial/errors.html#exception-chaining for details.

@wyli

wyli commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

yes, I'm merging a fix for that flake8 issue very soon #2940

@ahatamiz

Copy link
Copy Markdown
Contributor Author

Hi @wyli

Another issue that comes up is with the optional import ( or just import). For some reason transformers is not being installed and cannot be imported:

monai.utils.module.OptionalImportError: from transformers.models.bert.modeling_bert import BertEmbeddings (No module named 'transformers').

Thanks

@ahatamiz

Copy link
Copy Markdown
Contributor Author

yes, I'm merging a fix for that flake8 issue very soon #2940

Thanks much.

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@wyli

wyli commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

BertEmbeddings

how to replicate this issue?

I tried and the following works

Python 3.7.10 (default, Feb 26 2021, 10:16:00) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from monai.utils import optional_import
>>> bert_embeddings = optional_import("transformers.models.bert.modeling_bert", name="BertEmbeddings")[0]
>>> bert_embeddings
<class 'transformers.models.bert.modeling_bert.BertEmbeddings'>
>>> 

@wyli

wyli commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

/build

@ahatamiz

Copy link
Copy Markdown
Contributor Author

BertEmbeddings

how to replicate this issue?

I tried and the following works

Python 3.7.10 (default, Feb 26 2021, 10:16:00) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from monai.utils import optional_import
>>> bert_embeddings = optional_import("transformers.models.bert.modeling_bert", name="BertEmbeddings")[0]
>>> bert_embeddings
<class 'transformers.models.bert.modeling_bert.BertEmbeddings'>
>>> 

Thanks @wyli. I pushed a new PR based on this for optional imports.

@ahatamiz

Copy link
Copy Markdown
Contributor Author

BertEmbeddings

how to replicate this issue?
I tried and the following works

Python 3.7.10 (default, Feb 26 2021, 10:16:00) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from monai.utils import optional_import
>>> bert_embeddings = optional_import("transformers.models.bert.modeling_bert", name="BertEmbeddings")[0]
>>> bert_embeddings
<class 'transformers.models.bert.modeling_bert.BertEmbeddings'>
>>> 

Thanks @wyli. I pushed a new PR based on this for optional imports.

Hi @wyli , seems like this did not resolve the issue as some of the checks failed.

@wyli

wyli commented Sep 13, 2021

Copy link
Copy Markdown
Contributor

BertEmbeddings

how to replicate this issue?
I tried and the following works

Python 3.7.10 (default, Feb 26 2021, 10:16:00) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from monai.utils import optional_import
>>> bert_embeddings = optional_import("transformers.models.bert.modeling_bert", name="BertEmbeddings")[0]
>>> bert_embeddings
<class 'transformers.models.bert.modeling_bert.BertEmbeddings'>
>>> 

Thanks @wyli. I pushed a new PR based on this for optional imports.

Hi @wyli , seems like this did not resolve the issue as some of the checks failed.

Please add the test to this list to exclude it for minimum environment tests

"test_vit",

@ahatamiz

Copy link
Copy Markdown
Contributor Author

BertEmbeddings

how to replicate this issue?
I tried and the following works

Python 3.7.10 (default, Feb 26 2021, 10:16:00) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from monai.utils import optional_import
>>> bert_embeddings = optional_import("transformers.models.bert.modeling_bert", name="BertEmbeddings")[0]
>>> bert_embeddings
<class 'transformers.models.bert.modeling_bert.BertEmbeddings'>
>>> 

Thanks @wyli. I pushed a new PR based on this for optional imports.

Hi @wyli , seems like this did not resolve the issue as some of the checks failed.

Please add the test to this list to exclude it for minimum environment tests

"test_vit",

Sure.

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

1 similar comment
@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/build

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/build

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/build

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

Nic-Ma and others added 16 commits September 13, 2021 11:57
Signed-off-by: Nic Ma <nma@nvidia.com>

Co-authored-by: Wenqi Li <wenqil@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
* [DLMED] enhance the pad mode

Signed-off-by: Nic Ma <nma@nvidia.com>

* [DLMED] update all the tensor pad related

Signed-off-by: Nic Ma <nma@nvidia.com>

* [DLMED] fix error tests

Signed-off-by: Nic Ma <nma@nvidia.com>

* [DLMED] fix GPU tests

Signed-off-by: Nic Ma <nma@nvidia.com>

* [DLMED] update according to comments

Signed-off-by: Nic Ma <nma@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
@ahatamiz

Copy link
Copy Markdown
Contributor Author

/black

@ahatamiz

Copy link
Copy Markdown
Contributor Author

/build

Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
Signed-off-by: ahatamizadeh <ahatamizadeh@nvidia.com>
@ahatamiz

Copy link
Copy Markdown
Contributor Author

I close this pull request due to many conflicts between the two branches. Will push a new one.

@ahatamiz ahatamiz closed this Sep 15, 2021
@ahatamiz ahatamiz deleted the 2775_multimodality_v1 branch September 15, 2021 22:10
@wyli

wyli commented Sep 15, 2021

Copy link
Copy Markdown
Contributor

Hi @ahatamiz, why the close? the PR looks nice, sorry for the confusion that my previous comments might have created

@wyli

wyli commented Sep 15, 2021

Copy link
Copy Markdown
Contributor

the latest update with the flexible bert_config looks good to me.

@ahatamiz

ahatamiz commented Sep 16, 2021

Copy link
Copy Markdown
Contributor Author

Hi @wyli. Sorry about this. dealing with so many conflicts in this PR was a bit time-consuming. I had to submit another one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants