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

Closed
dmagee opened this issue Apr 4, 2024 · 16 comments

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

@ghsanti
Copy link
Contributor

ghsanti commented Aug 6, 2024

Adding this solves the problem (click me.)
# hack to change model config from keras 2->3 compliant
import h5py
f = h5py.File("/content/old_keras_model.h5", mode="r+")
model_config_string = f.attrs.get("model_config")
if model_config_string.find('"groups": 1,') != -1:
    model_config_string = model_config_string.replace('"groups": 1,', '')
    f.attrs.modify('model_config', model_config_string)
    f.flush()
    model_config_string = f.attrs.get("model_config")
    assert model_config_string.find('"groups": 1,') == -1

f.close()

As it was indicated in #19151, in this comment.

Is the it fixed for you @dmagee ?

I include a GIST that downloads and instantiates the model and problem (removing the snippet I provided above from it).

@sachinprasadhs sachinprasadhs added stat:awaiting response from contributor and removed stat:awaiting keras-eng Awaiting response from Keras engineer labels Aug 6, 2024
Copy link

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

@github-actions github-actions bot added the stale label Aug 21, 2024
Copy link

github-actions bot commented Sep 4, 2024

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

@github-actions github-actions bot closed this as completed Sep 4, 2024
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@CS-DaviMagalhaes
Copy link

try using tensorflow version 2.12.1, worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants