每天学一点tensorflow——tf.image.non_max_suppress(Non-max suppression)与K.gather()


tf.image.non_max_suppression(
    boxes,
    scores,
    max_output_size,
    iou_threshold=0.5,
    name=None
)
  • 参数说明:

  • boxes: 2维张量 [num_boxes, 4].

  • scores: 一维张量 [num_boxes],代表每个boxes的分数/概率.

  • max_output_size: 非极大值抑制最多输出boxes的数量.

  • iou_threshold: IOU阈值,大于阈值被删除

  • name: (optional).

  • 返回的是selected_indices:表示的是一个1-D的整数张量,大小为[M],代表的是选出来的留下来的边框下标,M小于等于max_output_size.

K.gather(reference,selected_indices)
  • 参数说明:

  • reference:给定的张量.

  • selected_indices: tf.image.non_max_suppress的返回值selected_indices,表示要查询的元素下标.

  • 返回值对要查询元素selected_indices对应的reference.

    nms_indices = tf.image.non_max_suppression(
    boxes,
    scores,
    max_boxes,
    iou_threshold)
##在给定的张量中搜索给定下标的向量,scores,boxes,classes为给定张量
    scores = K.gather(scores, nms_indices)
    boxes = K.gather(boxes, nms_indices)
    classes = K.gather(classes, nms_indices)

例子如下:

import numpy as np
import tensorflow as tf
from keras import backend as K
a = [[1,2],[3,4],[5,6],[7,8]]
b = [[0,4],[0,4],[1,5],[2,6],[3,7]]
sess =tf.Session()

with tf.Session() as sess:
    print(sess.run(K.gather(a,b)))

输出结果如下:

[[1 2]
 [1 2]
 [3 4]
 [5 6]
 [7 8]]

当a为张量时,更好理解:重点理解下面的张量

例子:

import numpy as np
import tensorflow as tf
from keras import backend as K  
a = tf.constant([[2,4,4,1],
[6,8,2,1]],tf.float32)

#b = tf.reduce_max(a,-1)
c=  np.array([True, True])
print("a="+str(sess.run(a)))
#print("b="+str(sess.run(b)))
print("c="+str(c))
d=tf.boolean_mask(a,c)#重点博客days2有讲解https://blog.csdn.net/qq_29444571/article/details/84574526
print("d="+str(d.eval(session=sess)))
d
b=tf.constant([0,0,1],tf.int32)
print('K.gather ='+str(sess.run(K.gather(a,b))))
print('K.gather ='+str(sess.run(K.gather(d,b))))

输出结果:

a=[[2. 4. 4. 1.]
 [6. 8. 2. 1.]]
c=[ True  True]
d=[[2. 4. 4. 1.]
 [6. 8. 2. 1.]]
K.gather =[[2. 4. 4. 1.]
 [2. 4. 4. 1.]
 [6. 8. 2. 1.]]
K.gather =[[2. 4. 4. 1.]
 [2. 4. 4. 1.]
 [6. 8. 2. 1.]]

认真注意区别!!!!

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值