danielegrattarola / spektral

Graph Neural Networks with Keras and Tensorflow 2.
https://graphneural.network
MIT License
2.37k stars 334 forks source link

Failing to predict after model saving #397

Open datapaf opened 1 year ago

datapaf commented 1 year ago

Hello!

I have a custom dataset of graphs composed from invoice receipts (receipts vary in the number of words, so the number of nodes varies too).

I use GCN model for node-level prediction: model = GCN(n_labels=2, channels=16, activation='relu', output_activation='softmax', dropout_rate=0)

Batch loader is used to train the model:

batch_size = 16
epochs = 4

loader_tr = BatchLoader(data_tr, batch_size=batch_size, epochs=epochs, node_level=True, shuffle=False)

After the training the following prediction runs successfully: model([features, adj])

But after saving the model the prediction fails:

model.save('model')

from tensorflow.keras.models import load_model
loaded_model = load_model('model')

loaded_model([features, adj])

With this error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-164-e804f85e8708>](https://localhost:8080/#) in <module>
----> 1 loaded_model([features, adj])

1 frames
[/usr/local/lib/python3.7/dist-packages/tensorflow/python/saved_model/function_deserialization.py](https://localhost:8080/#) in restored_function_body(*args, **kwargs)
    285           .format(index + 1, _pretty_format_positional(positional), keyword))
    286     raise ValueError(
--> 287         "Could not find matching concrete function to call loaded from the "
    288         f"SavedModel. Got:\n  {_pretty_format_positional(args)}\n  Keyword "
    289         f"arguments: {kwargs}\n\n Expected these arguments to match one of the "

ValueError: Exception encountered when calling layer "gcn_10" (type GCN).

Could not find matching concrete function to call loaded from the SavedModel. Got:
  Positional arguments (2 total):
    * [<tf.Tensor 'inputs:0' shape=(111, 108) dtype=float32>,
 <tf.Tensor 'inputs_1:0' shape=(111, 111) dtype=float32>]
    * False
  Keyword arguments: {}

 Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 89, 108), dtype=tf.float32, name='inputs/0'),
 TensorSpec(shape=(None, 89, 89), dtype=tf.float32, name='inputs/1'))
    * False
  Keyword arguments: {}

Option 2:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 89, 108), dtype=tf.float32, name='inputs/0'),
 TensorSpec(shape=(None, 89, 89), dtype=tf.float32, name='inputs/1'))
    * True
  Keyword arguments: {}

Option 3:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 89, 108), dtype=tf.float32, name='input_1'),
 TensorSpec(shape=(None, 89, 89), dtype=tf.float32, name='input_2'))
    * False
  Keyword arguments: {}

Option 4:
  Positional arguments (2 total):
    * (TensorSpec(shape=(None, 89, 108), dtype=tf.float32, name='input_1'),
 TensorSpec(shape=(None, 89, 89), dtype=tf.float32, name='input_2'))
    * True
  Keyword arguments: {}

Call arguments received by layer "gcn_10" (type GCN):
  • args=(['tf.Tensor(shape=(111, 108), dtype=float32)', 'tf.Tensor(shape=(111, 111), dtype=float32)'],)
  • kwargs={'training': 'False'}

How can I save the model so that it works like just after the training?

danielegrattarola commented 1 year ago

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

As the error suggests, you're trying to feed to the model a single graph with no leading batch dimension. You should reshape the features and adjacency matrix so that they have a batch size of 1.