"AttributeError: 'str' object has no attribute 'predict'". I am developing garbage detector model and this is the error

from tensorflow import keras
from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np

model = './ C:/Parth/cs11/converted_keras/saved_model.h5'


data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

image = Image.open('C:/cs11/pic.jpg')

size = (224, 224)
image = ImageOps.fit(image, size, Image.ANTIALIAS)

image_array = np.asarray(image)

normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

data[0] = normalized_image_array

prediction = model.predict(data)
print(prediction)

And the error i am receiving is

  File "cs11.py", line 22, in <module>
    prediction = model.predict('data')
AttributeError: 'str' object has no attribute 'predict'

I am not able to understand why it is happening so even if data is an np.ndarray. Could someone please tell me a way around this? Or an alternative method for it?


Solution 1:

you specified the path to the model. Now you must load the model

model_path=r'./ C:/Parth/cs11/converted_keras/saved_model.h5'
model = keras.models.load_model(model_path)

now do predictions. You can check if the model loaded correctly with

print (model.summary())

This should print out the model summary