Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with Masking layer with Tensor as mask_value #20791

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

Surya2k1
Copy link
Contributor

Currently , the Masking layer accepting Tensor also for argument mask_value . After saving and loading the model the mask_value no longer a tensor. Hence the issue. The issue with be an model which have Masking layer and uses a Tensor as mask_value.

Either the Masking layer should not allow Tensors for mask_value or while reloading the model we need to take care of this explicitly.

I am proposing to handle mask_value explicitly during reloading the model.

Might fix #20706

@codecov-commenter
Copy link

codecov-commenter commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.01%. Comparing base (5df8fb9) to head (3428a6a).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #20791   +/-   ##
=======================================
  Coverage   82.01%   82.01%           
=======================================
  Files         557      557           
  Lines       52014    52017    +3     
  Branches     8037     8038    +1     
=======================================
+ Hits        42657    42662    +5     
+ Misses       7403     7402    -1     
+ Partials     1954     1953    -1     
Flag Coverage Δ
keras 81.83% <100.00%> (+<0.01%) ⬆️
keras-jax 64.23% <100.00%> (+0.01%) ⬆️
keras-numpy 58.95% <33.33%> (-0.01%) ⬇️
keras-openvino 29.89% <33.33%> (+<0.01%) ⬆️
keras-tensorflow 64.79% <100.00%> (+<0.01%) ⬆️
keras-torch 64.16% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

You can fix the code format by running sh shell/format.sh

keras/src/layers/core/masking.py Outdated Show resolved Hide resolved
@@ -45,6 +46,9 @@ class Masking(Layer):

def __init__(self, mask_value=0.0, **kwargs):
super().__init__(**kwargs)
# `mask_value` can be a serialized tensor, hence verify it
if isinstance(mask_value, dict) and mask_value.get("config", None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually remove this line! deserialize_keras_object should work in all circumstances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually remove this line! deserialize_keras_object should work in all circumstances.

No. During model building with Tensor it will fail. For example consider below example code where we found the issue. Without this commit the below code is fine while building but fails when saved and reloaded.

model = keras.models.Sequential()
model.add(keras.layers.Masking(mask_value=keras.ops.convert_to_tensor([0.0])))

This will fail with Type Error. Hence I put that check to ensure only serialized tensor will go for deserialization and not other inputs and also not any random dict.

TypeError: Could not parse config: [0.]

    model.add(keras.layers.Masking(mask_value=keras.ops.convert_to_tensor(np.array([0.0]))))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\OSS\Keras\keras\keras\src\layers\core\masking.py", line 51, in __init__
    mask_value = deserialize_keras_object(mask_value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\OSS\Keras\keras\keras\src\saving\serialization_lib.py", line 591, in deserialize_keras_object
    raise TypeError(f"Could not parse config: {config}")
TypeError: Could not parse config: [0.]

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jan 23, 2025
@fchollet fchollet merged commit 592c118 into keras-team:master Jan 23, 2025
7 checks passed
@google-ml-butler google-ml-butler bot removed ready to pull Ready to be merged into the codebase kokoro:force-run labels Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loaded Keras Model Throws Error While Predicting (Likely Issues with Masking)
4 participants