Skip to content

compute_output_shape behavior in normalization layers need to fix #2677

@Zhaopudark

Description

@Zhaopudark

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Window 11
  • TensorFlow version and how it was installed (source or binary): binary
  • TensorFlow-Addons version and how it was installed (source or binary): binary
  • Python version: 3.9.7
  • Is GPU used? (yes/no): yes

Describe the bug
When calling function compute_output_shape of tfa.layers.GroupNormalization, tfa.layers.InstanceNormalization and tfa.layers.FilterResponseNormalization , the layer will not be built, different form the description in compute_output_shape, such as "https://github.com/keras-team/keras/blob/v2.8.0/keras/engine/base_layer.py#L798-L842":

compute_output_shape
View source
compute_output_shape(input_shape)
Computes the output shape of the layer.
If the layer has not been built, this method will call build on the layer. This assumes that the layer will later be used with inputs that match the input shape provided here.

Code to reproduce the issue

>>> import tensorflow as tf
>>> import tensorflow_addons as tfa
>>> layer = tfa.layers.GroupNormalization(groups=2, center=True, scale=True)
>>> layer.compute_output_shape(input_shape=[8, 28, 28, 16])
[8, 28, 28, 16]
>>> print(len(layer.varibales))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'GroupNormalization' object has no attribute 'varibales'
>>> print(len(layer.trainable_varibales))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'GroupNormalization' object has no attribute 'trainable_varibales'
>>> assert len(layer.trainable_variables) == 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

Other info / logs
I deeply know the bug. Since tfa.layers.GroupNormalization, tfa.layers.InstanceNormalization and tfa.layers.FilterResponseNormalization are all inherited from tf.keras.layers.Layer, they all override compute_output_shape function.

However, there is no need to override this function, because tf.keras.layers.Layer has done it well, i.e., if call and build functions have been overrided, the layer can automaticly give out correct output_shape buy compute_output_shape in superclass. Remove the overrided compute_output_shape in tfa.layers.GroupNormalization, tfa.layers.InstanceNormalization and tfa.layers.FilterResponseNormalization, the bug will disappear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions