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?