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

Loading model Conv2DTranspose from h5 file results in error complaining about group:1 parameter #19441

Open
dmagee opened this issue Apr 4, 2024 · 11 comments
Assignees
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug

Comments

@dmagee
Copy link

dmagee commented Apr 4, 2024

Loading a .h5 model created in TensorFlow.keras containing Conv2DTranspose layer in keras-core results in an error:

Exception encountered: Unrecognized keyword arguments passed to Conv2DTranspose: {'groups': 1}

A hacky fix is to edit keras_core/src/ops/operation.py in the method from_config() so:

    try:
        if cls.__name__=="Conv2DTranspose" and "groups" in config: 
            config.pop("groups")
        return cls(**config)
....

I've yet to confirm the resultant network behaves exactly the same as the original, but it seems to work. It's obviously not the ideal solution. I'm not quite sure what that parameter does, as it's not something I used when creating the network. I imagine most legacy networks using Conv2DTranspose will have the same issue.

Model is here if helpful:
https://drive.google.com/file/d/1Jn9C4vTRVWgHCqAXnMfO_i-B3pK4c14I/view?usp=sharing

Note:it also has the issue raised here about Lambda layers: #19151
(I proposed a hack there, and it looks like a better solution is being worked on)

Derek

@sachinprasadhs
Copy link
Collaborator

Can you try to create a model in Keras 3 instead of keras_core and try to load it in the same version and let us know the outcome of it. Thanks

@dmagee
Copy link
Author

dmagee commented Apr 5, 2024

I'm a bit confused about keras versions. I thought Keras 3 and keras-core were basically the same thing? The model I'm having issues with was created by tf-keras, and I'm trying to load into the latest version of keras-core (installed via pip as keras-core). Do you actually want me to try to install keras 3 (if so how), or do you wnat me to try to generate a model in keras-core (this is on the todo list for next week anyway).

@sachinprasadhs
Copy link
Collaborator

keras-core was a pre-release for Keras 3, now Keras 3 is available and all the latest updates will be made available in the Keras 3 release.
you can install it though pip install -U keras

@dmagee
Copy link
Author

dmagee commented Apr 8, 2024

I'm only getting keras 2.15.0 when I do that? I'm on windows with Python 3.8. Am I doing something wrong, or has Keras 3 not reached windows yet?

@dmagee
Copy link
Author

dmagee commented Apr 8, 2024

Also, 2.15.0 doesn't actually allow you to import keras as it fails with a tensorflow related error. The maximum tensorflow version that seems to be available is 2.13.1, which works with keras 2.13.1.

@dmagee
Copy link
Author

dmagee commented Apr 8, 2024

I managed to install keras3 by starting with a fresh installation of python 3.11 and just installing pytorch and keras (plus packaging and pandas which appear to be dependencies of keras but aren't configured as such). The same problem(s) exist with this version as with keras_core loading historic models from tf-keras, and the same hacky fix(es) work to get the model loaded. I'm going to port our model generation code over now, and see if a h5 generated by keras3 will have the same issue (not that that solves anything for legacy models). I'll report back.

@dmagee
Copy link
Author

dmagee commented Apr 8, 2024

I can now confirm that if the model is created in Keras 3, and saved as .h5 the problem does not occur. It only occurs with historic models created in tf.keras. However, we (and customers) have a large collections of such models.

@sachinprasadhs
Copy link
Collaborator

Could you please provide some sample reproducible code to replicate the reported behavior. Thanks

@dmagee
Copy link
Author

dmagee commented Apr 10, 2024

The minimal code below will throw that error with the model I linked above. However, first you need to fix #19151. I proposed a hack for that there, but I see there are some posts, so I'll go respond to that now.

import os
os.environ["KERAS_BACKEND"] = "torch"

import keras 
from keras.layers import Lambda

@keras.saving.register_keras_serializable()
class Linear(keras.layers.Layer):
    def __init__(self, units=32, input_shape=None, name=None,**kwargs):
        super(Linear, self).__init__(trainable=True, name=name)
        self.op_shape = input_shape
        self.units=units
        self.name=name


    def build(self, input_shape):

        self.b = self.add_weight(
            name="b",
            shape=(self.units,),
            initializer=keras.initializers.RandomNormal(mean=0.0, stddev=0.01),
            trainable=True
        )
        super(Linear, self).build(input_shape)
        

    def get_config(self):
        config = super(Linear, self).get_config()
        config['units'] = self.units
        config['input_shape'] = self.op_shape
        return dict(list(config.items()))
    @classmethod
    def from_config(cls, config):
        units_config = config.pop("units")
        units = keras.saving.deserialize_keras_object(units_config)
        input_shape_config = config.pop("input_shape")
        input_shape = keras.saving.deserialize_keras_object(input_shape_config)
        name = config.pop("name")
		
        return cls(units=units,input_shape=input_shape,name=name)
		
    def call(self, inputs):

        return keras.layers.LeakyReLU(negative_slope=0.01)(inputs + keras.ops.broadcast_to(self.b,[self.op_shape[0],self.op_shape[1],self.units]))

with keras.utils.custom_object_scope({'Linear': Linear}):		
        model = keras.models.load_model('old_keras_model.h5',compile=False, custom_objects={'LeakyReLU': keras.layers.LeakyReLU(negative_slope=0.01),'Linear':Linear})

@fchollet
Copy link
Member

Unfortunately this code snippet is not reproducible since it refers to 'old_keras_model.h5'. Do you have a reproducible code snippet? You can attach files to GitHub comments.

@dmagee
Copy link
Author

dmagee commented Apr 12, 2024

@fchollet I attached the model above. For convenience it is here: https://drive.google.com/file/d/1Jn9C4vTRVWgHCqAXnMfO_i-B3pK4c14I/view?usp=sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting keras-eng Awaiting response from Keras engineer type:Bug
Projects
None yet
Development

No branches or pull requests

3 participants