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

Updated 1 year ago

image-embedding

Image Embedding with SWAG

author: Jael Gu


Description

An image embedding operator generates a vector given an image. This operator extracts features for image with pretrained SWAG models from Torch Hub. SWAG implements models from the paper Revisiting Weakly Supervised Pre-Training of Visual Perception Models. To achieve higher accuracy in image classification, SWAG uses hashtags to perform weakly supervised learning instead of fully supervised pretraining with image class labels.


Code Example

Load an image from path './towhee.jpg' and use the pretrained SWAG model 'vit_b16_in1k' to generate an image embedding.

Write a pipeline with explicit inputs/outputs name specifications:

from towhee import pipe, ops, DataCollection

p = (
    pipe.input('path')
        .map('path', 'img', ops.image_decode())
        .map('img', 'vec', ops.image_embedding.swag(model_name='vit_b16_in1k'))
        .output('img', 'vec')
)

DataCollection(p('towhee.jpeg')).show()


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.


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.

Jael Gu 20caca02b8 Remove dc2 8 Commits
file-icon .gitattributes
1.1 KiB
download-icon
Initial commit 2 years ago
file-icon README.md
2.2 KiB
download-icon
Remove dc2 1 year ago
file-icon __init__.py
664 B
download-icon
Add SWAG 2 years ago
file-icon requirements.txt
59 B
download-icon
Add SWAG 2 years ago
file-icon result.png
80 KiB
download-icon
Update 1 year ago
file-icon swag.py
5.3 KiB
download-icon
rm _types 1 year ago
file-icon towhee.jpeg
49 KiB
download-icon
Update 1 year ago