Created
December 21, 2017 21:15
-
-
Save gauravkaila/cebac0023f81deef0068ddac50cd2089 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
@abatkins much thanks for this, i was stuck on this for a long time. 👍
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
@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
@yavac
I'm not sure why it's written this way...maybe it worked in older version of tensorflow. In tensorflow 1.10, make_tensor_proto expects a list or numpy array. In the example above, the file location is being passed in a set. I converted image to numpy array like using: scipy.misc.imread(FLAGS.input_image)
I then got another error, because the shape of the array wasn't passed. The full solution is to replace
make_tensor_proto(FLAGS.input_image)
withmake_tensor_proto(scipy.misc.imread(FLAGS.input_image), shape=[1] + list(img.shape))
https://www.tensorflow.org/api_docs/python/tf/make_tensor_proto
This all took me a long time to figure out. Hope it saves the next person some time.