class Tensorflow2Classifier(bentoml.BentoService): @bentoml.api(input=TfTensorInput(), batch=True) def predict1(self, tensor): return self.artifacts.model1(tensor) @bentoml.api(input=TfTensorInput(), batch=True) def predict2(self, tensor): return self.artifacts.model2(tensor) @bentoml.api(input=JsonInput(), batch=True) def predict3(self, jsons): import tensorflow as tf tensor = tf.ragged.constant(jsons, dtype=tf.float64) return self.artifacts.model3(tensor)
def test_tf_tensor_handle_batch_request(): ''' ref: https://www.tensorflow.org/tfx/serving/api_rest#request_format_2 ''' from bentoml.adapters import TfTensorInput input_adapter = TfTensorInput() request = Mock() for input_data, except_result in zip(TEST_CASES, EXPECTED_RESULTS): request.data = json.dumps(input_data).encode('utf-8') responses = input_adapter.handle_batch_request([request] * 3, lambda i: i) for response in responses: prediction = json.loads(response.data) assert_eq_or_both_nan(except_result, prediction)
def test_tf_tensor_handle_request(): ''' ref: https://www.tensorflow.org/tfx/serving/api_rest#request_format_2 ''' from bentoml.adapters import TfTensorInput request = Mock() request.headers = {} request.content_type = 'application/json' input_adapter = TfTensorInput() for input_data, except_result in zip(TEST_CASES, EXPECTED_RESULTS): request.data = json.dumps(input_data).encode('utf-8') response = input_adapter.handle_request(request, lambda i: i) prediction = json.loads(response.get_data()) assert_eq_or_both_nan(except_result, prediction)
class Tensorflow1Classifier(bentoml.BentoService): @bentoml.api(input=TfTensorInput(), batch=True) def predict(self, tensor): import tensorflow as tf tf.enable_eager_execution() pred_func = self.artifacts.model.signatures['serving_default'] return pred_func(tensor)['prediction']
def test_tf_tensor_handle_batch_request(test_cases): ''' ref: https://www.tensorflow.org/tfx/serving/api_rest#request_format_2 ''' from bentoml.adapters import TfTensorInput from bentoml.marshal.utils import SimpleRequest input_adapter = TfTensorInput() request = MagicMock(spec=flask.Request) input_data, headers, except_result = test_cases request.get_data.return_value = json.dumps(input_data).encode('utf-8') request.headers = headers responses = input_adapter.handle_batch_request( [SimpleRequest.from_flask_request(request)] * 3, lambda i: i) for response in responses: prediction = json.loads(response.data) assert_eq_or_both_nan(except_result, prediction)
def test_tf_tensor_handle_request(test_cases): ''' ref: https://www.tensorflow.org/tfx/serving/api_rest#request_format_2 ''' from bentoml.adapters import TfTensorInput request = MagicMock(spec=flask.Request) request.headers = {} request.content_type = 'application/json' input_adapter = TfTensorInput() input_data, headers, except_result = test_cases request.get_data.return_value = json.dumps(input_data).encode('utf-8') request.headers = headers response = input_adapter.handle_request(request, lambda i: i) prediction = json.loads(response.get_data()) assert_eq_or_both_nan(except_result, prediction)
def test_tf_tensor_handle_request(make_api, test_cases): ''' ref: https://www.tensorflow.org/tfx/serving/api_rest#request_format_2 ''' from bentoml.adapters import TfTensorInput api = make_api(input_adapter=TfTensorInput(), user_func=lambda i: i) input_data, headers, except_result = test_cases body = json.dumps(input_data).encode('utf-8') request = HTTPRequest(headers=headers, body=body) response = tuple(api.handle_batch_request([request]))[0] prediction = json.loads(response.body) assert_eq_or_both_nan(except_result, prediction)
class Tensorflow2Classifier(bentoml.BentoService): @bentoml.api(input=TfTensorInput()) def predict(self, tensor): return self.artifacts.model(tensor)
class FashionMnistTensorflow(bentoml.BentoService): @bentoml.api(input=TfTensorInput(), batch=True) def predict(self, inputs): outputs = self.artifacts.model.predict_image(inputs) output_classes = tf.math.argmax(outputs, axis=1) return [FASHION_MNIST_CLASSES[c] for c in output_classes]