0

I implemented my custom layer to perform resizing of images with padding. I just want to resize my images (proportionally) that's why I implemented custom layer instead of using tf.image.resize(). And I want to do this using special preprocessing layer instead of doing it manually in Python. Then I can save and load the model that takes images of different sizes. So it will be very portable and so on. But when I use the predict() method to test the model, I get the following red warnings:

WARNING:tensorflow:6 out of the last 12 calls to <function _make_execution_function..distributed_function at 0x0000026D2E2C53A8> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings is likely due to passing python objects instead of tensors. Also, tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. Please refer to https://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args and https://www.tensorflow.org/api_docs/python/tf/function for more details.

Here is my code

class ResizeWithPad(Layer):
"""Resize an image to the target width and height by keeping the aspect ratio
the same without distortion. If the target dimensions don't match the image dimensions,
the image is resized and then padded with zeroes to match requested dimensions.
The input should be 4-D Tensor of shape [batch, height, width, channels].

Arguments:

height: Integer, the height of the output shape.

width: Integer, the width of the output shape.

name: A string, the name of the layer.
"""

def __init__(self, height, width, name=None, **kwargs):
    super(ResizeWithPad, self).__init__(name=name, **kwargs)
    self.target_height = height
    self.target_width = width
    self.input_spec = InputSpec(shape=(None, None, None, 3))
    print("\nTRACE!!!\n")

@tf.function(input_signature=(tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32),),
             experimental_relax_shapes=True)
def call(self, inputs):
    print("\nTRACE!!!\n")
    outputs = tf.image.resize_with_pad(
        inputs, self.target_height, self.target_width)
    return outputs

@tf.function(input_signature=(tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32),),
             experimental_relax_shapes=True)
def compute_output_shape(self, input_shape):
    print("\nTRACE!!!\n")
    input_shape = tensor_shape.TensorShape(input_shape).as_list()
    return tensor_shape.TensorShape(
        [input_shape[0], self.target_height, self.target_width, input_shape[3]])

@tf.function(input_signature=(tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32),),
             experimental_relax_shapes=True)
def get_config(self):
    print("\nTRACE!!!\n")
    config = {
        'height': self.target_height,
        'width': self.target_width
    }
    base_config = super(ResizeWithPad, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))

def main():
    tf.config.experimental_run_functions_eagerly(False)
    img = cv2.imread(file)
    inp = tf.convert_to_tensor(np.expand_dims(roi, axis=0))
    model_output = test_model.predict(inp)[0]

I tried to disable eager execution, use tf.function with different arguments, but nothing helps. Maybe it's just unavoidable when you have inputs of variable length? And maybe I should not be worried about this, because it's normal. You should rebuild the graph if you have new shape. Of course, it takes time, but preprocessing images in Python function before feeding to the model also takes time. So maybe it doesn't matter. What do you think?

  • You might want to check whether existing code with similar functionality (for example you mentioned `tf.image.resize()`) also yields such warnings, and if not, check how it is implemented. – root Jul 31 '21 at 13:50
  • Ok, I will check that out. But I cannot run anything at the moment. 2 days ago in the evening, everything was fine. But yesterday morning the whole program stopped working. I get the same error, whatever I try to run. Maybe some GPU drivers were updated, and it broke my TensorFlow installation. I will reinstall everything and see what will happen. –  Aug 01 '21 at 13:36
  • Is your issue resolved after fresh installation? What is your tensorflow version when everything was working fine? –  Aug 10 '21 at 01:25
  • Yes, I reinstalled TensorFlow 2.1 and it works now. Altough there are still some warnings regarding retracing if I try to implement custom layers. So I decided not to use custom preprocessing layers. I just perform preprocessing using Python code. –  Aug 16 '21 at 13:51

0 Answers0