Search code examples
pythontensorflowkeras

Keras model won't use predict correctly


I have a Keras model that I have trained and evaluated and even tested. Now I am trying to use three test images into the model.

I run the images through a preprocessor which is the same one I used to make the training data. I then do the exact same thing to the single images that I did for the testing data. But it gives me an error of

Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 2 arrays:

I don't know what is wrong with it.

So this is how I test the model successfully.

y_pred = []
y_true = []
for i in range(0, len(test_x1)):
    x1 = test_x1[i]
    x2 = test_x2[i]
    x1 = np.expand_dims(x1, axis=0)
    x2 = np.expand_dims(x2, axis=0)
    y_true.append(np.argmax(test_y[i]))
    pred = model.predict([x1, x2])
    y_pred.append(make_binary(pred))

This is the preprocessing method I used for both images

def create_features(file, image_dir, base_model):
    img_path = os.path.join(image_dir, file)
    img = image.load_img(img_path, target_size=(224, 224))
    img = image.img_to_array(img)
    x = resnet50.preprocess_input(img)
    x = np.array([x])
    feature = base_model.predict(x)
    return feature

And this is the way I am processing the new images:

IMAGE_DIR = 'Data'
img1 = 'test1.jpg'
img2 = 'test2.jpg'
img3 = 'test3.jpg'

img1_feat = create_features(img1, IMAGE_DIR, model)
img2_feat = create_features(img2, IMAGE_DIR, model)
img3_feat = create_features(img3, IMAGE_DIR, model)

Now when I look at the two features they are the same.

x1 = test_x1[0]
x1 = np.expand_dims(x1, axis=0)
print(x1.shape)
print(type(x1))

print(img1_feat.shape)
print(type(img1_feat))
(1, 1, 1000)
<class 'numpy.ndarray'>
(1, 1, 1000)
<class 'numpy.ndarray'>

And then I try to make a prediction from it

pred1 = model.predict([img1_feat, img2_feat])

But that results in an error.


Solution

  • I figured out what was wrong thanks to @Matias Valdenegro and @Mukul I was doing this in an Ipython notebook and after a few epochs go around I found out that on occasion the model gets overwritten by an imported resent model from another class.

    Thanks to everyone for the help. I didn't think about using the model.summary() as I didn't really think that it has changed.