0

我有一个用于相机姿势网络的 ResNet 网络。我已经用 1024 密集层替换了最后的分类器层,然后是 7 密集层(前 3 个用于 xyz,最后 4 个用于四元数)。

我的问题是我想将 xyz 误差和四元数误差记录为两个单独的误差或指标(而不是仅仅意味着所有 7 个的绝对误差)。customer_error(y_true,y_pred) 的自定义度量模板的输入是张量。我不知道如何将输入分成两个不同的 xyz 和 q 数组。该函数在编译时运行,此时张量为空且没有任何 numpy 组件。

最终我想得到中位数 xyz 和 q 误差使用

中值 = tensorflow_probability.stats.percentile(输入,q=50,插值='线性')。

任何帮助将非常感激。

4

1 回答 1

0

您可以使用tf.slice()仅提取模型输出的前三个元素。


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]])>

可以将这样的内容包装到您的自定义指标中以获得您需要的内容。

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

于 2019-12-19T12:39:29.667 回答