0
votes

I am testing the code to process row by row of a tensor.

The tensor may have a row with the last 4 elements are 0 or with non-zero values.

If the row has 0 for the last 4 elements [1.0,2.0,2.3,3.4,0,0,0,0] The last four are removed and shape is changed to 5 elements in the row. The first element represents the row index. It becomes like [0.0,1.0,2.0,2.3,3.4].

If the row has all 8 elements with non-zero values, then split into two rows and put row index in the first place. Then [3.0,4.0,1.0,2.1,1.2,1.4,1.2,1.5] becomes like [[2.0,3.0,4.0,1.0,2.1],[2.0,1.2,1.4,1.2,1.5]]. The first element 2.0 is row index in tensor.

So after processing [[1.0,2.0,2.3,3.4,0,0,0,0],[2.0,3.2,4.2,4.0,0,0,0,0],[3.0,4.0,1.0,2.1,1.2,1.4,1.2,1.5],[1.2,1.3,3.4,4.5,1,2,3,4]] becomes

[[0,1.0,2.0,2.3,3.4],[1.0,2.0,3.2,4.2,4.0],[2.0,3.0,4.0,1.0,2.1],[2.0,1.2,1.4,1.2,1.5],[3.0,1.2,1.3,3.4,4.5],[3.0,1,2,3,4]]

I did as follow. But error as TypeError: TypeErro...pected',) at map_fn.

    import tensorflow as tf

    boxes = tf.constant([[1.0,2.0,2.3,3.4,0,0,0,0],[2.0,3.2,4.2,4.0,0,0,0,0],[3.0,4.0,1.0,2.1,1.2,1.4,1.2,1.5],[1.2,1.3,3.4,4.5,1,2,3,4]])
    rows = tf.expand_dims(tf.range(tf.shape(boxes)[0], dtype=tf.int32), 1)
    def bbox_organize(box, i):
       if(tf.reduce_sum(box[4:]) == 0):
          box=tf.squeeze(box, [5,6,7]
          box=tf.roll(box, shift=1, axis=0)
          box[0]=i
       else:
          box=tf.reshape(box, [2, 4])
          const_=tf.constant(i, shape=[2, 1])
          box=tf.concat([const_, box], 0)
       return box
    b_boxes= tf.map_fn(lambda x: (bbox_organize(x[0], x[1]), x[1]), (boxes, rows), dtype=(tf.int32, tf.int32))
    with tf.Session() as sess: print(sess.run(b_boxes))

I am not good at Tensorflow and still learning.

Is there better way of implementing Tensorflow apis to process it?

1

1 Answers

1
votes

What you need to do is transform the shape of boxes and get non-zero lines

import tensorflow as tf
tf.enable_eager_execution();

boxes = tf.constant([[1.0,2.0,2.3,3.4,0,0,0,0],[2.0,3.2,4.2,4.0,0,0,0,0],[3.0,4.0,1.0,2.1,1.2,1.4,1.2,1.5],[1.2,1.3,3.4,4.5,1,2,3,4]])

boxes = tf.reshape(boxes,[tf.shape(boxes)[0]*2,-1])
# [[1.  2.  2.3 3.4]
#  [0.  0.  0.  0. ]
#  [2.  3.2 4.2 4. ]
#  [0.  0.  0.  0. ]
#  [3.  4.  1.  2.1]
#  [1.2 1.4 1.2 1.5]
#  [1.2 1.3 3.4 4.5]
#  [1.  2.  3.  4. ]]

rows = tf.floor(tf.expand_dims(tf.range(tf.shape(boxes)[0]), 1)/2)
# [[0.]
#  [0.]
#  [1.]
#  [1.]
#  [2.]
#  [2.]
#  [3.]
#  [3.]]

add_index = tf.concat([tf.cast(rows,tf.float32),boxes],-1)

index = tf.not_equal(tf.reduce_sum(add_index[:,4:],axis=1),0)
# [ True False  True False  True  True  True  True]

boxes_ = tf.gather_nd(add_index,tf.where(index))
print(boxes_)

# tf.Tensor(
# [[0.  1.  2.  2.3 3.4]
#  [1.  2.  3.2 4.2 4. ]
#  [2.  3.  4.  1.  2.1]
#  [2.  1.2 1.4 1.2 1.5]
#  [3.  1.2 1.3 3.4 4.5]
#  [3.  1.  2.  3.  4. ]], shape=(6, 5), dtype=float32)

I think you need the above code more. The vectorization method will be significantly faster than the tf.map_fn().