【发布时间】:2025-12-29 11:50:12
【问题描述】:
我正在尝试创建一个过滤器掩码,通过比较它们各自的哪个值更大来从向量中删除重复的索引。
我目前的做法是:
- 将 3-D 索引转换为 1-D
- 检查一维索引的唯一性
- 计算每个唯一索引的最大值
- 将最大值与原始值进行比较。如果存在相同的值,请保留该 3-D 索引。
我想获得一个过滤器数组,这样我也可以将boolean_mask 应用于其他张量。对于此示例,掩码应如下所示:
[False True True True True].
除非值本身也重复,否则我当前的代码类型可以正常工作。但是,当我使用它时似乎就是这种情况,因此我需要找到更好的解决方案。
这是我的代码外观示例
import tensorflow as tf
# Dummy Input values with same Structure as the real
x_cells = tf.constant([1,2,3,4,1], dtype=tf.int32) # Index_1
y_cells = tf.constant([4,4,4,4,4], dtype=tf.int32) # Index_2
iou_index = tf.constant([1,2,3,4,1], dtype=tf.int32) # Index_3
iou_max = tf.constant([1.,2.,3.,4.,5.], dtype=tf.float32) # Values
# my Output should be a mask that is [False True True True True]
# So if i filter this i get e.g. x_cells = [2,3,4,1] or iou_max = [2.,3.,4.,5.]
max_dim_y = tf.constant(10)
max_dim_x = tf.constant(20)
num_anchors = 5
stride = 32
# 1. Transforming the 3D-Index to 1D
tmp = tf.stack([x_cells, y_cells, iou_index], axis=1)
indices = tf.matmul(tmp, [[max_dim_y * num_anchors], [num_anchors],[1]])
# 2. Looking for unique / duplicate indices
y, idx = tf.unique(tf.squeeze(indices))
# 3. Calculating the maximum values of each unique index.
# An function like unsorted_segment_argmax() would be awesome here
num_segments = tf.shape(y)[0]
ious = tf.unsorted_segment_max(iou_max, idx, num_segments)
iou_max_length = tf.shape(iou_max)[0]
ious_length = tf.shape(ious)[0]
# 4. Compare all max values to original values.
iou_max_tiled = tf.tile(iou_max, [ious_length])
iou_reshaped = tf.reshape(iou_max_tiled, [ious_length, iou_max_length])
iou_max_reshaped = tf.transpose(iou_reshaped)
filter_mask = tf.reduce_any(tf.equal(iou_max_reshaped, ious), -1)
filter_mask = tf.reshape(filter_mask, shape=[-1])
如果我们简单地将开头的iou_max 变量的值更改为:
x_cells = tf.constant([1,2,3,4,1], dtype=tf.int32)
y_cells = tf.constant([4,4,4,4,4], dtype=tf.int32)
iou_index = tf.constant([1,2,3,4,1], dtype=tf.int32)
iou_max = tf.constant([2.,2.,3.,4.,5.], dtype=tf.float32)
【问题讨论】:
标签: python numpy tensorflow indexing tensor