TensorFlow Serving传图报错:tf.image.decode_jpeg要求输入为标量
Let's break down what's causing this error and how to fix it quickly:
Root Cause
Your client is sending a batch of 1 image (wrapped in an array: [{"images": input_string}]), so TensorFlow Serving parses this into a string tensor with shape [1]. But your server's input_bytes placeholder is defined as a scalar (shape=[]), and tf.image.decode_jpeg only accepts scalar string inputs (one raw image byte string at a time). Your attempted tf.reshape(input_bytes, []) doesn't work because the shape mismatch happens before that operation runs—during input parsing from the JSON request.
Solution 1: Update the Server to Accept Batches
If you want to support batch predictions later, modify the server code to handle a variable-length batch of image strings:
Change the placeholder shape to accept batches:
# Accept a batch of byte strings (shape [None] = variable length) input_bytes = tf.placeholder(tf.string, shape=[None], name="input_bytes")Use
tf.map_fnto process each image in the batch:def process_single_image(byte_str): # Decode and preprocess one image input_tensor = tf.image.decode_jpeg(byte_str, channels=3) input_tensor = tf.image.convert_image_dtype(input_tensor, dtype=tf.float32) input_tensor = input_tensor / 127.5 - 1.0 input_tensor = tf.reshape(input_tensor, [64, 64, 3]) return input_tensor # Apply processing to all images in the batch input_tensors = tf.map_fn(process_single_image, input_bytes, dtype=tf.float32) # Run batch inference y = model(input_tensors)Rebuild the SavedModel with this updated graph—now the input signature expects a batch, matching what your client sends.
Solution 2: Modify the Client to Send a Single Scalar Input
If you only need single-image predictions, adjust the client JSON to send a single instance instead of an array:
# Remove the outer array from the instance instance = {"images": input_string} # Wrap directly as a single instance (not an array) data = json.dumps({"instances": instance})
This way, TensorFlow Serving parses the input as a scalar string tensor (shape=[]), which matches your server's placeholder and works with tf.image.decode_jpeg.
Verify the Fix
After making either change, re-deploy the server (if you modified it) and re-run the client. The error should disappear, and you'll get the prediction response as expected.
内容的提问来源于stack exchange,提问作者Manuel Jan

