Reputation: 21
my code:
import cv2 import tensorflow as tf
CATEGORIES=['Dog','Cat']
def prepare(filepath):
IMG_SIZE=50
img_array = cv2.imread(filepath,cv2.IMGREAD_GRAYSCALE)
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
model = tf.keras.models.load_model("64x3-CNN.model")
prediction = model.predict([prepare('dog.jpg')]) print(prediction)
my error :
TypeError Traceback (most recent call last)
<ipython-input-8-182101876678> in <module>
10 return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
11
---> 12 model = tf.keras.models.load_model("64x3-CNN.model")
13
14 prediction = model.predict([prepare('dog.jpg')])
TypeError: 'str' object is not callable
Upvotes: 2
Views: 5084
Reputation: 535
Just to complement other answers. The sanity checks are:
tf.keras.models.load_model
, check if the path is the correct folder or file. You can use both of them, just be sure to be consistent in saving and loading. For example, if you are saving as a folder, load as a folder.If those steps are OK, you can also have trouble if you have custom objects in the model. For me, it worked only by specifying them in the load function to avoid this error. For example:
class SomeCustomLayer(tf.keras.layers.Layer):
# their definition ...
loaded-model = tf.keras.models.load_model("path_to_folder/trained_model", custom_objects={"SomeCustomLayer": SomeCustomLayer})
Upvotes: 1
Reputation: 989
For me, it originated with this error when building the model:
UserWarning: The `keras.initializers.serialize()` API should only be used for objects of type `keras.initializers.Initializer`. Found an instance of type <class 'method'>, which may lead to improper serialization.
This prevented it from being saved properly
Upvotes: 0
Reputation: 135
I had same error. Actually I tried to save with .keras and .h5 extensions. When I was tryin to use tf.keras.models.load_model() or tf.saved_model.load() it would give this error. Seeing Anjul Tyagi's answer inspired me to save the model just as a folder (without writing an extension in the saving path) and then loaded it again just as a folder. And it worked. So, I wrote not ("model.keras") or ("model.h5") but just ("model").
Btw I never got this error while saving and then loading models I wrote myself. This time I used hub.KerasLayer() and I think that's what caused the error. So, bottom line: saved and loaded the model as a folder. Hope this helps someone.
Upvotes: 1
Reputation: 478
I faced the same error while loading a saved pb
model in TensorFlow. Turns out that I didn't save the model using Keras, so had to load the model using tf.saved_model.load('model_directory')
instead of using Keras.
Upvotes: 4
Reputation:
May be these are the factors to produce this error....
In your case, Your extension of the model is wrong since there is no extenison like model" Check Your extension of the model. It may "h5" https://www.tensorflow.org/tutorials/keras/save_and_load#hdf5_format
Upvotes: 0