Separating custom keras metric inputs into two seperate metrics and finding median error

183 Views Asked by At

I have a ResNet network that I am using for a camera pose network. I have replaced the final classifier layer with a 1024 dense layer and then a 7 dense layer (first 3 for xyz, final 4 for quaternion).

My problem is that I want to record the xyz error and the quaternion error as two separate errors or metrics (Instead of just mean absolute error of all 7). The inputs of the custom metric template of customer_error(y_true,y_pred) are tensors. I don't know how to separate the inputs into two different xyz and q arrays. The function runs at compile time, when the tensors are empty and don't have any numpy components.

Ultimately I want to get the median xyz and q error using

median = tensorflow_probability.stats.percentile(input,q=50, interpolation='linear').

Any help would be really appreciated.

1

There are 1 best solutions below

0
On

You could use the tf.slice() to extract just the first three elements of your model output.


import tensorflow as tf
# enabling eager mode to demo the slice fn
tf.compat.v1.enable_eager_execution()
import numpy as np
# just creating a random array dimesions size (2, 7)
# where 2 is just an arbitrary value chosen for the batch dimension
out = np.arange(0,14).reshape(2,7)
print(out)
# array([[ 0,  1,  2,  3,  4,  5,  6],
#        [ 7,  8,  9, 10, 11, 12, 13]])
# put it in a tf variable
out_tf = tf.Variable(out)
# now using the slice operator
xyz = tf.slice(out_tf, begin=[0, 0], size=[-1,3])
# lets see what it looked like
print(xyz)
# <tf.Tensor: id=11, shape=(2, 3), dtype=int64, numpy=
# array([[0, 1, 2],
#       [7, 8, 9]])>

Could wrap something like this into your custom metric to get what you need.

def xyz_median(y_true, y_pred):
  """get the median of just the X,Y,Z coords

  UNTESTED though :)
  """
  # slice to get just the xyz
  xyz = tf.slice(out_tf, begin=[0, 0], size=[-1,3])
  median = tfp.stats.percentile(xyz, q=50, interpolation='linear')
  return median