Skip to content

Instantly share code, notes, and snippets.

@gauravkaila
Created December 21, 2017 21:15
Show Gist options
  • Save gauravkaila/cebac0023f81deef0068ddac50cd2089 to your computer and use it in GitHub Desktop.
Save gauravkaila/cebac0023f81deef0068ddac50cd2089 to your computer and use it in GitHub Desktop.
# Create stub
host, port = FLAGS.server.split(':')
channel = implementations.insecure_channel(host, int(port))
stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
# Create prediction request object
request = predict_pb2.PredictRequest()
# Specify model name (must be the same as when the TensorFlow serving serving was started)
request.model_spec.name = 'obj_det'
# Initalize prediction
# Specify signature name (should be the same as specified when exporting model)
request.model_spec.signature_name = "detection_signature"
request.inputs['inputs'].CopyFrom(
tf.contrib.util.make_tensor_proto({FLAGS.input_image}))
# Call the prediction server
result = stub.Predict(request, 10.0) # 10 secs timeout
# Plot boxes on the input image
category_index = load_label_map(FLAGS.path_to_labels)
boxes = result.outputs['detection_boxes'].float_val
classes = result.outputs['detection_classes'].float_val
scores = result.outputs['detection_scores'].float_val
image_vis = vis_util.visualize_boxes_and_labels_on_image_array(
FLAGS.input_image,
np.reshape(boxes,[100,4]),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
# Save inference to disk
scipy.misc.imsave('%s.jpg'%(FLAGS.input_image), image_vis)
@osteinnes
Copy link

osteinnes commented Feb 22, 2019

I'm getting failed prediction error. Any ideas?

python object_detection_client.py -server=localhost:8500 -input_image=/home/osteinnes/Fy01_Fv112_hp01_f1_m05917.jpg -path_to_labels=/home/osteinnes/programming/rcnn-p18-object-detection/label.pbtxt
/home/osteinnes/.conda/envs/doNotDelete/lib/python3.6/site-packages/tensorflow_serving/apis/prediction_service_pb2.py:131: DeprecationWarning: beta_create_PredictionService_stub() method is deprecated. This method will be removed in near future versions of TF Serving. Please switch to GA gRPC API in prediction_service_pb2_grpc.
  'prediction_service_pb2_grpc.', DeprecationWarning)
object_detection_client.py:31: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.
  img = scipy.misc.imread(FLAGS.input_image)
/home/osteinnes/.conda/envs/doNotDelete/lib/python3.6/site-packages/scipy/misc/pilutil.py:165: DeprecationWarning: `fromimage` is deprecated!
`fromimage` is deprecated in SciPy 1.0.0. and will be removed in 1.2.0.
Use ``np.asarray(im)`` instead.
  return fromimage(im, flatten=flatten, mode=mode)
/home/osteinnes/.conda/envs/doNotDelete/lib/python3.6/site-packages/tensorflow/python/util/tf_inspect.py:75: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
  return _inspect.getargspec(target)
Traceback (most recent call last):
  File "object_detection_client.py", line 35, in <module>
    result = stub.Predict(request, 10.0)  # 10 secs timeout
  File "/home/osteinnes/.conda/envs/doNotDelete/lib/python3.6/site-packages/grpc/_channel.py", line 550, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/osteinnes/.conda/envs/doNotDelete/lib/python3.6/site-packages/grpc/_channel.py", line 467, in _end_unary_response_blocking
    raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
        status = StatusCode.FAILED_PRECONDITION
        details = "Serving signature key "model" not found."
        debug_error_string = "{"created":"@1550853885.188677522","description":"Error received from peer","file":"src/core/lib/surface/call.cc","file_line":1036,"grpc_message":"Serving signature key "model" not found.","grpc_status":9}"

EDIT: Figured it out. Even though I specified the model signature when exporting the re-trained model, it turned out that it was saved as "".
Inspected this by the following GET-request.

GET http://host:port/v1/models/model/metadata

@louiselmps
Copy link

@abatkins what are the necessary imports for this object_detection_client file to make this work. Do you know??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment