# Video-Text Retrieval Embedding with Frozen In Time

*author: Jinling Xu*

<br />

## Description

This operator extracts features for video or text with [Frozen In Time](https://arxiv.org/abs/2104.00650) which can generate embeddings for text and video by jointly training a video encoder and text encoder to maximize the cosine similarity.


<br />


## Code Example

Load a video from path './demo_video.mp4' to generate a video embedding. 

Read the text 'kids feeding and playing with the horse' to generate a text embedding. 

 *Write the pipeline in simplified style*:

- Encode video (default):
```python
import towhee
towhee.dc(['./demo_video.mp4']) \
      .video_decode.ffmpeg(sample_type='uniform_temporal_subsample', args={'num_samples': 4}) \
      .runas_op(func=lambda x: [y for y in x]) \
      .video_text_embedding.frozen_in_time(model_name='frozen_in_time_base_16_244', modality='video', device='cpu') \
      .show()

```
<img src="./result1.png" width="800px"/>

- Encode text:
```python
import towhee

towhee.dc(['kids feeding and playing with the horse']) \
      .video_text_embedding.frozen_in_time(model_name='frozen_in_time_base_16_244', modality='text', device='cpu') \
      .show()
```
<img src="./result2.png" width="800px"/>

*Write a same pipeline with explicit inputs/outputs name specifications:*

```python
import towhee

towhee.dc['path'](['./demo_video.mp4']) \
      .video_decode.ffmpeg['path', 'frames'](sample_type='uniform_temporal_subsample', args={'num_samples': 4}) \
      .runas_op['frames', 'frames'](func=lambda x: [y for y in x]) \
      .video_text_embedding.frozen_in_time['frames', 'vec'](model_name='frozen_in_time_base_16_244', modality='video', device='cpu') \
      .select['path', 'vec']() \
      .show(formatter={'path': 'video_path'})

towhee.dc['text'](["kids feeding and playing with the horse"]) \
      .video_text_embedding.frozen_in_time['text','vec'](model_name='frozen_in_time_base_16_244', modality='text', device='cpu') \
      .select['text', 'vec']() \
      .show()
```
<img src="./result3.png" width="800px"/>
<img src="./result4.png" width="800px"/>


<br />



## Factory Constructor

Create the operator via the following factory method

***frozen_in_time(model_name, modality, weight_path)***

**Parameters:**

​   ***model_name:*** *str*

​   The model name of frozen in time. Supported model names: 
- frozen_in_time_base_16_244


​   ***modality:*** *str*

​   Which modality(*video* or *text*) is used to generate the embedding. 

​   ***weight_path:*** *str*

​   pretrained model weights path.  

<br />



## Interface

An video-text embedding operator takes a list of [Towhee VideoFrame](link/to/towhee/image/api/doc) or string as input and generate an embedding in ndarray.


**Parameters:**

​	***data:*** *List[towhee.types.Image]*  or *str*

​  The data (list of  Towhee VideoFrame (which is uniform subsampled from a video) or text based on specified modality) to generate embedding.	



**Returns:** *numpy.ndarray*

​   The data embedding extracted by model.