EasyCV/easycv/predictors/interface.py

109 lines
3.8 KiB
Python

# Copyright (c) Alibaba, Inc. and its affiliates.
import abc
class PredictorInterface(object):
version = 1
def __init__(self, model_path, model_config=None):
"""
init model
Args:
model_path: init model from this directory
model_config: config string for model to init, in json format
"""
pass
@abc.abstractmethod
def predict(self, input_data, batch_size):
"""
using session run predict a number of samples using batch_size
Args:
input_data: a list of numpy array, each array is a sample to be predicted
batch_size: batch_size passed by the caller, you can also ignore this param and
use a fixed number if you do not want to adjust batch_size in runtime
Return:
result: a list of dict, each dict is the prediction result of one sample
eg, {"output1": value1, "output2": value2}, the value type can be
python int str float, and numpy array
"""
pass
def get_output_type(self):
"""
in this function user should return a type dict, which indicates
which type of data should the output of predictor be converted to
* type json, data will be serialized to json str
* type image, data will be converted to encode image binary and write to oss file,
whose name is output_dir/${key}/${input_filename}_${idx}.jpg, where input_filename
is extracted from url, key corresponds to the key in the dict of output_type,
if the type of data indexed by key is a list, idx is the index of element in list, otherwhile ${idx} will be empty
* type video, data will be converted to encode video binary and write to oss file,
:: return {
'image': 'image',
'feature': 'json'
}
indicating that the image data in the output dict will be save to image
file and feature in output dict will be converted to json
"""
return {}
class PredictorInterfaceV2(PredictorInterface):
version = 2
def __init__(self, model_path, model_config=None):
"""
init model
Args:
model_path: init model from this directory
model_config: config string for model to init, in json format
"""
pass
def get_output_type(self):
"""
in this function user should return a type dict, which indicates
which type of data should the output of predictor be converted to
* type json, data will be serialized to json str
* type image, data will be converted to encode image binary and write to oss file,
whose name is output_dir/${key}/${input_filename}_${idx}.jpg, where input_filename
is the base filename extracted from url, key corresponds to the key in the dict of output_type,
if the type of data indexed by key is a list, idx is the index of element in list, otherwhile ${idx} will be empty
* type video, data will be converted to encode video binary and write to oss file,
:: return {
'image': 'image',
'feature': 'json'
}
indicating that the image data in the output dict will be save to image
file and feature in output dict will be converted to json
"""
return {}
@abc.abstractmethod
def predict(self, input_data_dict_list, batch_size):
"""
using session run predict a number of samples using batch_size
Args:
input_data_dict_list: a list of dict, each dict is a sample data to be predicted
batch_size: batch_size passed by the caller, you can also ignore this param and
use a fixed number if you do not want to adjust batch_size in runtime
Return:
result: a list of dict, each dict is the prediction result of one sample
eg, {"output1": value1, "output2": value2}, the value type can be
python int str float, and numpy array
"""
pass