**
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)