easy_rec.python.inference

easy_rec.python.inference.predictor

class easy_rec.python.inference.predictor.PredictorInterface(model_path, model_config=None)[source]

Bases: object

version = 1
__init__(model_path, model_config=None)[source]

Init tensorflow session and load tf model.

Parameters
  • model_path – init model from this directory

  • model_config – config string for model to init, in json format

abstract predict(input_data, batch_size)[source]

Using session run predict a number of samples using batch_size.

Parameters
  • 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

Returns

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

Return type

result

get_output_type()[source]

Get output types of prediction.

In this function user should return a type dict, which indicates which type of data should the output of predictor be converted to.

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,

eg: 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

classmethod create_class(name)
class easy_rec.python.inference.predictor.PredictorImpl(model_path, profiling_file=None)[source]

Bases: object

__init__(model_path, profiling_file=None)[source]

Impl class for predictor.

Parameters
  • model_path – saved_model directory or frozenpb file path

  • profiling_file – profiling result file, default None. if not None, predict function will use Timeline to profiling prediction time, and the result json will be saved to profiling_file

property input_names
property output_names
search_pb(directory)[source]

Search pb file recursively in model directory. if multiple pb files exist, exception will be raised.

If multiple pb files exist, exception will be raised.

Parameters

directory – model directory.

Returns

directory contain pb file

predict(input_data_dict, output_names=None)[source]

Predict input data with loaded model.

Parameters
  • input_data_dict – a dict containing all input data, key is the input name, value is the corresponding value

  • output_names – if not None, will fetch certain outputs, if set None, will return all the output info according to the output info in model signature

Returns

a dict of outputs, key is the output name, value is the corresponding value

class easy_rec.python.inference.predictor.Predictor(model_path, profiling_file=None)[source]

Bases: easy_rec.python.inference.predictor.PredictorInterface

__init__(model_path, profiling_file=None)[source]

Initialize a Predictor.

Parameters
  • model_path – saved_model directory or frozenpb file path

  • profiling_file – profiling result file, default None. if not None, predict function will use Timeline to profiling prediction time, and the result json will be saved to profiling_file

property input_names

Input names of the model.

Returns

a list, which conaining the name of input nodes available in model

property output_names

Output names of the model.

Returns

a list, which conaining the name of outputs nodes available in model

predict_impl(input_table, output_table, all_cols='', all_col_types='', selected_cols='', reserved_cols='', output_cols=None, batch_size=1024, slice_id=0, slice_num=1, input_sep=',', output_sep='\x01')[source]

Predict table input with loaded model.

Parameters
  • input_table – table/file_path to read

  • output_table – table/file_path to write

  • all_cols – union of columns

  • all_col_types – data types of the columns

  • selected_cols – included column names, comma separated, such as “a,b,c”

  • reserved_cols – columns to be copy to output_table, comma separated, such as “a,b”

  • output_cols – output columns, comma separated, such as “y float, embedding string”, the output names[y, embedding] must be in saved_model output_names

  • batch_size – predict batch size

  • slice_id – when multiple workers write the same table, each worker should be assigned different slice_id, which is usually slice_id

  • slice_num – table slice number

  • input_sep – separator of input file.

  • output_sep – separator of predict result file.

predict_csv(input_path, output_path, reserved_cols, output_cols, batch_size, slice_id, slice_num, input_sep, output_sep)[source]
predict_table(input_table, output_table, all_cols, all_col_types, selected_cols, reserved_cols, output_cols=None, batch_size=1024, slice_id=0, slice_num=1)[source]
predict(input_data_dict_list, output_names=None, batch_size=1)[source]

Predict input data with loaded model.

Parameters
  • input_data_dict_list – list of dict

  • output_names – if not None, will fetch certain outputs, if set None, will

  • batch_size – batch_size used to predict, -1 indicates to use the real batch_size

Returns

a list of dict, each dict contain a key-value pair for output_name, output_value

batch(data_list)[source]

Batching the data.

classmethod create_class(name)