Artifacts

SklearnModelArtifact

class bentoml.artifact.SklearnModelArtifact(name, pickle_extension='.pkl')

Abstraction for saving/loading scikit learn models using sklearn.externals.joblib

Parameters
  • name (str) – Name for the artifact

  • pickle_extension (str) – The extension format for pickled file

Raises

ImportError – sklean package is required for SklearnModelArtifact

PytorchModelArtifact

class bentoml.artifact.PytorchModelArtifact(name, file_extension='.pt')

Abstraction for saving/loading objects with torch.save and torch.load

Parameters

name (string) – name of the artifact

Raises
  • ImportError – torch package is required for PytorchModelArtifact

  • TypeError – invalid argument type, model being packed must be instance of torch.nn.Module

KerasModelArtifact

class bentoml.artifact.KerasModelArtifact(name, custom_objects=None, model_extension='.h5', store_as_json_and_weights=False)

Abstraction for saving/loading Keras model

Parameters
  • name (string) – name of the artifact

  • custom_objects (dict) – dictionary of Keras custom objects for model

  • store_as_json_and_weights (bool) – flag allowing storage of the Keras model as JSON and weights

Raises
  • ImportError – keras or tensorflow.keras package is required for KerasModelArtifact

  • TypeError – invalid argument type, model being packed must be instance of keras.engine.network.Network, tf.keras.models.Model, or their aliases

FastaiModelArtifact

class bentoml.artifact.FastaiModelArtifact(name)

Saving and Loading FastAI Model

Parameters

name (str) – Name for the fastai model

Raises
  • ImportError – Require fastai package to use Fast ai model artifact

  • TypeError – invalid argument type, model being packed must be instance of fastai.basic_train.Learner

TensorflowSavedModelArtifact

class bentoml.artifact.TensorflowSavedModelArtifact(name)

Abstraction for saving/loading Tensorflow model in tf.saved_model format

Parameters

name (string) – name of the artifact

Raises

ImportError – tensorflow package is required for TensorflowSavedModelArtifact

Example usage:

>>> import tensorflow as tf
>>>
>>> # Option 1: custom model with specific method call
>>> class Adder(tf.Module):
>>>     @tf.function(input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])
>>>     def add(self, x):
>>>         return x + x + 1.
>>> model_to_save = Adder()
>>> # ... compiling, training, etc
>>>
>>> # Option 2: Sequential model (direct call only)
>>> model_to_save = tf.keras.Sequential([
>>>     tf.keras.layers.Flatten(input_shape=(28, 28)),
>>>     tf.keras.layers.Dense(128, activation='relu'),
>>>     tf.keras.layers.Dense(10, activation='softmax')
>>> ])
>>> # ... compiling, training, etc
>>>
>>> import bentoml
>>> from bentoml.handlers import JsonHandler
>>> from bentoml.artifact import TensorflowSavedModelArtifact
>>>
>>> @bentoml.env(pip_dependencies=["tensorflow"])
>>> @bentoml.artifacts([TensorflowSavedModelArtifact('model')])
>>> class TfModelService(bentoml.BentoService):
>>>
>>>     @bentoml.api(JsonHandler)
>>>     def predict(self, json):
>>>         input_data = json['input']
>>>         prediction = self.artifacts.model.add(input_data)
>>>         # prediction = self.artifacts.model(input_data)  # if Sequential mode
>>>         return prediction.numpy()
>>>
>>> svc = TfModelService()
>>>
>>> # Option 1: pack directly with Tensorflow trackable object
>>> svc.pack('model', model_to_save)
>>>
>>> # Option 2: save to file path then pack
>>> tf.saved_model.save(model_to_save, '/tmp/adder/1')
>>> svc.pack('model', '/tmp/adder/1')

XgboostModelArtifact

class bentoml.artifact.XgboostModelArtifact(name, model_extension='.model')

Abstraction for save/load object with Xgboost.

Parameters
  • name (string) – name of the artifact

  • model_extension (string) – Extension name for saved xgboost model

Raises
  • ImportError – xgboost package is required for using XgboostModelArtifact

  • TypeError – invalid argument type, model being packed must be instance of xgboost.core.Booster

H2oModelArtifact

class bentoml.artifact.H2oModelArtifact(name)

Abstraction for saving/loading objects with h2o.save_model and h2o.load_model

Parameters

name (str) – Name for this h2o artifact..

Raises

ImportError – h2o package is required to use H2o model artifact

PickleArtifact

class bentoml.artifact.PickleArtifact(name, pickle_module=<module 'bentoml.utils.cloudpickle' from '/home/docs/checkouts/readthedocs.org/user_builds/bentoml/checkouts/latest/bentoml/utils/cloudpickle.py'>, pickle_extension='.pkl')

Abstraction for saving/loading python objects with pickle serialization

Parameters
  • name (str) – Name for the artifact

  • pickle_module (module|str) – The python module will be used for pickle and unpickle artifact, default pickle module in BentoML’s fork of cloudpickle, which is identical to the Apache Spark fork

  • pickle_extension (str) – The extension format for pickled file.

TextFileArtifact

class bentoml.artifact.TextFileArtifact(name, file_extension='.txt', encoding='utf8')

Abstraction for saving/loading string to/from text files

Parameters
  • name (str) – Name of the artifact

  • file_extension (str, optional) – The file extention used for the saved text file. Defaults to “.txt”

  • encoding (str) – The encoding will be used for saving/loading text. Defaults to “utf8”