看代码,若网络预测出了10个box=[b1,b2,b3,,b10]对应10个scores=·[s1,s2,s3,...s10]=[0.1,0.2,0.3,0.4,..., 1],10个labels=[0,1,2,3,4,0,1,2,0,2] #就是网络检测种类有5种目标
step1 ,对于scores,过滤出得分大于阈值=0.05的那些box,记录其索引indices=[5,6,7,8,9]
step2: 根据indices 找出对应的lables[0,1,2,0,2]
step3:将indices和indices拼接在一起形成新的indices
indices=[[5,0], [6,1],
[7,2],
[8,0],
[9,2]]
具体见程序
def _filter_detections(scores, labels):
# threshold based on score
indices = backend.where(keras.backend.greater(scores, score_threshold))#筛那些得分低于0.05的目标
print('nms',nms)
# print('indices',indices)
# tf.print(indices)
if nms:
filtered_boxes = backend.gather_nd(boxes, indices)
filtered_scores = keras.backend.gather(scores, indices)[:, 0]
# perform NMS 应用NMS二次筛选, 调用了tensorflow.image.non_max_suppression执行最大值抑制,最多输出的300个
nms_indices = backend.non_max_suppression(filtered_boxes, filtered_scores, max_output_size=max_detections, iou_threshold=nms_threshold)
# filter indices based on NMS
indices = keras.backend.gather(indices, nms_indices)#找出经过极大值抑制之后保留的box的索引号
# add indices to list of all indices
# print('labels',labels)
labels = backend.gather_nd(labels, indices)#找出经过极大值抑制之后保留的labels的索引号
indices = keras.backend.stack([indices[:, 0], labels], axis=1)#这里把索引值与label的具体值放在一起,比如indices=[0,1,3]
#labels=[8,9,10],则stack之后indices=[[0 8]
# [1 9]
# [3 10]]
# print('len(indices',indices)
return indices#z这里的indices除了有满足要求的(就是得分大于阈值的)的那些索引,还有这个索引所对应的类别信息
然后根据上面的indices可以轻松解写出需要的labels
labels= indices[:, 1]#这里就会显示出为什么需要在_filter_detections函数中,最后一行程序中,把labels又给stack上去,
备注:当然了,也可以不stack,根据初始的indices用gather_nd去labels中取需要的label也可以