tensorflow / addons

Useful extra functionality for TensorFlow 2.x maintained by SIG-addons
Apache License 2.0
1.69k stars 611 forks source link

Typeguard dependency update needed #2819

Closed bryzgalovdm closed 1 year ago

bryzgalovdm commented 1 year ago

System information

Describe the bug

When WeightNormalization layer is initialized, check_type_internal() function of typeguard used to make sure the object given to WeightNormalization is a tensorflow layer fails with the following traceback (see below). Tensorflow-addons installs the last typeguard which is 3.0.0 (released yesterday). If I fall back to 2.13.0 the problem dissapears.

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

[<ipython-input-6-973cec7abd8b>](https://localhost:8080/#) in <module>
      1 # WeightNorm ConvNet
      2 wn_model = tf.keras.Sequential([
----> 3     tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(6, 5, activation='relu')),
      4     tf.keras.layers.MaxPooling2D(2, 2),
      5     tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(16, 5, activation='relu')),

2 frames

[/usr/local/lib/python3.9/dist-packages/tensorflow_addons/layers/wrappers.py](https://localhost:8080/#) in __init__(self, layer, data_init, **kwargs)
     57 
     58     @typechecked
---> 59     def __init__(self, layer: tf.keras.layers, data_init: bool = True, **kwargs):
     60         super().__init__(layer, **kwargs)
     61         self.data_init = data_init

[/usr/local/lib/python3.9/dist-packages/typeguard/_functions.py](https://localhost:8080/#) in check_argument_types(memo)
    111             value = memo.arguments[argname]
    112             try:
--> 113                 check_type_internal(value, expected_type, memo=memo)
    114             except TypeCheckError as exc:
    115                 qualname = qualified_name(value, add_class_prefix=True)

[/usr/local/lib/python3.9/dist-packages/typeguard/_checkers.py](https://localhost:8080/#) in check_type_internal(value, annotation, memo)
    668             return
    669 
--> 670     if not isinstance(value, origin_type):
    671         raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
    672 

TypeError: isinstance() arg 2 must be a type or tuple of types

Code to reproduce the issue

You can reproduce the code using example code on the tensorflow page. The error will be in the second cell under build models.

Other info / logs

It breaks here:

# WeightNorm ConvNet
wn_model = tf.keras.Sequential([
    tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(6, 5, activation='relu')),
    tf.keras.layers.MaxPooling2D(2, 2),
    tfa.layers.WeightNormalization(tf.keras.layers.Conv2D(16, 5, activation='relu')),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Flatten(),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(120, activation='relu')),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(84, activation='relu')),
    tfa.layers.WeightNormalization(tf.keras.layers.Dense(num_classes, activation='softmax')),
])
DanaBCooper commented 1 year ago

Second the dependency update - typeguard 3.0.0 also breaks pytest < 7.0.0