-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add text to vision embedding #6282
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
Merged
Merged
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
7f255ad
Add text to vision embedding
2dd5566
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1481992
Merge branch 'Project-MONAI:dev' into textembedding
tangy5 81c2965
update parameters
f84ac5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d737121
update encoding
ae7c2fe
change file mode
bd4dc37
fix flake8 format
3fd70e3
fix flake8 format2
a65d6c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1ffd1b2
Merge branch 'Project-MONAI:dev' into textembedding
tangy5 79a8f85
update var name
2cc0ce4
update var name
88f392f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b5604df
Merge branch 'dev' into textembedding
tangy5 fefa8d1
update 2d case, pretrain option, release CLIP weights
4ba43a9
loadable options
6b3d8fe
remove print
af30d79
add skip if downloading fails
900729d
update pretrained load logic
5e8de2c
Merge branch 'dev' into textembedding
tangy5 2be5867
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 38696a5
Merge branch 'Project-MONAI:dev' into textembedding
tangy5 c2a1755
fix cpu only test and others
4392a46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 447509b
[MONAI] code formatting
monai-bot bd6c4e3
fixes
wyli 72bf74a
fixes
wyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import torch | ||
from torch import nn | ||
from torch.utils import model_zoo | ||
|
||
url_map = { | ||
"clip_encoding_univeral_model_32": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/clip_encoding_univeral_model.pth", | ||
} | ||
|
||
|
||
class TextEncoder(nn.Module): | ||
""" | ||
Text to vision encoding by Contrastive Language-Image Pre-training (CLIP) or random embedding. | ||
The text to vision encoder loads the pre-trained or random initialized weights with connection to 2D/3D vision models. | ||
|
||
Contrastive Language-Image Pre-training (CLIP), based on: "Radford et al., | ||
Learning Transferable Visual Models From Natural Language Supervision <https://arxiv.org/abs/2103.00020>" | ||
|
||
Connecting text and medical 3D image, based on: "Liu et al., | ||
CLIP-Driven Universal Model for Organ Segmentation and Tumor Detection <https://arxiv.org/pdf/2301.00785.pdf>" | ||
""" | ||
def __init__( | ||
self, | ||
out_channels: int, | ||
spatial_dims: int = 3, | ||
text_dim: int = 512, | ||
hidden_size: int = 256, | ||
encoding: str = "rand_embedding", | ||
pretrained: bool = False | ||
) -> None: | ||
""" | ||
Args: | ||
out_channels: number of output channels, to control text-baesd embedding for classes. | ||
spatial_dims: number of spatial dims. | ||
text_dim: dimension of text embeddings. | ||
hidden_size: dimension of hidden features, compatible to different vision feature dimensions. | ||
encoding: the text embedding type, default to use clip text pretrained weights. | ||
pretrained: whether to load pretrained weights from e.g., (CLIP) to initialize text embeddings, default to False. | ||
""" | ||
super().__init__() | ||
self.encoding = encoding | ||
|
||
self.spatial_dims = spatial_dims | ||
if spatial_dims not in (2, 3): | ||
raise ValueError("spatial dimension should be 2 or 3.") | ||
|
||
if self.encoding == 'rand_embedding': | ||
self.text_embedding = nn.Embedding(out_channels, hidden_size) | ||
else: | ||
self.register_buffer('text_embedding', torch.randn(out_channels, text_dim)) | ||
|
||
if pretrained: | ||
model_url = url_map[self.encoding] | ||
pretrain_state_dict = model_zoo.load_url(model_url) | ||
self.text_embedding.data = pretrain_state_dict.float() | ||
else: | ||
print(f'{self.encoding} is not implemented, and can not be downloaded, please load your own') | ||
|
||
self.text_to_vision = nn.Linear(text_dim, hidden_size) | ||
|
||
def forward(self): | ||
if self.encoding == 'rand_embedding': | ||
# text embedding as random initialized 'rand_embedding' | ||
test_encoding = self.text_embedding.weight | ||
else: | ||
test_encoding = nn.functional.relu(self.text_to_vision(self.text_embedding)) | ||
|
||
if self.spatial_dims == 3: | ||
test_encoding = test_encoding.unsqueeze(2).unsqueeze(2).unsqueeze(2) | ||
elif self.spatial_dims == 2: | ||
test_encoding = test_encoding.unsqueeze(2).unsqueeze(2) | ||
|
||
return test_encoding | ||
tangy5 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Copyright (c) MONAI Consortium | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import unittest | ||
|
||
import torch | ||
from monai.networks.blocks.text_embedding import TextEncoder | ||
from tests.utils import skip_if_downloading_fails | ||
|
||
device = "cuda" if torch.cuda.is_available() else "cpu" | ||
|
||
|
||
class TestTextEncoder(unittest.TestCase): | ||
def test_test_encoding_shape(self): | ||
with skip_if_downloading_fails(): | ||
# test 2D encoder | ||
text_encoder = TextEncoder(spatial_dims=2, out_channels=32, encoding="clip_encoding_univeral_model_32", pretrained=True).to(device) | ||
text_encoding = text_encoder() | ||
self.assertEqual(text_encoding.shape, (32,256,1,1)) | ||
|
||
# test 3D encoder | ||
text_encoder = TextEncoder(spatial_dims=3, out_channels=32, encoding="clip_encoding_univeral_model_32", pretrained=True).to(device) | ||
text_encoding = text_encoder() | ||
self.assertEqual(text_encoding.shape, (32,256,1,1,1)) | ||
|
||
# test random enbedding 3D | ||
text_encoder = TextEncoder(spatial_dims=3, out_channels=32, encoding="rand_embedding", pretrained=True).to(device) | ||
text_encoding = text_encoder() | ||
self.assertEqual(text_encoding.shape, (32,256,1,1,1)) | ||
|
||
# test random enbedding 2D | ||
text_encoder = TextEncoder(spatial_dims=2, out_channels=32, encoding="rand_embedding", pretrained=True).to(device) | ||
text_encoding = text_encoder() | ||
self.assertEqual(text_encoding.shape, (32,256,1,1)) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.