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 a model created and trained in Keras 2. in new Keras 3.7 env for inference #20687

Open
demybug123 opened this issue Dec 25, 2024 · 0 comments
Assignees
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.

Comments

@demybug123
Copy link

I am working on a computer vision project and I have this model that is trained in Keras 2. and saved as an .h5 file.
In the 'old' Keras 2. environment, I can load the model with custom and run predict just fine.

Here's the code snippet I use for loading model and custom eswish activation function.

from tensorflow.keras.backend import set_learning_phase
from tensorflow.keras.models import load_model

class eswish(Activation):
    """
    Custom Swish activation function for Keras.
    """
    
    def __init__(self, **kwargs):
        super(Swish, self).__init__(**kwargs)
        self.__name__ = 'Swish'
    def __call__(self, inputs):
        beta = 1.25
        return beta * inputs * sigmoid(inputs)
    def get_config(self):
        return super().get_config()

set_learning_phase(0)
model = load_model(r'path/to/model.h5', custom_objects={'eswish':eswish})
#run inference on model
model.predict()

Here is the specification for the 'old' environment:
Python 3.7
Tensorflow 2.6
Keras 2.6

Now, I want to run inference with this model in 'new' environment. Specifcation for the 'new' environment.
Python 3.12
Tensorflow 2.16.1
Keras 3.0

I understand that loading old model with keras.model.load_model is no longer usable in Keras 3. So currently I am trying to export the model's config and weights to model.json and model.weights.h5 seperately then load these files in new Keras 3.0 environment.
The problem is that the model has custom activation function and layers. I have the definition for these custom activation function and layers. The question is how can I export the config of the model to .json with these custom functions and layers? Here's the code snippet for what I'm trying to do.

In 'old' Keras 2. environment:

import json
from tensorflow.keras.backend import set_learning_phase
from tensorflow.keras.models import load_model

class eswish(Activation):
    """
    Custom Swish activation function for Keras.
    """
    ...

set_learning_phase(0)
model = load_model(r'path/to/model.h5', custom_objects={'eswish':eswish})

#export config to json with custom activation function?
json_config = model.to_json()

with open('model_config.json', 'w') as outfile:
    json.dump(json_config, outfile)
@dhantule dhantule added the type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited. label Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:support User is asking for help / asking an implementation question. Stackoverflow would be better suited.
Projects
None yet
Development

No branches or pull requests

3 participants