-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(helper): set_embedding function for all frameworks (#163)
- Loading branch information
Showing
9 changed files
with
164 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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,68 @@ | ||
from typing import Union | ||
|
||
from jina import DocumentArray, DocumentArrayMemmap | ||
|
||
from .helper import AnyDNN, get_framework | ||
|
||
|
||
def set_embeddings( | ||
docs: Union[DocumentArray, DocumentArrayMemmap], | ||
embed_model: AnyDNN, | ||
device: str = 'cpu', | ||
) -> None: | ||
"""Fill the embedding of Documents inplace by using `embed_model` | ||
:param docs: the Documents to be embedded | ||
:param embed_model: the embedding model written in Keras/Pytorch/Paddle | ||
:param device: the computational device for `embed_model`, can be `cpu`, `cuda`, etc. | ||
""" | ||
fm = get_framework(embed_model) | ||
globals()[f'_set_embeddings_{fm}'](docs, embed_model, device) | ||
|
||
|
||
def _set_embeddings_keras( | ||
docs: Union[DocumentArray, DocumentArrayMemmap], | ||
embed_model: AnyDNN, | ||
device: str = 'cpu', | ||
): | ||
from .tuner.keras import get_device | ||
|
||
device = get_device(device) | ||
with device: | ||
embeddings = embed_model(docs.blobs).numpy() | ||
|
||
docs.embeddings = embeddings | ||
|
||
|
||
def _set_embeddings_torch( | ||
docs: Union[DocumentArray, DocumentArrayMemmap], | ||
embed_model: AnyDNN, | ||
device: str = 'cpu', | ||
): | ||
from .tuner.pytorch import get_device | ||
|
||
device = get_device(device) | ||
|
||
import torch | ||
|
||
tensor = torch.tensor(docs.blobs, device=device) | ||
with torch.inference_mode(): | ||
embeddings = embed_model(tensor).cpu().numpy() | ||
|
||
docs.embeddings = embeddings | ||
|
||
|
||
def _set_embeddings_paddle( | ||
docs: Union[DocumentArray, DocumentArrayMemmap], | ||
embed_model: AnyDNN, | ||
device: str = 'cpu', | ||
): | ||
from .tuner.paddle import get_device | ||
|
||
get_device(device) | ||
|
||
import paddle | ||
|
||
embeddings = embed_model(paddle.Tensor(docs.blobs)).numpy() | ||
docs.embeddings = embeddings |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import json | ||
|
||
import paddle | ||
import tensorflow as tf | ||
import torch | ||
|
This file contains 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,51 @@ | ||
import paddle | ||
import pytest | ||
import tensorflow as tf | ||
import torch | ||
from jina import DocumentArray, DocumentArrayMemmap | ||
|
||
from finetuner.embedding import set_embeddings | ||
from finetuner.toydata import generate_fashion_match | ||
|
||
embed_models = { | ||
'keras': lambda: tf.keras.Sequential( | ||
[ | ||
tf.keras.layers.Flatten(input_shape=(28, 28)), | ||
tf.keras.layers.Dense(128, activation='relu'), | ||
tf.keras.layers.Dense(32), | ||
] | ||
), | ||
'pytorch': lambda: torch.nn.Sequential( | ||
torch.nn.Flatten(), | ||
torch.nn.Linear( | ||
in_features=28 * 28, | ||
out_features=128, | ||
), | ||
torch.nn.ReLU(), | ||
torch.nn.Linear(in_features=128, out_features=32), | ||
), | ||
'paddle': lambda: paddle.nn.Sequential( | ||
paddle.nn.Flatten(), | ||
paddle.nn.Linear( | ||
in_features=28 * 28, | ||
out_features=128, | ||
), | ||
paddle.nn.ReLU(), | ||
paddle.nn.Linear(in_features=128, out_features=32), | ||
), | ||
} | ||
|
||
|
||
@pytest.mark.parametrize('framework', ['keras', 'pytorch', 'paddle']) | ||
def test_embedding_docs(framework, tmpdir): | ||
# works for DA | ||
embed_model = embed_models[framework]() | ||
docs = DocumentArray(generate_fashion_match(num_total=100)) | ||
set_embeddings(docs, embed_model) | ||
assert docs.embeddings.shape == (100, 32) | ||
|
||
# works for DAM | ||
dam = DocumentArrayMemmap(tmpdir) | ||
dam.extend(generate_fashion_match(num_total=42)) | ||
set_embeddings(dam, embed_model) | ||
assert dam.embeddings.shape == (42, 32) |
Empty file.