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?