dlhub_sdk.models.servables package

Subpackages

Submodules

dlhub_sdk.models.servables.keras module

class dlhub_sdk.models.servables.keras.KerasModel

Bases: dlhub_sdk.models.servables.python.BasePythonServableModel

Servable based on a Keras Model object.

Assumes that the model has been saved to an hdf5 file

add_custom_object(name, custom_object)

Add a custom layer to the model specification

See Keras FAQs <https://keras.io/getting-started/faq/#handling-custom-layers-or-other-custom-objects-in-saved-models> for details.

Parameters:
  • name (string) – Name of the custom object
  • custom_object (class) – Class of the custom object
Returns:

self

classmethod create_model(model_path, output_names=None, arch_path=None, custom_objects=None, force_tf_keras: bool = False) → dlhub_sdk.models.servables.keras.KerasModel

Initialize a Keras model.

Parameters:
  • model_path (string) – Path to the hd5 file that contains the weights and, optionally, the architecture
  • output_names ([string] or [[string]]) – Names of output classes.
  • arch_path (string) – Path to the hd5 model containing the architecture, if not available in the file at model_path.
  • custom_objects (dict) – Map of layer names to custom layers. See Keras Documentation for more details.
  • force_tf_keras (bool) – Force the use of TF.Keras even if keras is installed
format_layer_spec(layers) → dlhub_sdk.models.servables.ArgumentTypeMetadata

Make a description of a list of input or output layers

Parameters:layers (tuple or [tuple]) – Shape of the layers
Returns:(dict) Description of the inputs / outputs

dlhub_sdk.models.servables.pytorch module

class dlhub_sdk.models.servables.pytorch.TorchModel

Bases: dlhub_sdk.models.servables.python.BasePythonServableModel

Servable based on a Torch Model object.

Assumes that the model has been saved to a pt or a pth file

classmethod create_model(model_path, input_shape, output_shape, input_type='float', output_type='float')

Initialize a PyTorch model.

Parameters:
  • model_path (string) – Path to the pt or pth file that contains the weights and the architecture
  • input_shape (tuple or [tuple]) – Shape of input matrix to model
  • output_shape (tuple or [tuple]) – Shape of output matrix from model
  • input_type (str or [str]) – Data type of inputs
  • output_type (str or [str]) – Data type of outputs
format_layer_spec(layers, datatypes)

Make a description of a list of input or output layers

Parameters:
  • layers (tuple or [tuple]) – Shape of the layers
  • datatypes (str or [str]) – Data type of each input layer
Returns:

(dict) Description of the inputs / outputs

dlhub_sdk.models.servables.python module

Tools to annotate generic operations (e.g., class method calls) in Python

class dlhub_sdk.models.servables.python.BasePythonServableModel

Bases: dlhub_sdk.models.servables.BaseServableModel

Describes a static python function to be run

classmethod create_model(method, function_kwargs=None) → dlhub_sdk.models.servables.python.BasePythonServableModel

Initialize a model for a python object

Parameters:
  • method (string) – Name of the method for this class
  • function_kwargs (dict) – Names and values of any other argument of the function to set the values must be JSON serializable.
set_input_description(description, method='run')

Set the human-readable description for this servable’s inputs

This method can be called when implementing a Keras, PyTorch, etc. servable to fill in an empty input description.

Parameters:
  • description (string) – Human-readable description of the servable’s inputs
  • method (string) – Name of the servable method to apply description to (by default, ‘run’)
set_inputs(data_type, description, shape=(), item_type=None, **kwargs)

Define the inputs to the default (“run”) function

Parameters:
  • data_type (string) – Type of the input data
  • description (string) – Human-friendly description of the data
  • shape (list) – Required for data_type of list or ndarray. Use None for dimensions that can have any numbers of values
  • item_type (string/dict) – Description of the item type. Required for data_type = list
set_output_description(description, method='run')

Set the human-readable description for this servable’s inputs

This method can be called when implementing a Keras, PyTorch, etc. servable to fill in an empty input description.

Parameters:
  • description (string) – Human-readable description of the servable’s inputs
  • method (string) – Name of the servable method to apply description to (by default, ‘run’)
set_outputs(data_type, description, shape=(), item_type=None, **kwargs)

Define the outputs to the default (“run”) function

Parameters:
  • data_type (string) – Type of the output data
  • description (string) – Human-friendly description of the data
  • shape (list) – Required for data_type of ndarray. Use None for dimensions that can have any numbers of values
  • item_type (string) – Description of the type of item in a list
set_unpack_inputs(x, method_name='run')

Define whether the inputs need to be unpacked before executing the function

Set to true if the function takes more than one input. Otherwise, the default is False

Parameters:
  • x (bool) – Desired setting
  • method_name (str) – Name of the method to modify
Returns:

self

Return type:

(BasePythonServableModel)

class dlhub_sdk.models.servables.python.PythonClassMethodModel

Bases: dlhub_sdk.models.servables.python.BasePythonServableModel

Model for describing servables where the function to be performed is a method of a class.

To use this model, you must define the path to the pickled object and the function of that object to be called. Any additional libraries (beyond the standard libraries) required to run the library and their versions must also be specified. You may also specify any arguments of the class that should be set as defaults.

