Week 4 >>> Face_Recognition programming exercise >>> Exercise 2 - verify

In [[[Week 4 >>> Face_Recognition programming exercise >>> Exercise 2 - verify]]] I got this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-97-166ca75f1224> in <module>
      1 # BEGIN UNIT TEST
----> 2 assert(np.allclose(verify("images/camera_1.jpg", "bertrand", database, FRmodel), (0.54364836, True)))
      3 assert(np.allclose(verify("images/camera_3.jpg", "bertrand", database, FRmodel), (0.38616243, True)))
      4 assert(np.allclose(verify("images/camera_1.jpg", "younes", database, FRmodel), (1.3963861, False)))
      5 assert(np.allclose(verify("images/camera_3.jpg", "younes", database, FRmodel), (1.3872949, False)))

<__array_function__ internals> in allclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in allclose(a, b, rtol, atol, equal_nan)
   2157 
   2158     """
-> 2159     res = all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
   2160     return bool(res)
   2161 

<__array_function__ internals> in isclose(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/numeric.py in isclose(a, b, rtol, atol, equal_nan)
   2255     y = array(y, dtype=dt, copy=False, subok=True)
   2256 
-> 2257     xfin = isfinite(x)
   2258     yfin = isfinite(y)
   2259     if all(xfin) and all(yfin):

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

I figured out the error came from the dist calculation, and I managed to solve it using numpy() to convert the tf tensor to a numpy array and np.asscalar() to convert the numpy array to a scalar, as shown below, but it seems like a very patchy solution.

dist = np.asscalar(((tf.norm( tf.subtract(encoding, database[identity]), axis=-1 )).numpy()))

How did others write the dist line to avoid that error?

Thanks for your time

Jaime

No need to use any tf functions there, because none of the inputs or outputs are tensors.
Use np.linalg.norm() and plain-vanilla subtraction.
That’s what the instructions say to use also.

2 Likes

I did not have any trouble getting this to work using either all TF (tf.subtract, tf.norm) or all numpy (normal subtract plus np.linalg.norm). I think the problem is the axis = -1 on your tf.norm. That is an error because it tells it to compute the norms across the last dimension, leaving you with a 1D tensor instead of a scalar. Just remove that and strip out all the numpy and np references there and it should work.

1 Like

Yes, when I do it your way, here’s the beginning of the output, which you didn’t show:

Dist for bertrand = tf.Tensor([0.54364824], shape=(1,), dtype=float32)
It's bertrand, welcome in!
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-02e2f1840f11> in <module>
----> 1 assert(np.allclose(verify("images/camera_1.jpg", "bertrand", database, FRmodel), (0.54364836, True)))
      2 assert(np.allclose(verify("images/camera_3.jpg", "bertrand", database, FRmodel), (0.38616243, True)))
      3 assert(np.allclose(verify("images/camera_1.jpg", "younes", database, FRmodel), (1.3963861, False)))
      4 assert(np.allclose(verify("images/camera_3.jpg", "younes", database, FRmodel), (1.3872949, False)))
      5 

<__array_function__ internals> in allclose(*args, **kwargs)

The key thing to notice there is that the shape of dist is (1,), meaning that it is a “rank 1” tensor and not a scalar. Here’s what it looks like when I remove the “axis” argument:

Dist for bertrand = tf.Tensor(0.54364824, shape=(), dtype=float32) It's bertrand, welcome in! Dist for bertrand = tf.Tensor(0.38616234, shape=(), dtype=float32) It's bertrand, welcome in! Dist for younes = tf.Tensor(1.3963861, shape=(), dtype=float32) It's not younes, please go away Dist for younes = tf.Tensor(1.3872949, shape=(), dtype=float32) It's not younes, please go away Dist for younes = tf.Tensor(0.5992949, shape=(), dtype=float32) It's younes, welcome in!

Out[19]:

See the difference? It’s perfectly fine for it to be a tensor, but it has to be of the correct shape.

But as Tom mentioned above, you can do the whole thing with straight numpy and no TF at all and it also works fine.

1 Like