question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`RandomAugmentationPipeline` serialization test fails for `layers!=[]`

See original GitHub issue

@LukeWood

In layers/serialization_test.py, currently the RandomAugmentationPipeline serialization test is as follows: https://github.com/keras-team/keras-cv/blob/92b4e16fa5686e861b2f2cd7d04a6c5d7d3c0921/keras_cv/layers/serialization_test.py#L98-L102

However, if we change it as follows the test fails.

        (
            "RandomAugmentationPipeline",
            preprocessing.RandomAugmentationPipeline,
            {
                "layers": [preprocessing.RandomSaturation(0.5)],
                "augmentations_per_image": 1,
                "rate": 1.0
            },
        ),

If I’m not mistaken this implies that the layers argument, being a list, is not getting correctly tested. We are facing the same problem as earlier, the test compares the memory location and not the logical value. When we manually print the internal layer attributes, they turn out to be the same.

# Original config:
{'name': 'random_augmentation_pipeline', 'trainable': True, 'dtype': 'float32', 'augmentations_per_image': 1, 'rate': 1.0, 'layers': ListWrapper([<keras_cv.layers.preprocessing.random_saturation.RandomSaturation object at 0x0000025C55B18488>]), 'seed': None}

# Reconstructed config
{'name': 'random_augmentation_pipeline', 'trainable': True, 'dtype': 'float32', 'augmentations_per_image': 1, 'rate': 1.0, 'layers': ListWrapper([<keras_cv.layers.preprocessing.random_saturation.RandomSaturation object at 0x0000025C58530788>]), 'seed': None}
    # keras_cv\layers\serialization_test.py: 233
    # Checking correctness of serialized and deserialized objects
    def test_layer_serialization(self, layer_cls, init_args):
        layer = layer_cls(**init_args)
        if "seed" in init_args:
            self.assertIn("seed", layer.get_config())

        model = tf.keras.models.Sequential(layer)
        model_config = model.get_config()

        reconstructed_model = tf.keras.Sequential().from_config(model_config)
        reconstructed_layer = reconstructed_model.layers[0]

        if layer_cls == preprocessing.RandomAugmentationPipeline:
            print(layer.get_config()["layers"][0].factor.upper)
            print(reconstructed_layer.get_config()["layers"][0].factor.upper)

        self.assertTrue(
            config_equals(layer.get_config(), reconstructed_layer.get_config())
        )

# Output:
# 0.5
# 0.5

I think we need a more robust test to check serialization and deserialization. Otherwise these problems will keep arising as the complexity of the layers (and their arguments) increases. Please share your thoughts on the same.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
LukeWoodcommented, Jul 21, 2022

Thank you @AdityaKane2001 !

1reaction
LukeWoodcommented, Jul 14, 2022

Sounds good to me Aditya!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Junit test failed for user-defined object Serialization
It looks like your test is comparing the object references and not the object values. Override the equals function or compare each value...
Read more >
keras.layers.IntegerLookup fails to deserialize vocubulary
When creating a keras.layers.IntegerLookup, we provide a vocabulary. This is saved off via serialization. However, upon deserialization ...
Read more >
Testing JSON Serialization With @JsonTest and Spring Boot
Use Spring Boot's @JsonTest annotation to focus on testing your JSON serialization for using either Jackson, Gson or Jsonb.
Read more >
Serialization and saving - Keras
Description: Complete guide to saving & serializing models. ... The architecture, or configuration, which specifies what layers the model ...
Read more >
tf.keras.layers.serialize | TensorFlow v2.11.0
Serializes a Layer object into a JSON-compatible representation. ... Check out sessions from the WiML Symposium covering diffusion models ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found