Beispiel #1
0
def get_prediction(image,
                   server_host='127.0.0.1',
                   server_port=8080,
                   deployment_name="server"):
    """
  Retrieve a prediction from a TensorFlow model server

  :param image:       a MNIST image represented as a 1x784 array
  :param server_host: the address of the Seldon server
  :param server_port: the port used by the server
  :param deployment_name: the name of the deployment
  :return 0:          the integer predicted in the MNIST image
  """

    try:
        # build request
        chosen = 0
        data = image[chosen].reshape(784)
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=data.shape, values=data))

        # retrieve results
        request = prediction_pb2.SeldonMessage(data=datadef)
        logging.info("connecting to:%s:%i", server_host, server_port)
        channel = grpc.insecure_channel(server_host + ":" + str(server_port))
        stub = prediction_pb2_grpc.SeldonStub(channel)
        metadata = [('seldon', deployment_name)]
        response = stub.Predict(request=request, metadata=metadata)
    except IOError as e:
        # server connection failed
        logging.error("Could Not Connect to Server: %s", str(e))
    return response.data.tensor.values
Beispiel #2
0
 def get_prediction(self):
     datadef = prediction_pb2.DefaultData(
         names=["a", "b"],
         tensor=prediction_pb2.Tensor(
             shape=[3, 2], values=[1.0, 1.0, 2.0, 3.0, 4.0, 5.0]))
     request = prediction_pb2.SeldonMessage(data=datadef)
     start_time = time.time()
     try:
         if self.oauth_enabled:
             metadata = [('oauth_token', self.access_token)]
             response = self.stub.Predict(request=request,
                                          metadata=metadata)
         else:
             response = self.stub.Predict(request=request)
     except Exception as e:
         total_time = int((time.time() - start_time) * 1000)
         print(e)
         events.request_failure.fire(request_type="grpc",
                                     name=HOST,
                                     response_time=total_time,
                                     exception=e)
     else:
         total_time = int((time.time() - start_time) * 1000)
         events.request_success.fire(request_type="grpc",
                                     name=HOST,
                                     response_time=total_time,
                                     response_length=0)
Beispiel #3
0
def grpc_request(deploymentName, data):
    datadef = prediction_pb2.DefaultData(names=["a", "b"],
                                         tensor=prediction_pb2.Tensor(
                                             shape=[1, 784], values=data))
    request = prediction_pb2.SeldonMessage(data=datadef)
    channel = grpc.insecure_channel(AMBASSADOR_API_IP)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    metadata = [('seldon', deploymentName)]
    response = stub.Predict(request=request, metadata=metadata)
    return response
Beispiel #4
0
def gen_GRPC_request(batch, features, tensor=True):
    if tensor:
        datadef = prediction_pb2.DefaultData(
            names=features,
            tensor=prediction_pb2.Tensor(shape=batch.shape,
                                         values=batch.ravel().tolist()))
    else:
        datadef = prediction_pb2.DefaultData(
            names=features, ndarray=array_to_list_value(batch))
    request = prediction_pb2.SeldonMessage(data=datadef)
    return request
Beispiel #5
0
def gen_GRPC_request(batch, features, tensor=True):
    if tensor:
        datadef = prediction_pb2.DefaultData(
            names=features,
            tensor=prediction_pb2.Tensor(shape=batch.shape,
                                         values=batch.ravel().tolist()))
    else:
        v = '{"names":' + json.dumps(features) + ',"ndarray":' + json.dumps(
            batch.tolist()) + '}'
        datadef = json_format.Parse(v, prediction_pb2.DefaultData())
    request = prediction_pb2.SeldonMessage(data=datadef)
    return request
Beispiel #6
0
def grpc_request_ambassador(deploymentName,
                            endpoint="localhost:8004",
                            data_size=5,
                            rows=1):
    shape, arr = create_random_data(data_size, rows)
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=shape, values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    metadata = [('seldon', deploymentName)]
    response = stub.Predict(request=request, metadata=metadata)
    print(response)
Beispiel #7
0
def array_to_grpc_datadef(array, names, data_type):
    if data_type == "tensor":
        datadef = prediction_pb2.DefaultData(
            names=names,
            tensor=prediction_pb2.Tensor(shape=array.shape,
                                         values=array.ravel().tolist()))
    elif data_type == "ndarray":
        datadef = prediction_pb2.DefaultData(
            names=names, ndarray=array_to_list_value(array))
    else:
        datadef = prediction_pb2.DefaultData(
            names=names, ndarray=array_to_list_value(array))

    return datadef
