8

I have started using TensorFlow 2.0 and have a little uncertainty with regard to one aspect.

Suppose I have this use case: while ingesting data with the tf.data.Dataset I want to apply some specific augmentation operations upon some images. However, the external libraries that I am using require that the image is a numpy array, not a tensor.

When using tf.data.Dataset.from_tensor_slices(), the flowing data needs to be of type Tensor. Concrete example:

def my_function(tensor_image):
   print(tensor_image.numpy()
   return


data = tf.data.Dataset.from_tensor_slices(tensor_images).map(my_function)

The code above does not work yielding an

'Tensor' object has no attribute 'numpy' error.

I have read the documentation on TensorFlow 2.0 stating that if one wants to use an arbitrary python logic, one should use tf.py_function or only TensorFlow primitives according to: How to convert "tensor" to "numpy" array in tensorflow?

My question is the following: Is there another way to use arbitrary python code in a function with a custom decorator/an easier way than to use tf.py_function?

To me honestly it seems that there must be a more elegant way than passing to a tf.py_function, transforming to a numpy array, perform operations A,B,C,D and then retransform to a tensor and yield the result.

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59

1 Answers1

9

There is no other way of doing it, because tf.data.Datasets are still (and they will always be, I suppose, for performance reasons) executed in graph mode and, thus, you cannot use anything outside of the tf.* methods, that can be easily converted by TensorFlow to its graph representation.

Using tf.py_function is the only way to mix Python execution (and thus, you can use any Python library) and graph execution when using a tf.data.Dataset object (on the contrary of what happens when using TensorFlow 2.0, that being eager by default allow this mixed execution naturally).

nessuno
  • 26,493
  • 5
  • 83
  • 74
  • kindly, does eager in tf 2.6 allow the mix between python and graph executions? coz when i try to convert tensor to numpy() during model building, it throws an error saying 'Tensor' object has no attribute 'numpy'. Please advise. – ALI Q SAEED Nov 15 '21 at 23:57
  • 1
    No, it's forbidden. It's impossible to call the .numpy() method on a Tensor,l if this tensor is being executed in graph mode. A graph is built implicitly when you define a keras model, and explicitly when you decore a function with tf.function. – nessuno Nov 16 '21 at 11:17
  • 1
    so, is there an alternative way to extract feature maps righ after each conv layer to do computations on them then convert them back to tensors to feed them to the next layer in the model ? please if this is possible, let me know, coz i fed up searching for that and no hope – ALI Q SAEED Nov 17 '21 at 07:45
  • to make it clear, kindly, have a look at the question i posted in stackoverflow https://stackoverflow.com/questions/69890585/how-to-convert-kerastensor-to-numpy-array-and-vise-versa-during-model-building – ALI Q SAEED Nov 17 '21 at 07:51