@ -1,4 +1,4 @@
# Image-Text Retrieval Embdd ing with BLIP
# Image Caption ing with BLIP
*author: David Wang*
*author: David Wang*
@ -9,7 +9,7 @@
## Description
## Description
This operator extracts features for image or text with [BLIP ](https://arxiv.org/abs/2201.12086 ) 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 adaptation from [salesforce/BLIP ](https://github.com/salesforce/BLIP ).
This operator generates the caption with [BLIP ](https://arxiv.org/abs/2201.12086 ) which describes the content of the given image. This is an adaptation from [salesforce/BLIP ](https://github.com/salesforce/BLIP ).
< br / >
< br / >
@ -17,45 +17,33 @@ This operator extracts features for image or text with [BLIP](https://arxiv.org/
## Code Example
## Code Example
Load an image from path './teddy.jpg' to generate an image embedding.
Read the text 'A teddybear on a skateboard in Times Square.' to generate an text embedding.
Load an image from path './animals.jpg' to generate the caption.
*Write the pipeline in simplified style* :
*Write the pipeline in simplified style* :
```python
```python
import towhee
import towhee
towhee.glob('./teddy .jpg') \
towhee.glob('./animals .jpg') \
.image_decode() \
.image_decode() \
.image_text_embedding.blip(model_name='blip_base', modality='image') \
.show()
towhee.dc(["A teddybear on a skateboard in Times Square."]) \
.image_text_embedding.blip(model_name='blip_base', modality='text') \
.image_captioning.blip(model_name='blip_base') \
.select() \
.show()
.show()
```
```
< img src = "https://towhee.io/image-text-embedding/blip/raw/branch/main/vec1.png" alt = "result1" style = "height:20px;" / >
< img src = "https://towhee.io/image-text-embedding/blip/raw/branch/main/vec2.png" alt = "result2" style = "height:20px;" / >
< img src = "./cap.png" alt = "result1" style = "height:20px;" / >
*Write a same pipeline with explicit inputs/outputs name specifications:*
*Write a same pipeline with explicit inputs/outputs name specifications:*
```python
```python
import towhee
import towhee
towhee.glob['path']('./teddy .jpg') \
towhee.glob['path']('./animals .jpg') \
.image_decode['path', 'img']() \
.image_decode['path', 'img']() \
.image_text_embedding.blip['img', 'vec'](model_name='blip_base', modality='image') \
.select['img', 'vec']() \
.show()
towhee.dc['text'](["A teddybear on a skateboard in Times Square."]) \
.image_text_embedding.blip['text','vec'](model_name='blip_base', modality='text') \
.select['text', 'vec']() \
.image_captioning.blip['img', 'text'](model_name='blip_base') \
.select['img', 'text']() \
.show()
.show()
```
```
< img src = "https://towhee.io/image-text-embedding/blip/raw/branch/main/tabular1.png" alt = "result1" style = "height:60px;" / >
< img src = "https://towhee.io/image-text-embedding/blip/raw/branch/main/tabular2.png" alt = "result2" style = "height:60px;" / >
< img src = "./tabular.png" alt = "result2" style = "height:60px;" / >
< br / >
< br / >
@ -66,7 +54,7 @@ towhee.dc['text'](["A teddybear on a skateboard in Times Square."]) \
Create the operator via the following factory method
Create the operator via the following factory method
***blip(model_name, modality )***
***blip(model_name)***
**Parameters:**
**Parameters:**
@ -75,33 +63,23 @@ Create the operator via the following factory method
The model name of BLIP. Supported model names:
The model name of BLIP. Supported model names:
- blip_base
- blip_base
** *modality:*** *str*
Which modality(*image* or *text* ) is used to generate the embedding.
< br / >
< br / >
## Interface
## 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 .
An image-text embedding operator takes a [towhee image ](link/to/towhee/image/api/doc ) as input and generate the correspoing caption .
**Parameters:**
**Parameters:**
** *data:*** *towhee.types.Image (a sub-class of numpy.ndarray)* or *str*
** *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.
The image to generate embedding.
**Returns:** *str*
The caption generated by model.