100Large
100Large

Reputation: 63

How do I interpret tf.keras.Model.predict() output?

I am having trouble finding the documentation I need on this. To summarize the issue, I have trained a tf.keras model using two classes of images, labeled as '0' or '1'. I now want to use this model to predict whether new images are a '0' or '1'. My question is as follows: model.predict() returns a number between 1 and 0, but I can't seem to find what exactly this is. Is it correct to say that this is it's prediction (ie, closer to 1 means the image is likely a 1, and closer to 0 means the image is likely a 0)? Or is there something else going on here. I have included the code, and some output, below. In this case, is pred the probability the image is a 1, and 1 - pred the probability the image is a 0?

Thanks for any and all help.

for img_path in test_filenames:
   img = tf.keras.preprocessing.image.load_img(img_path, target_size=(IMAGE_SIZE,IMAGE_SIZE))
   img_array = tf.keras.preprocessing.image.img_to_array(img)
   img_array = tf.expand_dims(img_array, 0)
   pred = model.predict(img_array)
   print(pred)

Returns

[[0.8361757]]
[[0.26765466]]
[[0.2722953]]
[[0.81938094]]
[[0.24995388]]
[[0.45974937]]

Upvotes: 2

Views: 4790

Answers (1)

jkr
jkr

Reputation: 19250

is pred the probability the image is a 1, and 1 - pred the probability the image is a 0?

Yes, that is correct. If you want to get hard class (i.e., 0 or 1), then you can threshold the output. 0.5 is a common threshold, but I have also seen 0.3. This is something you can tune.

pred = model.predict(img_array)
classes = pred > 0.5

The predictions are between 0 and 1 most likely because the last activation of the model is a sigmoid function.

Upvotes: 1

Related Questions