【问题标题】:Find the intersection of two tensors. Return the sorted, unique values that are in both of the input tensors找到两个张量的交点。返回两个输入张量中的排序唯一值
【发布时间】:2018-01-17 14:23:18
【问题描述】:

您好,这里是 Tensorflow 初学者,

我想删除实现中的任何 numpy 代码,只使用 tensorflow 函数。目前我正在尝试过滤掉背景边界框和置信度低的框。为此,我想要一个名为 keep 的索引,我可以使用它来跟踪要保留哪些框:

# Filter out background boxes
keep = np.where(class_ids > 0)[0]
# Filter out low confidence boxes
if config.DETECTION_MIN_CONFIDENCE:
    keep = np.intersect1d(
        keep, np.where(class_scores >= config.DETECTION_MIN_CONFIDENCE)[0])

class_ids 是一个形状为 (1000,) 的张量,其中每个条目是一个介于 0 和 80 之间的数字,具体取决于类别(总共 81 个类别)。

class_scores 是一个形状为 (1000) 的张量,其中每个条目是对应边界框的类别的概率。

我知道 np.where() 很容易更改为 tf.where,但我怎样才能通过 tensorflow 获得与 np.intersect1d() 相同的功能?

感谢您的帮助。

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    这似乎重复了 numpy.intersect1d 示例。

    import tensorflow as tf
    
    a = tf.constant([3, 1, 2, 1])
    b = tf.constant([1, 3, 4, 3])
    
    # This set appears to be sorted, but that is not documented behavior.
    s = tf.sets.set_intersection(a[None,:], b[None, :])
    fsort = tf.contrib.framework.sort(s.values)
    
    with tf.Session() as sess:
        print(sess.run(s).values)
        print(sess.run(fsort))
    

    这个输出

    [1 3]
    [1 3]
    

    通过一些测试示例,set 函数似乎给出了有序的结果,但我无法验证它是否总是会这样做。因此,您可能想使用 contrib 函数来确定。

    【讨论】:

    • 感谢您的帮助!因为我稍后将 keep 与 tf.gather() 一起使用,所以我认为它实际上不需要排序,但在我的测试中 tf.sets.set_intersection() 也返回了有序结果。我唯一的问题是为什么参数必须是 a[None,:]b[None,:],而不仅仅是 ab
    • 这似乎是因为该集合适用于最后一个维度,而它似乎不适用于一维张量。 [None,:] 语法只是在一维中扩展每个张量。
    猜你喜欢
    • 2021-02-15
    • 2019-08-02
    • 1970-01-01
    • 2022-10-07
    • 1970-01-01
    • 2017-04-01
    • 2020-10-08
    • 2022-01-05
    • 2018-08-30
    相关资源
    最近更新 更多