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

159 lines
5.6 KiB

# Copyright 2021 Zilliz. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
import numpy
from typing import Union, List
from pathlib import Path
import towhee
from towhee.operator.base import NNOperator, OperatorFlag
from towhee.types.arg import arg, to_image_color
from towhee import register
from towhee.models import isc
import torch
from torch import nn
from torchvision import transforms
from PIL import Image as PILImage
import timm
import warnings
warnings.filterwarnings('ignore')
log = logging.getLogger('isc_op')
@register(output_schema=['vec'])
class Isc(NNOperator):
"""
The operator uses pretrained ISC model to extract features for an image input.
Args:
skip_preprocess (`bool = False`):
Whether skip image transforms.
"""
def __init__(self,
timm_backbone: str = 'tf_efficientnetv2_m_in21ft1k',
img_size: int = 512,
checkpoint_path: str = None,
skip_preprocess: bool = False,
device: str = None) -> None:
super().__init__()
if device is None:
device = 'cuda' if torch.cuda.is_available() else 'cpu'
self.device = device
self.skip_tfms = skip_preprocess
self.timm_backbone = timm_backbone
if checkpoint_path is None:
checkpoint_path = os.path.join(str(Path(__file__).parent), 'checkpoints', timm_backbone + '.pth')
backbone = timm.create_model(timm_backbone, features_only=True, pretrained=False)
self.model = isc.create_model(pretrained=True, checkpoint_path=checkpoint_path, device=self.device,
backbone=backbone, p=3.0, eval_p=1.0)
self.model.eval()
self.tfms = transforms.Compose([
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
transforms.Normalize(mean=backbone.default_cfg['mean'],
std=backbone.default_cfg['std'])
])
def __call__(self, data: Union[List[towhee._types.Image], towhee._types.Image]):
if isinstance(data, towhee._types.Image):
imgs = [data]
else:
imgs = data
img_list = []
for img in imgs:
img = self.convert_img(img)
img = img if self.skip_tfms else self.tfms(img)
img_list.append(img)
inputs = torch.stack(img_list)
inputs = inputs.to(self.device)
features = self.model(inputs)
features = features.to('cpu')
if isinstance(data, list):
vecs = list(features.detach().numpy())
else:
vecs = features.squeeze(0).detach().numpy()
return vecs
def save_model(self, format: str = 'pytorch', path: str = 'default'):
if path == 'default':
path = str(Path(__file__).parent)
path = os.path.join(path, 'saved', format)
os.makedirs(path, exist_ok=True)
name = self.timm_backbone.replace('/', '-')
path = os.path.join(path, name)
dummy_input = torch.rand(1, 3, 224, 224)
if format == 'pytorch':
path = path + '.pt'
torch.save(self.model, path)
elif format == 'torchscript':
path = path + '.pt'
try:
try:
jit_model = torch.jit.script(self.model)
except Exception:
jit_model = torch.jit.trace(self.model, dummy_input, strict=False)
torch.jit.save(jit_model, path)
except Exception as e:
log.error(f'Fail to save as torchscript: {e}.')
raise RuntimeError(f'Fail to save as torchscript: {e}.')
elif format == 'onnx':
path = path + '.onnx'
try:
torch.onnx.export(self.model,
dummy_input,
path,
input_names=['input_0'],
output_names=['output_0'],
opset_version=14,
dynamic_axes={
'input_0': {0: 'batch_size', 2: 'height', 3: 'width'},
'output_0': {0: 'batch_size', 1: 'dim'}
},
do_constant_folding=True
)
except Exception as e:
log.error(f'Fail to save as onnx: {e}.')
raise RuntimeError(f'Fail to save as onnx: {e}.')
# todo: elif format == 'tensorrt':
else:
log.error(f'Unsupported format "{format}".')
@arg(1, to_image_color('RGB'))
def convert_img(self, img: towhee._types.Image):
img = PILImage.fromarray(img.astype('uint8'), 'RGB')
return img
# if __name__ == '__main__':
# from towhee import ops
#
# path = 'https://github.com/towhee-io/towhee/raw/main/towhee_logo.png'
#
# decoder = ops.image_decode.cv2()
# img = decoder(path)
#
# op = Isc()
# out = op(img)
# assert out.shape == (256,)