Beispiel #8
0
def grpc_request_api_gateway(oauth_key,
                             oauth_secret,
                             rest_endpoint="localhost:8002",
                             grpc_endpoint="localhost:8003",
                             data_size=5,
                             rows=1):
    token = get_token(oauth_key, oauth_secret, rest_endpoint)
    shape, arr = create_random_data(data_size, rows)
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=shape, values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    channel = grpc.insecure_channel(grpc_endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    metadata = [('oauth_token', token)]
    response = stub.Predict(request=request, metadata=metadata)
    print(response)
def test_proto_ok():
    user_object = UserObject()
    app = SeldonModelGRPC(user_object)
    arr = np.array([1, 2])
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    resp = app.Predict(request, None)
    jStr = json_format.MessageToJson(resp)
    j = json.loads(jStr)
    print(j)
    assert j["meta"]["tags"] == {"mytag": 1}
    # add default type
    j["meta"]["metrics"][0]["type"] = "COUNTER"
    assert j["meta"]["metrics"] == user_object.metrics()
    assert j["data"]["tensor"]["shape"] == [2, 1]
    assert j["data"]["tensor"]["values"] == [1, 2]
Beispiel #10
0
def gen_grpc_response(predictions, target_names, request):
    if len(request.request.tensor.values) > 0:
        # We will return a tensor in the response

        response = prediction_pb2.ResponseDef(
            response=prediction_pb2.DefaultDataDef(
                names=target_names,
                tensor=prediction_pb2.Tensor(
                    shape=predictions.shape,
                    values=predictions.ravel().tolist())))
    else:
        # We will return a ndarray in the response

        response = prediction_pb2.ResponseDef(
            response=prediction_pb2.DefaultDataDef(
                names=target_names, ndarray=predictions.tolist()))

    return response
 def getPrediction(self):
     batch_xs, batch_ys = self.mnist.train.next_batch(1)
     data = batch_xs[0].reshape((1, 784))
     data = np.around(data, decimals=2)
     features = ["X" + str(i + 1) for i in range(0, self.data_size)]
     #request = {"data":{"names":features,"ndarray":data.tolist()}}
     datadef = prediction_pb2.DefaultData(names=features,
                                          tensor=prediction_pb2.Tensor(
                                              shape=[1, 784],
                                              values=data.flatten()))
     request = prediction_pb2.SeldonMessage(data=datadef)
     start_time = time.time()
     try:
         response = self.stub.Predict(request=request)
         if self.send_feedback == 1:
             response = MessageToJson(response)
             response = json.loads(response)
             route = response.get("meta").get("routing").get("eg-router")
             proba = response["data"]["tensor"]["values"]
             predicted = proba.index(max(proba))
             correct = np.argmax(batch_ys[0])
             j = json.loads(r.content)
             if predicted == correct:
                 self.sendFeedback(request, j, 1.0)
                 print("Correct!")
             else:
                 self.sendFeedback(request, j, 0.0)
                 print("Incorrect!")
     except Exception as e:
         total_time = int((time.time() - start_time) * 1000)
         print(e)
         events.request_failure.fire(request_type="grpc",
                                     name=HOST,
                                     response_time=total_time,
                                     exception=e)
     else:
         total_time = int((time.time() - start_time) * 1000)
         events.request_success.fire(request_type="grpc",
                                     name=HOST,
                                     response_time=total_time,
                                     response_length=0)
Beispiel #12
0
def grpc_request_ambassador(deploymentName,
                            namespace,
                            endpoint="localhost:8004",
                            data_size=5,
                            rows=1,
                            data=None):
    if data is None:
        shape, arr = create_random_data(data_size, rows)
    else:
        shape = data.shape
        arr = data.flatten()
    datadef = prediction_pb2.DefaultData(
        tensor=prediction_pb2.Tensor(shape=shape, values=arr))
    request = prediction_pb2.SeldonMessage(data=datadef)
    channel = grpc.insecure_channel(endpoint)
    stub = prediction_pb2_grpc.SeldonStub(channel)
    if namespace is None:
        metadata = [('seldon', deploymentName)]
    else:
        metadata = [('seldon', deploymentName), ('namespace', namespace)]
    response = stub.Predict(request=request, metadata=metadata)
    return response
Beispiel #13
0
def get_prediction(image,
                   server_host='127.0.0.1',
                   server_port=8080,
                   deployment_name="server",
                   timeout=10.0):
    """
    Retrieve a prediction from a TensorFlow model server

    :param image:       a MNIST image represented as a 1x784 array
    :param server_host: the address of the Seldon server
    :param server_port: the port used by the server
    :param deployment_name: the name of the deployment
    :param timeout:     the amount of time to wait for a prediction to complete
    :return 0:          the integer predicted in the MNIST image
    :return 1:          the confidence scores for all classes
    :return 2:          the version number of the model handling the request
    """

    try:
        # build request
        chosen = 0
        data = image[chosen].reshape(784)
        datadef = prediction_pb2.DefaultData(
            tensor=prediction_pb2.Tensor(shape=data.shape, values=data))

        # retrieve results
        request = prediction_pb2.SeldonMessage(data=datadef)
        print("connecting to:%s:%i" % (server_host, server_port))
        channel = grpc.insecure_channel(server_host + ":" + str(server_port))
        stub = prediction_pb2_grpc.SeldonStub(channel)
        metadata = [('seldon', deployment_name)]
        response = stub.Predict(request=request, metadata=metadata)
    except Exception as e:
        # server connection failed
        print("Could Not Connect to Server: " + str(e))
    return response.data.tensor.values