Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 坐标/地标预测的自定义损耗_Tensorflow_Keras - Fatal编程技术网

Tensorflow 坐标/地标预测的自定义损耗

Tensorflow 坐标/地标预测的自定义损耗,tensorflow,keras,Tensorflow,Keras,我目前正在尝试运行一个里程碑式的预测器,并考虑损失函数 目前,最后一个(密集)层有32个值,16个坐标编码为x1、y1、x2、y2 到目前为止,我只是在摆弄均方误差或平均绝对误差损失,但我认为地面真实值和预测坐标之间的距离将更能体现这些值的正确性 我当前的实现如下所示: def dst_objective(y_true, y_pred): vats = dict() for i in range(0, 16): true_px = y_true[:, i * 2:

我目前正在尝试运行一个里程碑式的预测器,并考虑损失函数

目前,最后一个(密集)层有32个值,16个坐标编码为x1、y1、x2、y2

到目前为止,我只是在摆弄均方误差或平均绝对误差损失,但我认为地面真实值和预测坐标之间的距离将更能体现这些值的正确性

我当前的实现如下所示:

def dst_objective(y_true, y_pred):
    vats = dict()
    for i in range(0, 16):
        true_px = y_true[:, i * 2:i * 2 + 1]
        pred_px = y_pred[:, i * 2:i * 2 + 1]
        true_py = y_true[:, i * 2 + 1:i * 2 + 2]
        pred_py = y_pred[:, i * 2 + 1:i * 2 + 2]
        vats[i] = K.sqrt(K.square(true_px - pred_px) + K.square(true_py - pred_py))
    out = K.concatenate([
        vats[0], vats[1], vats[2], vats[3], vats[4], vats[5], vats[6], vats[7],
        vats[8], vats[9], vats[10], vats[11], vats[12], vats[13], vats[14],
        vats[15]
    ],axis=1)
    return K.mean(out,axis=0)

当我对它进行评估时,它似乎确实起了作用,但在我看来,它确实“有问题”。有什么建议可以改进吗?

相同的计算表示为Keras中的张量运算,不分离X和Y坐标,因为这基本上是不必要的:

# get all the squared difference in coordinates
sq_distances = K.square( y_true - y_pred )

# then take the sum of each pair
sum_pool = 2 * K.AveragePooling1D( sq_distances,
                                   pool_size = 2,
                                   strides = 2,
                                   padding = "valid" )

# take the square root to get the distance
dists = K.sqrt( sum_pool )

# take the mean of the distances
mean_dist = K.mean( dists )

我真的很喜欢这个。谢谢!这假设输出格式是[[x1,y1],[x2,y2]…],对吗?因为到目前为止我一直使用[x1,y1,x2,y2…],不,它假设你使用它的方式是[x1,y1,x2,y2…]。上面参数化的一维卷积将把相邻的卷积连接起来。您可能必须添加大小为1的第0个dim,因为AveragePoolg1d也需要批处理维度,但不确定。