tensorflow 一些操作记录

**

0 在tensorflow-gpu==1.8.0下训练的weights在1.9.0和1.14.0上运行不了gpu模式。。默认跑的纯cpu模式,真的慢。

**

sess=tf.InteractiveSession()

1 移除全0行

x=tf.convert_to_tensor([[1,0,3],[0,0,0],[4,5,6]])
dim=tf.shape(x)[1]
c = tf.reduce_sum(tf.abs(x), axis=1)  # [N,1]
index_vector = tf.transpose(tf.not_equal(c, tf.zeros((1, 1))))  # N,1
index_matrix = tf.reshape(tf.tile(index_vector, [1, dim]), [-1, dim])  # N,5
gtbox_and_label = tf.reshape(tf.boolean_mask(x, index_matrix), [-1, dim])
Out[1]: 
array([[1, 0, 3],
       [4, 5, 6]], dtype=int32)

2 将数据扩充一维

c=tf.convert_to_tensor([[1,2,3],[4,5,6]])
N=3 #扩充3次
 # shape 从(2,3) 变成 (3,2,3)
tf.reshape(tf.tile(c,[N,1]),[N,c.shape[0],c.shape[1]])
Out[2]: 
array([[[1, 2, 3],
        [4, 5, 6]],
       [[1, 2, 3],
        [4, 5, 6]],
       [[1, 2, 3],
        [4, 5, 6]]], dtype=int32) 
 

3 变成0维

images = tf.placeholder(tf.string,shape=[1,],name='images')
images= tf.reshape(images,[])

4 按条件取值 《划重点》

假设a b c的维度分别是[N,4] ,[N,2]
我现在要按照提取出b的第二维上值大于0.5的index,从而得到筛选之后的a b

#方法一 自己写 
thres= 0.5
index_vector = tf.transpose(tf.greater(b, thres * tf.ones([1, 1])))  # N,1
index_matrix = tf.reshape(tf.tile(index_vector, [1, 4]), [-1, 4])  # N,4
a1 = tf.reshape(tf.boolean_mask(a, index_matrix), [-1, 4])
b1 = tf.boolean_mask(b, tf.greater(b, ct))
#上面代码写成python就是
a = a[ b[:,1] > thres, :]  # [N,5]
b = b[ b[:,1] > thres]  # [N,]
#方法2 用tf.gather_nd 
# tf.nn.top_k 似乎

5 tf的NMS

a=tf.convert_to_tensor([[1,1,4,4],[1,1,5,5],[2,2,5,5],[1,1,4,4]])
b=tf.convert_to_tensor([0.6,0.7,0.8,0.9])
keep = tf.image.non_max_suppression(boxes=a,scores=b,max_output_size=500,iou_threshold=0.5)
# keep.eval=array([3, 2, 1], dtype=int32)
final_boxes = tf.gather(a, keep)
final_probs = tf.gather(b, keep)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值