0

I'm following a tutorial from Sentdex, however I tried to load my saved model from a new file (run_test.py) and ran into the following error.

ValueError: Could not find matching function to call loaded from the SavedModel. 
Got:
 Positional arguments (1 total):
  * Tensor("inputs:0", shape=(None, 28, 28), dtype=uint8)
 Keyword arguments: {}

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

Option 1:
 Positional arguments (1 total):
  * TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name='inputs')
 Keyword arguments: {}

main.py

import tensorflow as tf
import numpy as np

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())  
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) 
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu)) 
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'] ) 
model.fit(x_train, y_train, epochs=1) # run the training process 3 times

val_loss, val_acc = model.evaluate(x_test, y_test)

model.save('num_reader_basic.model')

run_test.py

import tensorflow as tf
import numpy as np

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()

new_model = tf.keras.models.load_model('num_reader_basic.model')
predictions = new_model.predict(x_test)
print(np.argmax(predictions[0]))

When running the load command in the same file as the model training file (main.py) it does not cause any error, it only causes an error when ran from a separate file. Is there any mistakes in my second file (run_test.py) or are there any other methods in loading a saved model from a new file?

  • 1
    I was not able to reproduce the error when running your code samples. Which TensorFlow versions are you using? Similar kind of issues I found: https://stackoverflow.com/questions/58575586/could-not-find-matching-function-to-call-loaded-from-the-savedmodel https://github.com/tensorflow/tensorflow/issues/37339 It could have something to do with the TF version. – sakumoil Jul 08 '21 at 12:32
  • Yup, I think it has something to do with my tensorflow since I am running on M1 chip. The architecture cause issues since Python for M1 is shipped with 2 architecture. Thanks for your suggestion, I may not look into my program version if you don't point it out. – Ungku Amer Jul 08 '21 at 17:45

1 Answers1

0

The problem does not come from the code itself but is caused by the M1 architecture and the TensorFlow installation. Do note that the original TensorFlow does not work well with the new chip, so a separate version installation is needed.

There are many methods of installing the arm64 version of TensorFlow but this one works for me.