WEEK two ALPACA assignment

I passed the alpaca/not alpaca test.But I don’t understand why, when I want to check an image to see if it contains alpaca or not, model2 predicts a value above 1. Is that correct, or what is the problem?

import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

img = Image.open('dataset/alpaca/0cb5cae66bb9c4cd.jpg')
img = img.resize(IMG_SIZE)
x = np.array(img)

preds = model2.predict(np.array([x]))

if preds[0][0]>0.5:
    print('Alpaca')
else:
    print("not Alpaca")
plt.imshow(img)
plt.show()

Incorrectly predicting the class of an image should remind you of courses 2 and 3. A model is allowed to make mistakes on unseen data. That said, there is a missing image normalization step (dividing each pixel by 255). Pre-processing steps should be the same when training and during inference.

1 Like

Hello @Areeg_Fahad,

If you go back to exercise 2 and check the output layer you have added to model2, you will see that we have never asked you to use sigmoid as activation. Therefore, the output layer does not produce a probability but something called “logit”. This is also why we have seen from_logits=True is being used throughout the assignment. While a probability ranges from 0 to 1, a logit is unbounded and can be any number. To convert a logit back to a probability, you will need to apply the sigmoid function to the logit.

If you want to find out more about why we have chosen the model to predict a logit, here is a video from the MLS which explains that.

Cheers,
Raymond

1 Like

without using sigmoid How can I know which image is an alpaca?

It seems you have used a=0.5 as your threshold. I will let you find out the answer yourself, but here are two guiding questions that can help you:

  1. given a sigmoid function a = sigmoid(z), where a is probability and z is logit, at what value of the logit z will the resulting a be equal to 0.5?

  2. What is the condition for z to always produce a a larger than 0.5?

Raymond

Finally, I got it.

The output is Z, not A.

and any z value above 0 is more than 0.5.

Thank you @rmwkwok