NoobCoder
NoobCoder

Reputation: 21

Why am I getting a 'str' objet is not callable error?

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

Answers (5)

K. Bogdan
K. Bogdan

Reputation: 535

Just to complement other answers. The sanity checks are:

  1. Check the correct path to the model. As you are using 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.
  2. Check if the method you are using to load the model is the correct one for the saved model. More information on this can be found here, for instance.

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

user3433489
user3433489

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

Alena
Alena

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

Anjul Tyagi
Anjul Tyagi

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

user14135319
user14135319

Reputation:

May be these are the factors to produce this error....

  1. You saved the model different name.
  2. You saved the model in different directory with correct name or incorrect name
  3. your extension is wrong

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

Related Questions