Skip to content
Permalink
Browse files

[DOC] add TensorflowSavedModelArtifact doc (#368)

* add TensorflowSavedModelArtifact to docs

* update TensorflowSavedModelArtifact.__doc__

* update more artifact related doc string

* watch-docs script should also watch code changes
  • Loading branch information...
parano committed Nov 8, 2019
1 parent 1849c21 commit a5c273585750bd6b41b2b7eae509a90dd11c759c
@@ -42,7 +42,8 @@ class FastaiModelArtifact(BentoServiceArtifact):
Raises:
ImportError: Require fastai package to use Fast ai model artifact
ValueError: Model is not instance of fast ai model
TypeError: invalid argument type, model being packed must be instance of
fastai.basic_train.Learner
"""

def __init__(self, name):
@@ -59,7 +60,7 @@ def pack(self, model): # pylint:disable=arguments-differ
fastai_module = _import_fastai_module()

if not isinstance(model, fastai_module.basic_train.Learner):
raise ValueError(
raise TypeError(
"Expect `model` argument to be `fastai.basic_train.Learner` instance"
)

@@ -33,6 +33,12 @@ class KerasModelArtifact(BentoServiceArtifact):
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
"""

def __init__(
@@ -133,11 +139,11 @@ def pack(self, data): # pylint:disable=arguments-differ
import keras

if not isinstance(model, keras.engine.network.Network):
raise ValueError(error_msg)
raise TypeError(error_msg)
else:
self._keras_module_name = keras.__name__
except ImportError:
raise ValueError(error_msg)
raise TypeError(error_msg)

self.bind_keras_backend_session()
model._make_predict_function()
@@ -30,7 +30,7 @@ class PickleArtifact(BentoServiceArtifact):
Args:
name (str): Name for the artifact
pickle_module (module|str): The python module will be used for pickle
and unpickle artifact
and unpickle artifact, default pickle module in BentoML is `dill`
pickle_extension (str): The extension format for pickled file.
"""

@@ -25,6 +25,14 @@
class PytorchModelArtifact(BentoServiceArtifact):
"""
Abstraction for saving/loading objects with torch.save and torch.load
Args:
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
"""

def __init__(self, name, file_extension=".pt"):
@@ -45,7 +45,10 @@ class SklearnModelArtifact(BentoServiceArtifact):
Args:
name (str): Name for the artifact
pickle_extension (str): The extension format for pickled file.
pickle_extension (str): The extension format for pickled file
Raises:
ImportError: sklean package is required for SklearnModelArtifact
"""

def __init__(self, name, pickle_extension=".pkl"):
@@ -49,42 +49,43 @@ class TensorflowSavedModelArtifact(BentoServiceArtifact):
"""
Abstraction for saving/loading Tensorflow model in tf.saved_model format
Example usage:
```python
import tensorflow as tf
class Adder(tf.Module):
@tf.function(input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])
def add(self, x):
return x + x + 1.
to_export = Adder()
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)
return prediction.numpy()
svc = TfModelService()
Args:
name (string): name of the artifact
# Option 1: pack directly with Tensorflow trackable object
svc.pack('model', to_export)
Raises:
ImportError: tensorflow package is required for TensorflowSavedModelArtifact
# Option 2: save to file path then pack
tf.saved_model.save(to_export, '/tmp/adder/1')
svc.pack('model', '/tmp/adder/1')
```
Example usage:
Args:
name (string): name of the artifact
>>> import tensorflow as tf
>>> class Adder(tf.Module):
>>> @tf.function(input_signature=[tf.TensorSpec(shape=None, dtype=tf.float32)])
>>> def add(self, x):
>>> return x + x + 1.
>>> to_export = Adder()
>>>
>>> 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)
>>> return prediction.numpy()
>>>
>>> svc = TfModelService()
>>>
>>> # Option 1: pack directly with Tensorflow trackable object
>>> svc.pack('model', to_export)
>>>
>>> # Option 2: save to file path then pack
>>> tf.saved_model.save(to_export, '/tmp/adder/1')
>>> svc.pack('model', '/tmp/adder/1')
"""

def _saved_model_path(self, base_path):
@@ -30,6 +30,8 @@ class XgboostModelArtifact(BentoServiceArtifact):
Raises:
ImportError: xgboost package is required for using XgboostModelArtifact
TypeError: invalid argument type, model being packed must be instance of
xgboost.core.Booster
"""

def __init__(self, name, model_extension=".model"):
@@ -63,7 +65,7 @@ def __init__(self, spec, model):
raise ImportError("xgboost package is required to use XgboostModelArtifact")

if not isinstance(model, xgb.core.Booster):
raise ValueError(
raise TypeError(
"Expect `model` argument to be a `xgboost.core.Booster` instance"
)

@@ -17,6 +17,10 @@ FastaiModelArtifact
+++++++++++++++++++
.. autoclass:: bentoml.artifact.FastaiModelArtifact

TensorflowSavedModelArtifact
++++++++++++++++++++++++++++
.. autoclass:: bentoml.artifact.TensorflowSavedModelArtifact

XgboostModelArtifact
++++++++++++++++++++
.. autoclass:: bentoml.artifact.XgboostModelArtifact
@@ -15,7 +15,7 @@ python3 -m http.server --directory $GIT_ROOT/built-docs &
echo "Open browser..."
open -a "Google Chrome" http://0.0.0.0:8000/

fswatch -o $GIT_ROOT/docs | while read; \
fswatch -o $GIT_ROOT | while read; \
do \
echo "Change detected, rebuilding docs..."
sphinx-build -E $GIT_ROOT/docs $GIT_ROOT/built-docs

0 comments on commit a5c2735

Please sign in to comment.
You can’t perform that action at this time.