classmethod create_model(path, method, function_kwargs=None, *, auto_inspect=False) → dlhub_sdk.models.servables.python.PythonClassMethodModel

Initialize a model for a python object

Parameters:
  • path (string) – Path to a pickled Python file
  • method (string) – Name of the method for this class
  • function_kwargs (dict) – Names and values of any other argument of the function to set the values must be JSON serializable.
  • auto_inspect (boolean) – Whether or not to attempt to automatically extract inputs from the function
class dlhub_sdk.models.servables.python.PythonStaticMethodModel

Bases: dlhub_sdk.models.servables.python.BasePythonServableModel

Model for a servable that calls a Python static method. Static methods can be called without any other context, unlike the class methods in PickledClassServableModelBase.

An example static method is the sqrt operation from numpy, numpy.sqrt. You can make a model of this function by calling PythonStaticMethodModel.from_function_pointer(numpy.sqrt).

classmethod create_model(module=None, method=None, autobatch=False, function_kwargs=None, *, f=None, auto_inspect=False)

Initialize the method based on the provided arguments

Parameters:
  • module (string) – Name of the module holding the function
  • method (string) – Name of the method for this class
  • autobatch (bool) – Whether to automatically run this function on a list of inputs. Calls map(f, list)
  • function_kwargs (dict) – Names and values of any other argument of the function to set the values must be JSON serializable.
  • f (object) – function pointer to the desired python function
  • auto_inspect (boolean) – Whether or not to attempt to automatically extract inputs from the function
Raises:

TypeError – If there is no valid way to process the given arguments

classmethod from_function_pointer(f, autobatch=False, function_kwargs=None)

Construct the module given a function pointer :param f: Function pointer to the Python function to be published :type f: object :param autobatch: Whether to run function on an iterable of entries :type autobatch: bool :param function_kwargs: Any default options for this function :type function_kwargs: dict

dlhub_sdk.models.servables.python.add_extracted_metadata(func, model: dlhub_sdk.models.servables.python.BasePythonServableModel) → dlhub_sdk.models.servables.python.BasePythonServableModel

Helper function for adding generated input/output metadata to a model object :param func: a pointer to the function whose data is to be extracted :param model: the model who needs its data to be updated :type model: BasePythonServableModel

Returns:the model that was given after it is updated
Return type:(BasePythonServableModel)

dlhub_sdk.models.servables.sklearn module

class dlhub_sdk.models.servables.sklearn.ScikitLearnModel

Bases: dlhub_sdk.models.servables.python.BasePythonServableModel

Metadata for a scikit-learn machine learning model

This class is build assuming that the inputs to the model will be a list of lists of fixed lengths. Models that take different kinds of inputs (e.g., Pipelines that include text-processing steps, KernelRidge models with custom kernel functions) will are not yet supported.

classmethod create_model(path, n_input_columns, classes=None, serialization_method='pickle')

Initialize a scikit-learn model

Parameters:
  • path (string) – Path to model file
  • n_input_columns (int) – Number of input columns for the model
  • classes (Union[int,tuple]) – For classification models, number of output classes or a list-like object with the names of the classes
  • serialization_method (string) – Library used to serialize model
inspect_model(model)

Extract and store metadata that describes an ML model

Parameters:model (BaseEstimator) – Model to be inspected

dlhub_sdk.models.servables.tensorflow module

class dlhub_sdk.models.servables.tensorflow.TensorFlowModel

Bases: dlhub_sdk.models.servables.BaseServableModel

Class for generating descriptions of a TensorFlow model

Assumes that a model has been saved using the tf.saved_model module. Users must provide the output directory for the Tensorflow model, and this tool will infer all functions that were defined for this model.

Note: DLHub assumes that the default method for a servable is “run” and TensorFlow assumes it to be tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY (currently defined as “serving_default”). We will rename the default method to “run”

classmethod create_model(export_directory)

Initialize the desription of a TensorFlow model

Parameters:export_directory (string) – Path to the output directory of a Tensorflow model

Module contents

class dlhub_sdk.models.servables.ArgumentTypeMetadata

Bases: pydantic.main.BaseModel

Description of an input argument

class dlhub_sdk.models.servables.BaseServableModel

Bases: dlhub_sdk.models.BaseMetadataModel

Base class for servables. Holds the metadata for the object and how to create and run the servable object.

register_function(name, inputs, outputs, parameters=None, method_details=None)

Registers a new function to this servable

See compose_argument_type utility function for how to define the inputs and outputs to this function.

Parameters:
  • name (string) – Name of the function (e.g., “run”)
  • inputs (dict) – Description of inputs to the function
  • outputs (dict) – Description of the outputs of the function
  • parameters (dict) – Any additional parameters for the function and their default values
  • method_details (dict) – Any options used when constructing a shim to run this function.
class dlhub_sdk.models.servables.MethodMetadata

Bases: pydantic.main.BaseModel

Metadata that describes each method

class dlhub_sdk.models.servables.ServableMetadata

Bases: pydantic.main.BaseModel

Metadata for servable objects.

Captures the information that describe how to run a servable.

class Config

Bases: object

extra = 'allow'