logo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readme
Files and versions

99 lines
2.4 KiB

# Image Embedding with SWAG
*author: [Jael Gu](https://github.com/jaelgu*
<br />
## Description
An image embedding operator generates a vector given an image.
This operator extracts features for image with pretrained [SWAG](https://github.com/facebookresearch/SWAG) models from [Torch Hub](https://pytorch.org/docs/stable/hub.html).
SWAG implements models from the paper [Revisiting Weakly Supervised Pre-Training of Visual Perception Models](https://arxiv.org/abs/2201.08371).
To achieve higher accuracy in image classification, SWAG uses hashtags to perform weakly supervised learning instead of fully supervised pretraining with image class labels.
<br />
## Code Example
Load an image from path './towhee.jpg'
and use the pretrained SWAG model 'vit_b16_in1k' to generate an image embedding.
*Write the pipeline in simplified style:*
```python
import towhee
(
towhee.glob('./towhee.jpg')
.image_decode()
.image_embedding.swag(model_name='vit_b16_in1k')
.show()
)
```
<img src="./result1.png" width="800px"/>
*Write a same pipeline with explicit inputs/outputs name specifications:*
```python
import towhee
(
towhee.glob['path']('./towhee.jpg')
.image_decode['path', 'img']()
.image_embedding.swag['img', 'vec'](model_name='vit_b16_in1k')
.select['img', 'vec']()
.show()
)
```
<img src="./result2.png" width="800px"/>
<br />
## Factory Constructor
Create the operator via the following factory method
***image_embedding.swag(model_name='vit_b16_in1k', skip_preprocess=False)***
**Parameters:**
***model_name:*** *str*
The model name in string. The default value is "vit_b16_in1k".
Supported model names:
- vit_b16_in1k
- vit_l16_in1k
- vit_h14_in1k
- regnety_16gf_in1k
- regnety_32gf_in1k
- regnety_128gf_in1k
***skip_preprocess:*** *bool*
The flag to control whether to skip image preprocess.
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:**
***img:*** *towhee.types.Image (a sub-class of numpy.ndarray)*
The decoded image data in numpy.ndarray.
**Returns:** *numpy.ndarray*
The image embedding extracted by model.
3 years ago