Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to efficiently vectorize Tensorflow ops on images?

Tensorflow has a great deal of transformations that can be applied to 3D-tensors representing images ([height, width, depth]) like tf.image.rot90() or tf.image.random_flip_left_right() for example.

I know that they are meant to be used with queues hence the fact that they operate on only one image.

But would there be a way to vectorize the ops to transform 4D-tensor ([batch_size,height,width,depth]) to same size tensor with op applied image-wise along the first dimension without explicitely looping through them with tf.while_loop()?

(EDIT : Regarding rot90() a clever hack taken from numpy rot90 would be to do:

rot90=tf.reverse(x,tf.convert_to_tensor((False,False,True,False)))
rot90=tf.transpose(rot90,([0,2,1,3])

EDIT 2: It turns out this question has already been answered quite a few times (one example) it seems map_fn is the way to go if you want an optimized version. I had already seen it but I had forgotten. I guess this makes this question a duplicate...

However for random op or more complex op it would be nice to have a generic method to vectorize existing functions...)

like image 963
jeandut Avatar asked Nov 07 '16 08:11

jeandut


People also ask

What is retracing TensorFlow?

Retracing, which is when your Function creates more than one trace, helps ensures that TensorFlow generates correct graphs for each set of inputs.

What is @TF function?

tf. function is a decorator function provided by Tensorflow 2.0 that converts regular python code to a callable Tensorflow graph function, which is usually more performant and python independent. It is used to create portable Tensorflow models.

Are tensors immutable?

All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one.


1 Answers

Try tf.map_fn.

processed_images = tf.map_fn(process_fn, images)
like image 84
yuefengz Avatar answered Oct 27 '22 01:10

yuefengz