Any solution to this?--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/a742fb11-ad01-4009-a0c4-81f1cc32af93%40tensorflow.org.
• • • • | Paige Bailey Product Manager (TensorFlow) @DynamicWebPaige
|
class MyModel(tf.Module): def __init__(self, vocabulary_path): super(MyModel, self).__init__() initializer = tf.lookup.TextFileInitializer( vocabulary_path, tf.string, tf.lookup.TextFileIndex.WHOLE_LINE, tf.int64, tf.lookup.TextFileIndex.LINE_NUMBER) self.table = tf.lookup.StaticVocabularyTable(initializer, num_oov_buckets=1) @tf.function(input_signature=[tf.TensorSpec([None], dtype=tf.string)]) def predict(self, x): print("Tracing predict") val = self.table.lookup(x) return {"prediction": val} @tf.function(input_signature=[tf.TensorSpec([None], dtype=tf.string)]) def classify(self, x): print("Tracing classify") val = self.table.lookup(x) return {"classification": val + 100} vocabulary_path = "/tmp/vocab.txt"with open(vocabulary_path, "w") as vocabulary_file: vocabulary_file.write("a\nb\nc\n") model = MyModel(vocabulary_path)
export_dir = "my-sick-servable/00000123"if os.path.exists(export_dir): shutil.rmtree(export_dir) input_spec = tf.TensorSpec([None], dtype=tf.string)pred_fn = model.predict.get_concrete_function(input_spec)clf_fn = model.classify.get_concrete_function(input_spec)
# Post 2.1.0?options = tf.saved_model.SaveOptions(function_aliases={ 'classify': model.classify, 'predict': model.predict,}) signatures = { tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY: pred_fn, "classify": clf_fn, "predict": pred_fn,}
tf.saved_model.save(model, export_dir, signatures=signatures, options=options)
import json
import requests
headers = {"content-type": "application/json"}
def classify(instances):
data = json.dumps({"signature_name": "classify", "instances": instances})
return requests.post('http://localhost:8501/v1/models/guh:predict', data=data)
def predict(instances):
data = json.dumps({"signature_name": "predict", "instances": instances})
return requests.post('http://localhost:8501/v1/models/guh:predict', data=data)
print(f"'classify' signature: {json.loads(classify(['a', 'b']).content)}")
print(f"'predict' signature: {json.loads(predict(['a', 'b']).content)}")
# 'classify' signature: {'predictions': [100, 101]}
# 'predict' signature: {'predictions': [0, 1]}
import tensorflow as tfimport osimport shutilimport tensorflow as tf
class ServingExporter(tf.Module): """ A wrapper around a keras model that provides a compatible signature for tenssorflow serving """ def __init__(self, model, label_vocabulary): super(MyModel, self).__init__() self.classes = tf.constant(label_vocabulary) self.model = model
@tf.function(input_signature=[tf.TensorSpec(None, dtype=tf.string)]) def predict(self, x, training=False): print("Tracing predict") predictions = self.model(x, training) return {"scores": val, "classes": self.classes}
model = # a trained keras model compat_model = ServingExporter(model, ["foo", "bar", "qux", "baz"])
export_dir = "my-sick-servable/00000123"if os.path.exists(export_dir): shutil.rmtree(export_dir) input_spec = tf.TensorSpec([None], dtype=tf.string)clf_fn = compat_model.classify.get_concrete_function(input_spec)
# Post 2.1.0?options = tf.saved_model.SaveOptions(function_aliases={ 'predict': compat_model.predict,}) signatures = { tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY: pred_fn,}
tf.saved_model.save(compat_model, export_dir, signatures=signatures, options=options)
Adding +Allen Lavoie and +Kathy Wu to help answer.
On Fri, Feb 7, 2020 at 9:17 AM Taylor Smith <tgsmit...@gmail.com> wrote:
Any solution to this?--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/a742fb11-ad01-4009-a0c4-81f1cc32af93%40tensorflow.org.
--
On Saturday, February 8, 2020 at 3:38:25 PM UTC-6, Paige Bailey wrote:Adding +Allen Lavoie and +Kathy Wu to help answer.On Fri, Feb 7, 2020 at 9:17 AM Taylor Smith <tgsmit...@gmail.com> wrote:Any solution to this?--
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dis...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/a742fb11-ad01-4009-a0c4-81f1cc32af93%40tensorflow.org.
--
•
•
•
•
Paige Bailey
Product Manager (TensorFlow)
@DynamicWebPaige
![]()
You received this message because you are subscribed to the Google Groups "Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/d06979de-24e3-4bda-9732-f5c12c969723%40tensorflow.org.