Skip to content

Instantly share code, notes, and snippets.

@zldrobit
Last active December 25, 2018 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zldrobit/2854c39a2ca4063239f75be2117f3bf0 to your computer and use it in GitHub Desktop.
Save zldrobit/2854c39a2ca4063239f75be2117f3bf0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 22 15:12:17 2018
@author: robitfang
"""
from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
from tensorflow.python.framework import tensor_util
import tensorflow as tf
import cv2
import numpy as np
host = '1.2.3.4'
port = 8500
s = open('/Users/robitfang/Downloads/blabla.jpg', 'rb').read()
s1 = open('/Users/robitfang/Documents/foooooo.png', 'rb').read()
print("Infering.")
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = 'method_name'
request.model_spec.signature_name = 'sig_name'
#request.inputs['input'].CopyFrom(
# tf.contrib.util.make_tensor_proto(
# [image.astype(dtype=np.float32), image2.astype(dtype=np.float32)],
# shape=[2, -1, -1, 3]))
request.inputs['images'].CopyFrom(
tf.contrib.util.make_tensor_proto([s, s1]))
result_future = stub.Predict(request, 30.)
result0 = result_future.outputs['scores']
result = tensor_util.MakeNdarray(result0)
from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
from tensorflow.python.framework import tensor_util
import tensorflow as tf
import cv2
import numpy as np
host = '1.2.3.4'
port = 8500
image = cv2.imread('image.jpg')
original_h, original_w, _ = image.shape
original_image = image
height, width = 224, 224
model_name = 'alpha'
image = cv2.resize(image, (height, width), interpolation=cv2.INTER_AREA)
image = np.array(image)
image = image[:, :, :3]
print("Infering.")
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = model_name
request.model_spec.signature_name = 'signature'
request.inputs['input'].CopyFrom(
tf.contrib.util.make_tensor_proto(image.astype(dtype=np.float32), shape=[1, height, width, 3]))
result_future = stub.Predict(request, 30.)
result0 = result_future.outputs['output']
result = tensor_util.MakeNdarray(result0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment