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 trained Spektral GeneralGNN model #407

Closed
senemaktas opened this issue Nov 16, 2022 · 6 comments
Closed

loading trained Spektral GeneralGNN model #407

senemaktas opened this issue Nov 16, 2022 · 6 comments

Comments

@senemaktas
Copy link

Hi,

I use the GeneralGNN model from Spektral with my own dataset. Training and evaluation works fine. When I try to load the trained model, I get different errors for different loading approaches for example, weights,SavedModel, model.to_json etc..
So, my question is how to save and load the trained GeneralGNN model, is there any way to do it?. Note: I do not make any changes to this model.

thanks.

@danielegrattarola
Copy link
Owner

Sorry for the late reply but for some reason I stopped receiving notifications.

Can you provide more details on the error you are getting?

@senemaktas
Copy link
Author

Hello again,

For example, in one of my approach; I use the below code:

from keras.models import model_from_config
model = model_from_config(open("model.json", "r").read())

The json file consist of those informations: "{"class_name": "GeneralGNN", "config": {"output": 2, "activation": "softmax", "hidden": 256, "message_passing": 4, "pre_process": 2, "post_process": 2, "connectivity": "cat", "batch_norm": true, "dropout": 0.0, "aggregate": "sum", "hidden_activation": "prelu", "pool": "sum"}, "keras_version": "2.8.0", "backend": "tensorflow"}"

The error is in the below.
ValueError: Unknown layer: GeneralGNN. Please ensure this object is passed to the custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

Same thing for

from tensorflow.keras.models import model_from_json
loaded_model = model_from_json(open("model.json", "r").read())
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

For the another approach:

# Retrieve the config
config = GeneralGNN(data.n_labels, activation="softmax").get_config()
print("config", config)

# At loading time, register the custom objects with a `custom_object_scope`:
custom_objects = {"GeneralGNN": GeneralGNN, "custom_activation": "softmax"}
with tf.keras.utils.custom_object_scope(custom_objects):
    new_model = keras.Model.from_config(config)

Error:
new_model = keras.Model.from_config(config)
File "C:\Users...\lib\site-packages\keras\engine\training.py", line 2641, in from_config functional.reconstruct_from_config(config, custom_objects))
File "C:\Users....\lib\site-packages\keras\engine\functional.py", line 1325, in reconstruct_from_config for layer_data in config['layers']:
KeyError: 'layers'

thank you.

@danielegrattarola
Copy link
Owner

Let me look into it.

In the meantime, does it work if you reinitialize a GeneralGNN model from scratch and then simply restore the weights?

@senemaktas
Copy link
Author

I tried loading the weights as shown in the below. I got config not found error in 3 models (model1,model2, model3). I have try them seperately, one by one.

from spektral.models import GeneralGNN
# from general_gnn import GeneralGNN
model = GeneralGNN(data.n_labels, activation="softmax")
model1 = load_model('model.h5')
model2 = keras.models.load_model("model.h5", custom_objects={'GeneralGNN': model})
model3 = keras.models.load_model("model.h5", custom_objects={'GeneralGNN': GeneralGNN, "custom_activation": "softmax"})
# model3 = keras.models.load_model("model.h5", custom_objects={'GeneralGNN': GeneralGNN(data.n_labels, activation="softmax")})

raise ValueError(f'No model config found in the file at {filepath}.')
ValueError: No model config found in the file at <tensorflow.python.platform.gfile.GFile object at 0x000002EA83502820>.


Also I have tried another approach

# Retrieve the config
# from general_gnn import GeneralGNN
from spektral.models import GeneralGNN
config = GeneralGNN(data.n_labels, activation="softmax").get_config()
# config = {'output': 2, 'activation': 'softmax', 'hidden': 256, 'message_passing': 4, 'pre_process': 2, 'post_process': 2,
# 'connectivity': 'cat', 'batch_norm': True, 'dropout': 0.0, 'aggregate': 'sum', 'hidden_activation': 'prelu', 'pool': 'sum'}

# At loading time, register the custom objects with a `custom_object_scope`:
custom_objects = {"GeneralGNN": GeneralGNN, "custom_activation": "softmax"}
with tf.keras.utils.custom_object_scope(custom_objects):
    new_model = keras.Model.from_config(config)

File "C:\Users.....\lib\site-packages\keras\engine\training.py", line 2641, in from_config
functional.reconstruct_from_config(config, custom_objects))
File "C:\Users......\lib\site-packages\keras\engine\functional.py", line 1325, in reconstruct_from_config
for layer_data in config['layers']:
KeyError: 'layers'

When i change the last line of code -> new_model = keras.Sequential.from_config(config)
It gives "ValueError: Unknown layer: output. Please ensure this object is passed to the custom_objects argument."
(The "output" layer in the config.)


For the last option:

When I load from json and get the weights

from tensorflow.keras.models import model_from_json
loaded_model = model_from_json(open("model.json", "r").read())
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
  • Error: Unknown layer: GeneralGNN. Please ensure this object is passed to the custom_objects argument.
  • İnside json: {"class_name": "GeneralGNN", "config": {"output": 2, "activation": "softmax", "hidden": 256, "message_passing": 4, "pre_process": 2, "post_process": 2, "connectivity": "cat", "batch_norm": true, "dropout": 0.0, "aggregate": "sum", "hidden_activation": "prelu", "pool": "sum"}, "keras_version": "2.8.0", "backend": "tensorflow"}

It wants right config files, but the files which i store does not save whole things related to GNN model (GeneralGNN). It only saves some basic or last layers. I am not sure how to do this. Training is working perfectly.

Thank you.

@danielegrattarola
Copy link
Owner

The problem is in recreating a model from the config, so the best you can do is instantiate a new model using the GeneralGNN constructor and then simply calling load_weights. This is only a temporary workaround but it should work

@senemaktas
Copy link
Author

Thank you, now i am able to upload my trained model. Solution for other people:

model.save_weights('./model_path/')
loaded_model = GeneralGNN(data.n_labels, activation="softmax")
loaded_model.load_weights("./model_path/")

note: model.save('my_model', include_optimizer=True) and model.save_weights("model.h5") does not work

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

No branches or pull requests

2 participants