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

tf.image.resize_with_pad类型错误排查:Sub操作输入类型不匹配问题及解决方案问询

Fixing Type Mismatch Error in tf.image.resize_with_pad with Random Scaling

Let's break down your problem and solve it step by step:

1. What do 'x' and 'y' mean in the error messages?

The Sub operation mentioned in the errors is an internal calculation used by tf.image.resize_with_pad (likely for computing padding amounts or scaling ratios).

  • In your first error, x refers to a float32 tensor derived from your image (either during internal type conversion of the input image, or intermediate scaling values), while y is an int32 tensor created by implicitly converting your float-type target dimensions (imgr * scaling/imgc * scaling) to integers.
  • In the second error, the roles flip slightly: the operation expects a float tensor for one input, but gets an int32 tensor from your target dimensions (again, due to implicit type conversion of your float target sizes).

The core issue here is a mismatch between the type of your target dimensions and what TensorFlow expects for internal calculations.

2. Why does this type mismatch happen?

tf.image.resize_with_pad requires integer values for target_height and target_width—you can't have an image with a fractional height or width!

In your code, scaling is a float tensor (generated by tf.random.uniform), so imgr * scaling and imgc * scaling result in float tensors. Even though your image data type (uint8 or float32) seems unrelated, TensorFlow's internal logic for resizing and padding relies on integer dimensions. The implicit conversion between your float target sizes and integer values causes a type clash during arithmetic operations like subtraction.

3. How to fix the error?

You need to explicitly convert your scaled target dimensions to integer tensors before passing them to tf.image.resize_with_pad. Here are two reliable approaches:

Option 1: Round to the nearest integer (most balanced)

Use tf.round to get the closest integer value, then cast to tf.int32:

imgc, imgr = 256, 256
import tensorflow as tf
img_path = "C:/Users/Manuel/Desktop/B1_6_000300000000_X.png"
scaling = ((tf.random.uniform(()) * 0.2) + 0.8)
image = tf.io.read_file(img_path)
image = tf.image.decode_png(image, channels = 3)
input_image = tf.cast(image, tf.float32) / 255.0

# Convert scaled dimensions to integers
target_height = tf.cast(tf.round(imgr * scaling), tf.int32)
target_width = tf.cast(tf.round(imgc * scaling), tf.int32)

input_image = tf.image.resize_with_pad(
    input_image,
    target_height=target_height,
    target_width=target_width,
    method="lanczos3"
)

Option 2: Floor or Ceil for consistent truncation/round-up

If you prefer to always round down or up instead of to the nearest integer, use tf.floor or tf.ceil:

# For rounding down to the nearest integer
target_height = tf.cast(tf.floor(imgr * scaling), tf.int32)
target_width = tf.cast(tf.floor(imgc * scaling), tf.int32)

# OR for rounding up to the nearest integer
target_height = tf.cast(tf.ceil(imgr * scaling), tf.int32)
target_width = tf.cast(tf.ceil(imgc * scaling), tf.int32)

Both approaches ensure your target dimensions are integer tensors, matching the type TensorFlow expects for resize_with_pad's internal calculations, eliminating the type mismatch error.

内容的提问来源于stack exchange,提问作者Manuel Popp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 16:18:11