ru-clip
copied
wxywb
2 years ago
9 changed files with 137 additions and 83 deletions
@ -1,67 +1,110 @@ |
|||
# Copyright 2021 Zilliz. All rights reserved. |
|||
# |
|||
# 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. |
|||
|
|||
@register(output_schema=['vec']) |
|||
class Clip(NNOperator): |
|||
""" |
|||
CLIP multi-modal embedding operator |
|||
""" |
|||
def __init__(self, model_name: str, modality: str): |
|||
self.modality = modality |
|||
self.device = "cuda" if torch.cuda.is_available() else "cpu" |
|||
self.model = clip.create_model(model_name=model_name, pretrained=True, jit=True) |
|||
self.tokenize = clip.tokenize |
|||
self.tfms = transforms.Compose([ |
|||
transforms.Resize(224, interpolation=transforms.InterpolationMode.BICUBIC), |
|||
transforms.CenterCrop(224), |
|||
transforms.ToTensor(), |
|||
transforms.Normalize( |
|||
(0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)) |
|||
]) |
|||
|
|||
def inference_single_data(self, data): |
|||
if self.modality == 'image': |
|||
vec = self._inference_from_image(data) |
|||
elif self.modality == 'text': |
|||
vec = self._inference_from_text(data) |
|||
else: |
|||
raise ValueError("modality[{}] not implemented.".format(self._modality)) |
|||
return vec.detach().cpu().numpy().flatten() |
|||
|
|||
def __call__(self, data): |
|||
if not isinstance(data, list): |
|||
data = [data] |
|||
else: |
|||
data = data |
|||
results = [] |
|||
for single_data in data: |
|||
result = self.inference_single_data(single_data) |
|||
results.append(result) |
|||
if len(data) == 1: |
|||
return results[0] |
|||
else: |
|||
return results |
|||
|
|||
def _inference_from_text(self, text): |
|||
text = self.tokenize(text).to(self.device) |
|||
text_features = self.model.encode_text(text) |
|||
return text_features |
|||
|
|||
@arg(1, to_image_color('RGB')) |
|||
def _inference_from_image(self, img): |
|||
img = to_pil(img) |
|||
image = self.tfms(img).unsqueeze(0).to(self.device) |
|||
image_features = self.model.encode_image(image) |
|||
return image_features |
|||
# Russian Image-Text Retrieval Embdding with CLIP |
|||
|
|||
*author: David Wang* |
|||
|
|||
|
|||
<br /> |
|||
|
|||
|
|||
|
|||
## Description |
|||
|
|||
This operator extracts features for image or text with [CLIP](https://arxiv.org/abs/2103.00020) which can generate embeddings for text and image by jointly training an image encoder and text encoder to maximize the cosine similarity. This is a Russian version of CLIP adopted from [ai-forever/ru-clip](https://github.com/ai-forever/ru-clip). |
|||
|
|||
|
|||
|
|||
<br /> |
|||
|
|||
|
|||
## Code Example |
|||
|
|||
Load an image from path './teddy.jpg' to generate an image embedding. |
|||
|
|||
Read the text 'Плюшевый мишка на скейтборде на Таймс-сквер.' to generate an text embedding. |
|||
|
|||
*Write the pipeline in simplified style*: |
|||
|
|||
```python |
|||
import towhee |
|||
|
|||
towhee.glob('./teddy.jpg') \ |
|||
.image_decode() \ |
|||
.image_text_embedding.ru_clip(model_name='ruclip-vit-base-patch32-224', modality='image') \ |
|||
.show() |
|||
|
|||
towhee.dc(["'Плюшевый мишка на скейтборде на Таймс-сквер."]) \ |
|||
.image_text_embedding.ru_clip(model_name='ruclip-vit-base-patch32-224', modality='text') \ |
|||
.show() |
|||
``` |
|||
<img src="./vec1.png" alt="result1" style="height:20px;"/> |
|||
<img src="./vec2.png" alt="result2" style="height:20px;"/> |
|||
|
|||
*Write a same pipeline with explicit inputs/outputs name specifications:* |
|||
|
|||
```python |
|||
import towhee |
|||
|
|||
towhee.glob['path']('./teddy.jpg') \ |
|||
.image_decode['path', 'img']() \ |
|||
.image_text_embedding.ru_clip['img', 'vec'](model_name='ruclip-vit-base-patch32-224', modality='image') \ |
|||
.select['img', 'vec']() \ |
|||
.show() |
|||
|
|||
towhee.dc['text'](["Плюшевый мишка на скейтборде на Таймс-сквер."]) \ |
|||
.image_text_embedding.ru_clip['text','vec'](model_name='ruclip-vit-base-patch32-224', modality='text') \ |
|||
.select['text', 'vec']() \ |
|||
.show() |
|||
``` |
|||
<img src="./tabular1.png" alt="result1" style="height:60px;"/> |
|||
<img src="./tabular2.png" alt="result2" style="height:60px;"/> |
|||
|
|||
|
|||
<br /> |
|||
|
|||
|
|||
|
|||
## Factory Constructor |
|||
|
|||
Create the operator via the following factory method |
|||
|
|||
***ru_clip(model_name, modality)*** |
|||
|
|||
**Parameters:** |
|||
|
|||
***model_name:*** *str* |
|||
|
|||
The model name of CLIP. Supported model names: |
|||
- ruclip-vit-base-patch32-224 |
|||
- ruclip-vit-base-patch16-224 |
|||
- ruclip-vit-large-patch14-224 |
|||
- ruclip-vit-large-patch14-336 |
|||
- ruclip-vit-base-patch32-384 |
|||
- ruclip-vit-base-patch16-384 |
|||
|
|||
|
|||
***modality:*** *str* |
|||
|
|||
Which modality(*image* or *text*) is used to generate the embedding. |
|||
|
|||
<br /> |
|||
|
|||
|
|||
|
|||
## Interface |
|||
|
|||
An image-text embedding operator takes a [towhee image](link/to/towhee/image/api/doc) or string as input and generate an embedding in ndarray. |
|||
|
|||
|
|||
**Parameters:** |
|||
|
|||
***data:*** *towhee.types.Image (a sub-class of numpy.ndarray)* or *str* |
|||
|
|||
The data (image or text based on specified modality) to generate embedding. |
|||
|
|||
|
|||
|
|||
**Returns:** *numpy.ndarray* |
|||
|
|||
The data embedding extracted by model. |
|||
|
|||
|
|||
|
@ -0,0 +1,19 @@ |
|||
# Copyright 2021 Zilliz. All rights reserved. |
|||
# |
|||
# 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 .ru_clip import RuClip |
|||
|
|||
|
|||
def ru_clip(model_name: str, modality: str): |
|||
return RuClip(model_name, modality) |
@ -0,0 +1,6 @@ |
|||
numpy |
|||
torch |
|||
torchvision |
|||
youtokentome |
|||
|
|||
|
After Width: | Height: | Size: 179 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 13 KiB |
Loading…
Reference in new issue