Home > Mobile >  tf.image.resize_with_pad produces type error
tf.image.resize_with_pad produces type error

Time:03-22

I recently introduced image resizing and random rotations to my image data loader. When trying to train my CNN, it gives me the following error message:

    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'.

Minimum reproducible example:

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")

Example image: Download example image (118 KB)

imgr and imgc are the respective number of rows and columns (height and width in pixels) of the original training image and the number of rows and columns of the expected input for model training.

What are 'x' and 'y' in case of the above error message (reproducing it outside a model training session, the error message I get is similar but in slightly different wording)? I suppose the resizing function takes an image and some arguments (height, width, which are numeric but not related to the image data type)?

What causes this error and how can I solve the issue?

Here an example that is closer to my actual script but gives the same error:

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]

I am a bit puzzled where Tensorflow gets a int32 tensor from? At least in the latter example, the image is changed to tf.float32 with values in [0, 1](?)

CodePudding user response:

Nevermind, the int32 tensor came from the width and height arguments. The problem can be solved by using

target_height = int(tf.math.round(imgr * scaling))

and similar for width.

  • Related