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 2 years ago

image-embedding

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 (towhee>=0.9.0):
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.jpg')).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')

Fine-tune

If you want to fine tune this operator, make sure your timm version is 0.4.12, and the higher version would cause model collapse during training.

Get start to fine-tune

Just call method op.train() and pass in your args.

import towhee

op = towhee.ops.image_embedding.isc().get_op()
op.train(training_args={
        'train_data_dir': './your_training_images',
        'distributed': False,
        'output_dir': './output',
        'gpu': 0,
        'epochs': 2,
        'batch_size': 128,
        'init_lr': 0.1
    })

These are each arg infos in training_args:

  • output_dir

    • default: './output'
    • metadata_dict: {'help': 'output checkpoint saving dir.'}
  • distributed

    • default: False
    • metadata_dict: {'help': 'If true, use all gpu in your machine, else use only one gpu.'}
  • gpu

    • default: 0
    • metadata_dict: {'help': 'When distributed is False, use this gpu No. in your machine.'}
  • start_epoch

    • default: 0
    • metadata_dict: {'help': 'Start epoch number.'}
  • epochs

    • default: 6
    • metadata_dict: {'help': 'End epoch number.'}
  • batch_size

    • default: 128
    • metadata_dict: {'help': 'Total batch size in all gpu.'}
  • init_lr

    • default: 0.1
    • metadata_dict: {'help': 'init learning rate in SGD.'}
  • train_data_dir

    • default: None
    • metadata_dict: {'help': 'The dir containing all training data image files.'}

Your custom training

Your can change training script in your way. Or your can refer to the original repo and paper to learn more about contrastive learning and image instance retrieval.

ChengZi 5db34032f6 add making saving dir 19 Commits
folder-icon checkpoints Add files 2 years ago
file-icon .gitattributes
1.1 KiB
download-icon
Initial commit 2 years ago
file-icon README.md
4.3 KiB
download-icon
add fine-tune guide in readme 2 years ago
file-icon __init__.py
660 B
download-icon
Add files 2 years ago
file-icon isc.py
6.7 KiB
download-icon
Fix load trainer issue 2 years ago
file-icon requirements.txt
23 B
download-icon
update timm version 2 years ago
file-icon result.png
124 KiB
download-icon
Update README 2 years ago
file-icon test_onnx.py
3.0 KiB
download-icon
Support tritonserve 2 years ago
file-icon train_isc.py
16 KiB
download-icon
add making saving dir 2 years ago