From 88f71adf3740746eedc44ffc7af6b7cdfc763418 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Thu, 4 Aug 2022 13:39:24 +0800 Subject: [PATCH 1/5] fix: encode text first when both text and uri are presented --- server/clip_server/executors/helper.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/clip_server/executors/helper.py b/server/clip_server/executors/helper.py index da24597b9..0bc7b3ef3 100644 --- a/server/clip_server/executors/helper.py +++ b/server/clip_server/executors/helper.py @@ -73,12 +73,12 @@ def preproc_text( def split_img_txt_da(doc: 'Document', img_da: 'DocumentArray', txt_da: 'DocumentArray'): - if doc.uri: - img_da.append(doc) + if doc.text: + txt_da.append(doc) elif doc.blob or (doc.tensor is not None): img_da.append(doc) - elif doc.text: - txt_da.append(doc) + elif doc.uri: + img_da.append(doc) def set_rank(docs, _logit_scale=np.exp(4.60517)): From 33e3235e8b51d19b3f1d82446072a4b34163d394 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Thu, 4 Aug 2022 13:48:05 +0800 Subject: [PATCH 2/5] fix: encode text first when both text and uri are presented --- server/clip_server/executors/helper.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/clip_server/executors/helper.py b/server/clip_server/executors/helper.py index 0bc7b3ef3..3c97a34de 100644 --- a/server/clip_server/executors/helper.py +++ b/server/clip_server/executors/helper.py @@ -75,9 +75,7 @@ def preproc_text( def split_img_txt_da(doc: 'Document', img_da: 'DocumentArray', txt_da: 'DocumentArray'): if doc.text: txt_da.append(doc) - elif doc.blob or (doc.tensor is not None): - img_da.append(doc) - elif doc.uri: + elif doc.blob or (doc.tensor is not None) or doc.uri: img_da.append(doc) From b65445d18d7af7fb1848e264815bc7acabbf0e4c Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Thu, 4 Aug 2022 14:44:11 +0800 Subject: [PATCH 3/5] test: add split da test --- tests/test_helper.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_helper.py b/tests/test_helper.py index 5a61441c2..41ed1befb 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -1,6 +1,8 @@ import pytest import numpy as np from clip_server.executors.helper import numpy_softmax +from clip_server.executors.helper import split_img_txt_da +from docarray import Document, DocumentArray @pytest.mark.parametrize('shape', [(5, 10), (5, 10, 10)]) @@ -17,3 +19,42 @@ def test_numpy_softmax(shape, axis): np_softmax = numpy_softmax(logits, axis=axis) torch_softmax = torch.from_numpy(logits).softmax(dim=axis).numpy() np.testing.assert_array_almost_equal(np_softmax, torch_softmax) + + +@pytest.mark.parametrize( + 'inputs', + [ + ( + DocumentArray( + [ + Document(text='hello, world'), + Document(text='goodbye, world'), + Document( + text='hello, world', + uri='https://docarray.jina.ai/_static/favicon.png', + ), + Document( + uri='https://docarray.jina.ai/_static/favicon.png', + ), + ] + ), + (3, 1), + ), + ( + DocumentArray( + [ + Document(text='hello, world'), + Document(uri='https://docarray.jina.ai/_static/favicon.png'), + ] + ), + (1, 1), + ), + ], +) +def test_split_img_txt_da(inputs): + txt_da = DocumentArray() + img_da = DocumentArray() + for doc in inputs[0]: + split_img_txt_da(doc, txt_da, img_da) + assert len(txt_da) == inputs[1][0] + assert len(img_da) == inputs[1][1] From b03d3309afa3f3fc5c6dcedcddfadaee371b04fa Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Thu, 4 Aug 2022 14:50:32 +0800 Subject: [PATCH 4/5] fix: typo --- tests/test_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_helper.py b/tests/test_helper.py index 41ed1befb..895a11403 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -55,6 +55,6 @@ def test_split_img_txt_da(inputs): txt_da = DocumentArray() img_da = DocumentArray() for doc in inputs[0]: - split_img_txt_da(doc, txt_da, img_da) + split_img_txt_da(doc, img_da, txt_da) assert len(txt_da) == inputs[1][0] assert len(img_da) == inputs[1][1] From 6220a2a837963547a90b825cd992a960806bcc75 Mon Sep 17 00:00:00 2001 From: ZiniuYu Date: Thu, 4 Aug 2022 15:17:05 +0800 Subject: [PATCH 5/5] test: test split da --- tests/test_helper.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_helper.py b/tests/test_helper.py index 895a11403..f7d79ac62 100644 --- a/tests/test_helper.py +++ b/tests/test_helper.py @@ -40,6 +40,25 @@ def test_numpy_softmax(shape, axis): ), (3, 1), ), + ( + DocumentArray( + [ + Document(text='hello, world'), + Document(tensor=np.array([0, 1, 2])), + Document( + uri='https://docarray.jina.ai/_static/favicon.png' + ).load_uri_to_blob(), + Document( + tensor=np.array([0, 1, 2]), + uri='https://docarray.jina.ai/_static/favicon.png', + ), + Document( + uri='https://docarray.jina.ai/_static/favicon.png', + ), + ] + ), + (1, 4), + ), ( DocumentArray( [