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

100 lines
3.2 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.
from re import I
import sys
import os
import pathlib
import pickle
from argparse import Namespace
import torch
import torchvision
from torchvision import transforms
from transformers import GPT2Tokenizer
from towhee.types.arg import arg, to_image_color
from towhee.types.image_utils import to_pil
from towhee.operator.base import NNOperator, OperatorFlag
from towhee import register
from towhee.models import clip
class Magic(NNOperator):
"""
Magic image captioning operator
"""
def __init__(self, model_name: str):
super().__init__()
path = str(pathlib.Path(__file__).parent)
sys.path.append(path)
from clip import CLIP
from simctg import SimCTG
sys.path.pop()
self.device = "cuda" if torch.cuda.is_available() else "cpu"
# Load Language Model
language_model_name = r'cambridgeltl/magic_mscoco' # or r'/path/to/downloaded/cambridgeltl/magic_mscoco'
sos_token, pad_token = r'<-start_of_text->', r'<-pad->'
self.generation_model = SimCTG(language_model_name, sos_token, pad_token).to(self.device)
self.generation_model.eval()
model_name = r"openai/clip-vit-base-patch32" # or r"/path/to/downloaded/openai/clip-vit-base-patch32"
self.clip = CLIP(model_name).to(self.device)
self.clip.eval()
def _preprocess(self, img):
img = to_pil(img)
processed_img = self.transf_1(img)
processed_img = self.transf_2(processed_img)
processed_img = processed_img.to(self.device)
return processed_img
@arg(1, to_image_color('RGB'))
def inference_single_data(self, data):
text = self._inference_from_image(data)
return text
def __call__(self, data):
if not isinstance(data, list):
data = [data]
else:
data = data
results = []
for single_data in data:
result = self.inference_single_data(single_data)
results.append(result)
if len(data) == 1:
return results[0]
else:
return results
@arg(1, to_image_color('RGB'))
def _inference_from_image(self, img):
#img = self._preprocess(img).unsqueeze(0)
k, alpha, beta, decoding_len = 45, 0.1, 2.0, 16
eos_token = '<|endoftext|>'
with torch.no_grad():
output = generation_model.magic_search(input_ids, k,
alpha, decoding_len, beta, image_instance, clip, 60)
return out
def _configs(self):
config = {}
config['expansionnet_rf'] = {}
config['expansionnet_rf']['weights'] = 'rf_model.pth'
return config