Run object_detection prediction on cloudML
With the latest changes for exporting a SavedModel d9d10fbbb938534af72f405983cabb85258ac5f3 I tried to run predictions on Google CloudML.
When I uploaded my model here https://console.cloud.google.com/mlengine/models I got the following error Model validation failed: SavedModel must contain exactly one metagraph with tag: serve which I was able to fix here: #1810
After this fix I ran into another error: Model validation failed: Outer dimension for SignatureDef outputs must be unknown, outer dimension of 'detection_scores:0' is 1
Printing out the detection_signature (https://github.com/tensorflow/models/blob/master/object_detection/exporter.py#L281) it looks like this:
inputs {
key: "inputs"
value {
name: "ToFloat:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
dim {
size: -1
}
dim {
size: -1
}
dim {
size: 3
}
}
}
}
outputs {
key: "detection_boxes"
value {
name: "detection_boxes:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
dim {
size: 300
}
dim {
size: 4
}
}
}
}
outputs {
key: "detection_classes"
value {
name: "detection_classes:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
dim {
size: 300
}
}
}
}
outputs {
key: "detection_scores"
value {
name: "detection_scores:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
dim {
size: 300
}
}
}
}
outputs {
key: "num_detections"
value {
name: "num_detections:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
}
}
}
method_name: "tensorflow/serving/predict"
I am just getting started with tensorflow... it looks to me like the dimension in the shapes should be somehow removed?
I have the same issue. I tried to reshape the output tensors but it didn't work. Did you have any progress?
same problem for me
@derekjchow Do you have any idea how we can fix that? Thanks!!!
I changed my outputs definition:
num_detections = tf.placeholder(dtype=tf.float32, shape=[None], name='num_detections')
boxes = tf.placeholder(dtype=tf.float32, shape=[None, 300, 4], name='detection_boxes')
scores = tf.placeholder(dtype=tf.float32, shape=[None, 300], name='detection_scores')
classes = tf.placeholder(dtype=tf.float32, shape=[None, 300], name='detection_classes')
tensor_info_outputs = {
'detection_boxes': tf.saved_model.utils.build_tensor_info(boxes),
'detection_scores': tf.saved_model.utils.build_tensor_info(scores),
'detection_classes': tf.saved_model.utils.build_tensor_info(classes),
'num_detections': tf.saved_model.utils.build_tensor_info(num_detections)
}
And the error changes to:
"error": {
"code": 3,
"message": "Bad model detected with error: \"Error loading the model: Could not load model. \""
}
Just FYI, this seems to be a google cloud ml issue. Something changed on their side in the last few days, because models that could previously be uploaded can't be deployed anymore either.
@asimshankar any pointers here?
I'm from the Cloud ML Engine team. We apologize for the inconvenience and are investigating.
I am from Google Cloud ML engine team. The current object detection example only supports reading one image at a time when doing inference. However, Cloud ML engine prediction service requires the inference graph be able to accept arbitrary number of input instances.The shape of the input tensor(s) must have None as its first dimension. We are actively working on adding that. The blog post and the code in github will be updated in next a few days.
@jlewi @yixinshi the problem isn't only limited to object detection. One of my models which is a text based model with a very simple input shape of (None, 100) results in the following error,
response: {u'error': {u'message': u"Model validation failed: Outer dimension for SignatureDef outputs must be unknown, outer dimension of 'Reshape:0' is 1", u'code': 3}
The thing to note is that this same model worked last week - we're unable to even deploy models to cloudml because of this.
Hi, I am from Google Cloud Support. The outer dimension of the output should also be None, so the prediction service can produce one output row for each input row. @viksit, could you share the signatures?
@LucasAntevere the second errors seems to be related to another issue. Could you try starting TensorFlow Model Server locally using your model. You don't need to implement the client, it is just to test whether the TensorFlow Model Server can use the model.
@ilnar here you go,
signature_def['predict']:
The given SavedModel SignatureDef contains the following input(s):
inputs['inputs'] tensor_info:
dtype: DT_STRING
shape: (-1, 15, 100)
name: input_2:0
The given SavedModel SignatureDef contains the following output(s):
outputs['classes'] tensor_info:
dtype: DT_STRING
shape: (1, 10)
name: Reshape:0
outputs['scores'] tensor_info:
dtype: DT_FLOAT
shape: (-1, 10)
name: dense_3/Softmax:0
Method name is: tensorflow/serving/predict
To clarify whats working and whats not.
a) Two weeks ago, I uploaded the model above (lets call it model A) to cloud ML. It uploaded fine and predictions worked. b) This week, I trained the same model type on a different dataset and tried to upload it. It failed to upload with the error I mentioned earlier. c) I figured I was doing something wrong, so I tried to re-upload model A (same saved_model file, just as a different version). d) Step c failed. So the model that was uploaded earlier (Model A, version 1) still serves. But the same model can't be uploaded again with a new version. That is, Model A version 2 won't upload. It's the exact same file though. e) Other models of the same type but different datasets can't be uploaded either.
PS, I realize this is a cloud ml issue. Is there a better forum to continue this discussion @ilnar @yixinshi @jlewi ?
Thank you for the update, @viksit. The outer dimension of classes (Reshape:0) is indeed not equal to None (aka unknown or -1), that is why the model check fails. There was a change in the service that introduced stricter signature checks. If we request prediction for 5 instances, the inputs will have shape of (5, 15, 100). As we provided 5 instances we expect to get 5 predictions, but we will get scores of shape (5, 10) and classes of shape (1, 10). So we don't know how to extend the classes tensor to 5 predictions. This is the logic behind that change.
There are several ways to get support and if you decide to open a support case, please mention my name, so the case is routed to me.
@ilnar thanks for the information. I think I understand although I'm still trying to wrap my head around it. What would you say is the change needed from our side to make it compatible to the new model check paradigm?
Edit: The classes tensor is a custom tensor that is constructed like this : https://github.com/tensorflow/serving/issues/450
After seeing that error, I added one more line to the code that did,
# class_list = ["a", "B", ...] # len 10
class_tensor = tf.constant(class_list)
indices = tf.constant(list(range(0,len(class_list))))
table = tf.contrib.lookup.index_to_string_table_from_tensor(class_tensor)
classes = table.lookup(tf.to_int64(indices))
reshaped_classes = tf.reshape(classes, [1, num_classes]) ##<-- new addition after issue #450
signature = predict_signature_def(inputs={"input": inputs},
outputs={
"scores": outputs,
"classes": reshaped_classes})
From what I understand, the outer dimension here needs to be ?/-1/unknown such that the system can appropriately build the tensor - which makes sense.
But since I do this during the model export process, there needs to be some way to auto-broadcast this [10,] tensor into [?, 10] (vs the [1,10] that I do today. Is there an easy way to do this in TF?
Edit:
Alright, solved this by doing a tf.tile and then a tf.reshape based on the input shape.
@viksit I'm trying to understand the semantics of classes. Is classes supposed to always repeat all available classes? Or are you trying to looking the class name corresponding to the highest score?
@rhaertel80 classes is just a table. The output then appears as [[list of classes],[list of scores]] - and you can use indices to do mappings to the class name strings in any way you'd like.
Solution:
# class_list = ["a", "B", ...] # len 10
class_tensor = tf.constant(class_list)
indices = tf.constant(list(range(0,len(class_list))))
table = tf.contrib.lookup.index_to_string_table_from_tensor(class_tensor)
classes = table.lookup(tf.to_int64(indices))
reshaped_classes = tf.tile(tf.reshape(classes, (1, -1)), [inputs.get_shape()[0], 1])
signature = predict_signature_def(inputs={"input": inputs},
outputs={
"scores": outputs,
"classes": reshaped_classes})
@viksit thanks for confirming. Repeating the list of classes for every instances is obviously wasteful, unfortunately, we only support having outputs/predictions for every instance at this time. We are looking into the best way to allow for per-request outputs, although with something like classes, I would expect that to be queryable on a per-model basis (something else we'll look into).
As long as you don't mind the repetition, your proposal should work.
Tip: in your code classes == class_tensor, so you can simplify by deleting everything related to the table and lookup.
@NilsLattek @viksit a quick update. We are still working on the new object_detection code to support batched inputs for Cloud ML prediction. The code would export a model that have correct shapes for both inputs and outputs (which would pass the model validation). We expect to have that ready early next week.
@yixinshi thanks for the update, and for your team's work. It appears that I can't assign you to this issue, because you're not in the "TensorFlow" github organization. But let's treat it as if you own things, until you determine that you've completed your part.
@yixinshi thanks a lot for your work! Looking forward to testing the changes.
Cliff Young [email protected] schrieb am Fr. 7. Juli 2017 um 20:23:
@yixinshi https://github.com/yixinshi thanks for the update, and for your team's work. It appears that I can't assign you to this issue, because you're not in the "TensorFlow" github organization. But let's treat it as if you own things, until you determine that you've completed your part.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/models/issues/1811#issuecomment-313820892, or mute the thread https://github.com/notifications/unsubscribe-auth/AAcvmgsCd0ndizUor69u8THQOJYFIWpzks5sLsvpgaJpZM4OJCBk .
are there any new results, yet?