You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow Serving传图报错:tf.image.decode_jpeg要求输入为标量

Fix for "contents must be scalar, got shape [1]" in TensorFlow Serving Image Classification

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:

  1. 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")
    
  2. Use tf.map_fn to 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)
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:42:19