小编Yel*_*Kyu的帖子

如何在 tensorflow 或 keras 中使用 scipy.optimize.linear_sum_assignment?

第一次在这里发帖!如果我的问题缺少任何内容,请告诉我,我会解决它!

Facebook 最近发布了 DETR,一个使用 Transformer 的对象检测模型!该模型是用 Pytorch 实现的,我正在尝试实现包含匈牙利算法的损失函数,但使用 Keras 和 Tensorflow 作为 Keras 模型的自定义损失函数。在 Facebook 的原始实现中,它是https://github.com/facebookresearch/detr/blob/master/models/matcher.py 中的第 81-82 行

为了使用 numpy 和经典的 python 函数,我使用了:

    def hungarian_loss(losses):
        row_ind, col_ind = linear_sum_assignment(losses)
        idx = [[i, j] for i, j in zip(row_ind, col_ind)]
        return idx

    # dist loss is a 5x5 matrix, and idx is 5x2 indexes
    idx = tf.py_function(func=hungarian_loss, inp=[dist_loss], Tout=tf.int32)
    min_val = tf.gather_nd(dist_loss, idx)
    return K.mean(min_val)
Run Code Online (Sandbox Code Playgroud)

但我得到了:

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Inner dimensions of output shape must match inner dimensions of updates …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

5
推荐指数
1
解决办法
909
查看次数

标签 统计

keras ×1

python ×1

tensorflow ×1