|
|
|
# Image Embedding with Timm
|
|
|
|
|
|
|
|
*author: [Jael Gu](https://github.com/jaelgu), Filip*
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
|
|
|
An image embedding operator generates a vector given an image.
|
|
|
|
This operator extracts features for image with pre-trained models provided by [Timm](https://github.com/rwightman/pytorch-image-models).
|
|
|
|
Timm is a deep-learning library developed by [Ross Wightman](https://twitter.com/wightmanr),
|
|
|
|
who maintains SOTA deep-learning models and tools in computer vision.
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
## Code Example
|
|
|
|
|
|
|
|
Load an image from path './towhee.jpeg'
|
|
|
|
and use the pre-trained ResNet50 model ('resnet50') to generate an image embedding.
|
|
|
|
|
|
|
|
*Write the pipeline in simplified style:*
|
|
|
|
|
|
|
|
```python
|
|
|
|
import towhee
|
|
|
|
|
|
|
|
towhee.glob('./towhee.jpeg') \
|
|
|
|
.image_decode() \
|
|
|
|
.image_embedding.timm(model_name='resnet50') \
|
|
|
|
.show()
|
|
|
|
```
|
|
|
|
<img src="./result1.png" height="50px"/>
|
|
|
|
|
|
|
|
*Write a same pipeline with explicit inputs/outputs name specifications:*
|
|
|
|
|
|
|
|
```python
|
|
|
|
import towhee
|
|
|
|
|
|
|
|
towhee.glob['path']('./towhee.jpeg') \
|
|
|
|
.image_decode['path', 'img']() \
|
|
|
|
.image_embedding.timm['img', 'vec'](model_name='resnet50') \
|
|
|
|
.select['img', 'vec']() \
|
|
|
|
.show()
|
|
|
|
```
|
|
|
|
<img src="./result2.png" height="150px"/>
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
## Factory Constructor
|
|
|
|
|
|
|
|
Create the operator via the following factory method:
|
|
|
|
|
|
|
|
***image_embedding.timm(model_name='resnet34', num_classes=1000, skip_preprocess=False)***
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***model_name:*** *str*
|
|
|
|
|
|
|
|
The model name in string. The default value is "resnet34".
|
|
|
|
Refer to [Timm Docs](https://fastai.github.io/timmdocs/#List-Models-with-Pretrained-Weights) to get a full list of supported models.
|
|
|
|
|
|
|
|
***num_classes:*** *int*
|
|
|
|
|
|
|
|
The number of classes. The default value is 1000.
|
|
|
|
It is related to model and dataset.
|
|
|
|
|
|
|
|
***skip_preprocess:*** *bool*
|
|
|
|
|
|
|
|
The flag to control whether to skip image pre-process.
|
|
|
|
The default value is False.
|
|
|
|
If set to True, it will skip image preprocessing steps (transforms).
|
|
|
|
In this case, input image data must be prepared in advance in order to properly fit the model.
|
|
|
|
|
|
|
|
<br />
|
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
|
|
An image embedding operator takes a towhee image as input.
|
|
|
|
It uses the pre-trained model specified by model name to generate an image embedding in ndarray.
|
|
|
|
|
|
|
|
**Parameters:**
|
|
|
|
|
|
|
|
***data:*** *Union[List[towhee._types.Image], towhee._types.Image]*
|
|
|
|
|
|
|
|
The decoded image data in numpy.ndarray. It allows both single input and a list for batch input.
|
|
|
|
|
|
|
|
|
|
|
|
**Returns:** *numpy.ndarray*
|
|
|
|
|
|
|
|
If only 1 image input, then output is an image embedding in shape of (feature_dim,).
|
|
|
|
If a list of images as input, then output is a numpy.ndarray in shape of (batch_num, feature_dim).
|