copied
Readme
Files and versions
2.6 KiB
Image Embedding with ISC
author: Jael Gu
Description
An image embedding operator generates a vector given an image. This operator extracts features for image top ranked models from Image Similarity Challenge 2021 - Descriptor Track. The default pretrained model weights are from The 1st Place Solution of ISC21 (Descriptor Track).
Code Example
Load an image from path './towhee.jpg' and use the pretrained ISC model ('resnet50') to generate an image embedding.
Write a same pipeline with explicit inputs/outputs name specifications:
- option 1:
from towhee.dc2 import pipe, ops, DataCollection
p = (
pipe.input('path')
.map('path', 'img', ops.image_decode())
.map('img', 'vec', ops.image_embedding.isc())
.output('img', 'vec')
)
DataCollection(p('towhee.jpeg')).show()
- option 2:
import towhee
(
towhee.glob['path']('./towhee.jpg')
.image_decode['path', 'img']()
.image_embedding.isc['img', 'vec']()
.select['img', 'vec']()
.show()
)
Factory Constructor
Create the operator via the following factory method
image_embedding.isc(skip_preprocess=False, device=None)
Parameters:
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.
device: str
The device to run this operator, defaults to None. When it is None, 'cuda' will be used if it is available, otherwise 'cpu' is used.
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.
save_model(format='pytorch', path='default')
Save model to local with specified format.
Parameters:
format: str
The format of saved model, defaults to 'pytorch'.
path: str
The path where model is saved to. By default, it will save model to the operator directory.
from towhee import ops
op = ops.image_embedding.isc().get_op()
op.save_model('onnx', 'test.onnx')
2.6 KiB
Image Embedding with ISC
author: Jael Gu
Description
An image embedding operator generates a vector given an image. This operator extracts features for image top ranked models from Image Similarity Challenge 2021 - Descriptor Track. The default pretrained model weights are from The 1st Place Solution of ISC21 (Descriptor Track).
Code Example
Load an image from path './towhee.jpg' and use the pretrained ISC model ('resnet50') to generate an image embedding.
Write a same pipeline with explicit inputs/outputs name specifications:
- option 1:
from towhee.dc2 import pipe, ops, DataCollection
p = (
pipe.input('path')
.map('path', 'img', ops.image_decode())
.map('img', 'vec', ops.image_embedding.isc())
.output('img', 'vec')
)
DataCollection(p('towhee.jpeg')).show()
- option 2:
import towhee
(
towhee.glob['path']('./towhee.jpg')
.image_decode['path', 'img']()
.image_embedding.isc['img', 'vec']()
.select['img', 'vec']()
.show()
)
Factory Constructor
Create the operator via the following factory method
image_embedding.isc(skip_preprocess=False, device=None)
Parameters:
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.
device: str
The device to run this operator, defaults to None. When it is None, 'cuda' will be used if it is available, otherwise 'cpu' is used.
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.
save_model(format='pytorch', path='default')
Save model to local with specified format.
Parameters:
format: str
The format of saved model, defaults to 'pytorch'.
path: str
The path where model is saved to. By default, it will save model to the operator directory.
from towhee import ops
op = ops.image_embedding.isc().get_op()
op.save_model('onnx', 'test.onnx')