Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to transpose a tensor without using the transpose function in tensorflow?

I have a model that i want to port to tflite micro. However, when i run the code it gives me the following error:

Didn't find op for builtin opcode 'TRANSPOSE' version '2'
Failed to get registration from op code d

I assume that the transpose function is not supported in tflite micro. i also tried replacing it with a PERMUTE layer but it seems that it uses tf.transpose under the hood. Here is the part of my model where i try to traspose:

output = tf.reshape(output, (img_width // B, B, img_height // B, B), name="reshape_in")
output = Permute((2, 1, 3), name="transpose_in")(output)

is there any other way i could perform this transpose without calling tf.transpose? Maybe using reshape?

like image 384
Ahmed Mokhtar Avatar asked Dec 06 '25 20:12

Ahmed Mokhtar


1 Answers

I had a similar issue when trying to run a model with TFLite's GPU delegate, which does not support the transpose operation.

One possibility is to use a combination of strided slices, tf.reshape and concat operations:

shape = (3,4,5)
a = tf.random.uniform(shape)
a_t = tf.transpose(a,(1,0,2)) # permuting first and second axis
a_concat = tf.concat([tf.reshape(a[i:i+1,:,:],(shape[1],1,shape[2])) for i in range(shape[0])],axis=1)
tf.debugging.assert_equal(a_t,a_concat)

Notes:

  • The shape of the tensor needs to be known in advance.
  • it uses tf.concat because PACK/tf.stack is not available on the GPU delegate
  • it uses a[i:i+1,:] instead of a[i] because strided slices that remove an axis are not supported on the GPU delegate
  • Performances are likely to be bad, especially if the dimension looped through is big: what this trick does is essentially unrolling the operation on one dimension, creating as many reshape node in the graph as the size of the dimension.
like image 146
Lescurel Avatar answered Dec 09 '25 23:12

Lescurel