def test_convert_predict_response_regression(self): """Test converting a PredictResponse to a RegressionResponse.""" predict = predict_pb2.PredictResponse() output = predict.outputs['scores'] dim = output.tensor_shape.dim.add() dim.size = 2 output.float_val.extend([0.1, 0.2]) bundle = inference_utils.ServingBundle('', '', 'regression', '', '', True, '', 'scores') converted = common_utils.convert_predict_response(predict, bundle) self.assertAlmostEqual(0.1, converted.result.regressions[0].value) self.assertAlmostEqual(0.2, converted.result.regressions[1].value)
def call_servo(examples, serving_bundle): """Send an RPC request to the Servomatic prediction service. Args: examples: A list of examples that matches the model spec. serving_bundle: A `ServingBundle` object that contains the information to make the serving request. Returns: A ClassificationResponse or RegressionResponse proto. """ parsed_url = urlparse('http://' + serving_bundle.inference_address) channel = implementations.insecure_channel(parsed_url.hostname, parsed_url.port) stub = prediction_service_pb2.beta_create_PredictionService_stub(channel) if serving_bundle.use_predict: request = predict_pb2.PredictRequest() elif serving_bundle.model_type == 'classification': request = classification_pb2.ClassificationRequest() else: request = regression_pb2.RegressionRequest() request.model_spec.name = serving_bundle.model_name if serving_bundle.model_version is not None: request.model_spec.version.value = serving_bundle.model_version if serving_bundle.signature is not None: request.model_spec.signature_name = serving_bundle.signature if serving_bundle.use_predict: # tf.compat.v1 API used here to convert tf.example into proto. This # utility file is bundled in the witwidget pip package which has a dep # on TensorFlow. request.inputs[serving_bundle.predict_input_tensor].CopyFrom( tf.compat.v1.make_tensor_proto( values=[ex.SerializeToString() for ex in examples], dtype=types_pb2.DT_STRING)) else: request.input.example_list.examples.extend(examples) if serving_bundle.use_predict: return common_utils.convert_predict_response( stub.Predict(request, 30.0), serving_bundle) # 30 secs timeout elif serving_bundle.model_type == 'classification': return stub.Classify(request, 30.0) # 30 secs timeout else: return stub.Regress(request, 30.0) # 30 secs timeout
def test_convert_predict_response_classification(self): """Test converting a PredictResponse to a ClassificationResponse.""" predict = predict_pb2.PredictResponse() output = predict.outputs['probabilities'] dim = output.tensor_shape.dim.add() dim.size = 3 dim = output.tensor_shape.dim.add() dim.size = 2 output.float_val.extend([1., 0., .9, .1, .8, .2]) bundle = inference_utils.ServingBundle('', '', 'classification', '', '', True, '', 'probabilities') converted = common_utils.convert_predict_response(predict, bundle) self.assertEqual("0", converted.result.classifications[0].classes[0].label) self.assertAlmostEqual( 1, converted.result.classifications[0].classes[0].score) self.assertEqual("1", converted.result.classifications[0].classes[1].label) self.assertAlmostEqual( 0, converted.result.classifications[0].classes[1].score) self.assertEqual("0", converted.result.classifications[1].classes[0].label) self.assertAlmostEqual( .9, converted.result.classifications[1].classes[0].score) self.assertEqual("1", converted.result.classifications[1].classes[1].label) self.assertAlmostEqual( .1, converted.result.classifications[1].classes[1].score) self.assertEqual("0", converted.result.classifications[2].classes[0].label) self.assertAlmostEqual( .8, converted.result.classifications[2].classes[0].score) self.assertEqual("1", converted.result.classifications[2].classes[1].label) self.assertAlmostEqual( .2, converted.result.classifications[2].classes[1].score)