Reputation: 1885
I'm using Tensorflow 1.14.0 and am trying to write a Python script that loads a model saved using tf.compat.v1.saved_model.simple_save to process an image from disk. The model works fine when I use Tensorflow Serving and a GRPC or REST client. However, when I try to use it in a single script, I get the following error:
ValueError: Cannot feed value of shape () for Tensor 'Placeholder_1084:0', which has shape '(?,)'
I'm sure that I've got something fundamental wrong but haven't been able to figure it out just yet. I've put together a short sample that illustrates what I'm trying to accomplish:
#!/usr/bin/env python
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.saved_model import signature_constants
savedModelPath = "./models/inception_v3/1/"
filename = "./imagenet/n02763714/image_0.jpg"
session = tf.compat.v1.Session(graph=tf.Graph())
with session.graph.as_default():
model = tf.compat.v1.saved_model.load(export_dir=savedModelPath, sess=session, tags=[tag_constants.SERVING])
model_def = model.signature_def[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
graph_def = tf.compat.v1.get_default_graph()
input_tensor = graph_def.get_tensor_by_name(model_def.inputs["image_bytes"].name)
output_tensor = graph_def.get_tensor_by_name(model_def.outputs["predictions"].name)
image_data = tf.io.gfile.GFile(filename, 'rb').read()
session.run(output_tensor, {input_tensor: image_data})
Thanks in advance for any help!
EDIT: If it's helpful here is the model signature:
inputs {
key: "image_bytes"
value {
name: "Placeholder_1084:0"
dtype: DT_STRING
tensor_shape {
dim {
size: -1
}
}
}
}
outputs {
key: "predictions"
value {
name: "inception_v3/predictions/Softmax:0"
dtype: DT_FLOAT
tensor_shape {
dim {
size: -1
}
dim {
size: 1000
}
}
}
}
method_name: "tensorflow/serving/predict"
Upvotes: 1
Views: 95
Reputation: 59691
Your model expects a one-dimensional input, which is what the shape (?,)
means. Presumably, in this input each value is a sequence of bytes encoding an image. In your code, you seem to be trying to use the model to get the prediction for a single image. When you do:
image_data = tf.io.gfile.GFile(filename, 'rb').read()
You are getting a single bytes
value in image_data
, which, from the point of view of TensorFlow, is a scalar value (a value with shape ()
), like an individual int
or float
. You can get it to work if you give that value as a one-element list with that value, which TensorFlow will interpret as a one-dimensional input:
session.run(output_tensor, {input_tensor: [image_data]})
Note that the value returned by the line above will (again, presumably) have shape (1, 1000)
. The predicted output vector for your image can then be extracted as:
session.run(output_tensor, {input_tensor: [image_data]})[0]
Upvotes: 1