Tensorflow 基于给定阈值过滤多个图像预测

Tensorflow 基于给定阈值过滤多个图像预测,tensorflow,Tensorflow,我在根据给定的阈值过滤张量时遇到了一些问题 我有一批包含两个图像: [23003003] 我对该批次进行预测,并收到以下值: 方框[2300,4] 分数[2300] 我想根据阈值过滤分数,比如说0.10,我如何过滤分数,然后过滤相应的框 因此,输出将如下所示: 输出[2,50,4](过滤后剩余50个框时) 提前谢谢。我想这会起作用: import numpy as np filtered_indices=np.where(scores>thresh) filtered_scores=x[f

我在根据给定的阈值过滤张量时遇到了一些问题

我有一批包含两个图像:

[23003003]

我对该批次进行预测,并收到以下值:

方框[2300,4]

分数[2300]

我想根据阈值过滤分数,比如说0.10,我如何过滤分数,然后过滤相应的框

因此,输出将如下所示:

输出[2,50,4](过滤后剩余50个框时)


提前谢谢。

我想这会起作用:

import numpy as np
filtered_indices=np.where(scores>thresh)
filtered_scores=x[filtered_indices]
filtered_boxes=boxes[filtered_indices]

如果您想要TF解决方案,下面的方法应该有效

import tensorflow as tf
import numpy as np

mask = tf.math.greater(scores,0.1)
boxes_above_thresh = tf.boolean_mask(boxes, mask)
scores_above_thresh = tf.boolean_mask(scores, mask)
with tf.Session() as sess:
  res = sess.run([scores_above_thresh, boxes_above_thresh])

说到这里,这将返回一个
(框数,4)
类型数组。也就是说,它不会返回
(2,50,4)
数组,而是返回
(100,4)
数组

编辑:获取带有
(2,x,4)
类型输出的张量

我想你在找tf.ragged_张量。适合你的目的。以下解决方案将产生
tf.raggedtenservalue

mask = tf.math.greater(scores,0.1)
boxes_above_thresh = tf.ragged.boolean_mask(boxes, mask)
scores_above_thresh = tf.ragged.boolean_mask(scores, mask)

with tf.Session() as sess:

  res1, res2 = sess.run([scores_above_thresh, boxes_above_thresh])

是的,这就是问题所在,因为使用此解决方案,我无法检查哪些框属于哪个图像…是否可以保证每个图像都有相同的框?我找到了一种方法来分别获取索引,但合并时遇到了问题,因为形状不同:
index=tf.map\fn(lambda index:tf.where(tf.keras.backend.greater(scores[index,:],threshold)),tf.range(batch_size),dtype=tf.int64)tf.print(tf.shape(index))
@Thomas编辑了我的答案。我想编辑中的解决方案对你有用。