tf.image.resize_with_pad 产生类型错误
tf.image.resize_with_pad produces type error
我最近在我的图像数据加载器中引入了图像大小调整和随机旋转。尝试训练我的 CNN 时,出现以下错误消息:
File "/home/kit/ifgg/mp3890/LeleNet/py3/LeleNet_trn.py", line 467, in
load_image_train * input_image = tf.image.resize_with_pad(input_image,
imgr * scaling, imgc * scaling)
TypeError: Input 'y' of 'Sub' Op has type int32 that does not match type float32 of argument 'x'.
最小可重现示例:
imgc, imgr = 256, 256
import tensorflow as tf
img_path = "C:/Users/Manuel/Desktop/B1_6_000300000000_X.png" # see image url below
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.image.resize_with_pad(image, \
target_height = imgr * scaling, \
target_width = imgc * scaling, \
method = "lanczos3")
示例图片:
Download example image (118 KB)
imgr
和imgc
分别是原始训练图像的行数和列数(高度和宽度以像素为单位)和模型训练的预期输入的行数和列数.
如果出现上述错误消息,'x' 和 'y' 是什么(在模型训练会话之外重现它,我得到的错误消息相似,但措辞略有不同)?我想调整大小函数需要一个图像和一些参数(高度、宽度,它们是数字但与图像数据类型无关)?
导致此错误的原因是什么?我该如何解决?
这是一个更接近我的实际脚本但给出相同错误的示例:
imgc, imgr = 256, 256
import tensorflow as tf
img_path = "C:/Users/Manuel/Desktop/B1_6_000300000000_X.png" # see image url
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
input_image = tf.image.resize_with_pad(input_image, \
target_height = imgr * scaling, \
target_width = imgc * scaling, \
method = "lanczos3")
InvalidArgumentError: cannot compute Sub as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Sub]
我有点疑惑Tensorflow从哪里得到int32张量?至少在后一个示例中,图像更改为 tf.float32
,其值在 [0, 1](?)
中
没关系,int32 张量来自宽度和高度参数。使用
可以解决问题
target_height = int(tf.math.round(imgr * scaling))
宽度类似。
我最近在我的图像数据加载器中引入了图像大小调整和随机旋转。尝试训练我的 CNN 时,出现以下错误消息:
File "/home/kit/ifgg/mp3890/LeleNet/py3/LeleNet_trn.py", line 467, in
load_image_train * input_image = tf.image.resize_with_pad(input_image,
imgr * scaling, imgc * scaling)
TypeError: Input 'y' of 'Sub' Op has type int32 that does not match type float32 of argument 'x'.
最小可重现示例:
imgc, imgr = 256, 256
import tensorflow as tf
img_path = "C:/Users/Manuel/Desktop/B1_6_000300000000_X.png" # see image url below
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.image.resize_with_pad(image, \
target_height = imgr * scaling, \
target_width = imgc * scaling, \
method = "lanczos3")
示例图片: Download example image (118 KB)
imgr
和imgc
分别是原始训练图像的行数和列数(高度和宽度以像素为单位)和模型训练的预期输入的行数和列数.
如果出现上述错误消息,'x' 和 'y' 是什么(在模型训练会话之外重现它,我得到的错误消息相似,但措辞略有不同)?我想调整大小函数需要一个图像和一些参数(高度、宽度,它们是数字但与图像数据类型无关)?
导致此错误的原因是什么?我该如何解决?
这是一个更接近我的实际脚本但给出相同错误的示例:
imgc, imgr = 256, 256
import tensorflow as tf
img_path = "C:/Users/Manuel/Desktop/B1_6_000300000000_X.png" # see image url
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
input_image = tf.image.resize_with_pad(input_image, \
target_height = imgr * scaling, \
target_width = imgc * scaling, \
method = "lanczos3")
InvalidArgumentError: cannot compute Sub as input #1(zero-based) was expected to be a float tensor but is a int32 tensor [Op:Sub]
我有点疑惑Tensorflow从哪里得到int32张量?至少在后一个示例中,图像更改为 tf.float32
,其值在 [0, 1](?)
没关系,int32 张量来自宽度和高度参数。使用
可以解决问题target_height = int(tf.math.round(imgr * scaling))
宽度类似。