title: Is there an alternative to tf.py_function() for custom Python code?
body: <p>I have started using TensorFlow 2.0 and have a little uncertainty with regard to one aspect.</p>
  
  <p>Suppose I have this use case: while ingesting data with the <code>tf.data.Dataset</code> I want to apply some specific augmentation operations upon some images. However, the external libraries that I am using <strong>require</strong> that the <strong>image is a numpy array</strong>, <strong>not a tensor</strong>.</p>
  
  <p>When using <code>tf.data.Dataset.from_tensor_slices()</code>, the flowing data needs to be of type Tensor. Concrete example:</p>
  
  <pre><code>def my_function(tensor_image):
     print(tensor_image.numpy()
     return
  
  
  data = tf.data.Dataset.from_tensor_slices(tensor_images).map(my_function)
  </code></pre>
  
  <p>The code above does not work yielding an </p>
  
  <blockquote>
    <p>'Tensor' object has no attribute 'numpy' error.</p>
  </blockquote>
  
  <p>I have read the documentation on TensorFlow 2.0 stating that if one wants to use an arbitrary python logic, one should use <code>tf.py_function</code> <strong>or only TensorFlow primitives</strong> according to:
  <a href="https://stackoverflow.com/questions/56075037/how-to-convert-tensor-to-numpy-array-in-tensorflow">How to convert &quot;tensor&quot; to &quot;numpy&quot; array in tensorflow?</a> </p>
  
  <p><strong>My question is the following</strong>: Is there another way to use arbitrary python code in a function with a custom decorator/an easier way than to use <code>tf.py_function</code>?</p>
  
  <p>To me honestly it seems that there must be a more elegant way than passing to a <code>tf.py_function</code>, transforming to a numpy array, perform operations A,B,C,D and then retransform to a tensor and yield the result. </p>