title: tf.data.Dataset does not fetch images from file path using function map
body: <p>Tensorflow throws an error if I use <code>tf.data.Dataset</code> and <code>.map</code> to retrieve images from a file path.</p>
  <p>Reproducible example on Google colab below. I have also <a href="https://colab.research.google.com/drive/1s9d6l17uS-RsIwzQdZnqj8NPVjMSbrAD#scrollTo=sFkgFBN8LpDw" rel="nofollow noreferrer">attached notebook</a> if anyone wants (you'd have to copy to your drive)</p>
  <pre><code>import tensorflow as tf
  import PIL.Image
  import numpy as np
  
  data = np.random.randint(0,255,(28, 28, 3), dtype=np.uint8)
  img = PIL.Image.fromarray(data, 'RGB')
  img.save('abc.png')
  img.save('pqr.png')
  </code></pre>
  <p>Now once I have these images, I am able to normal operations using a tensorflow dataset, but if I try to use this path to fetch an image, it throws a <code> TypeError: expected str, bytes or os.PathLike object, not Tensor</code> error</p>
  <p>For instance, if I try to replace some values in the string (here I am just replacing &quot;abc&quot; with &quot;xyz&quot;), it works fine</p>
  <pre><code>def fn(x1,x2):
    if tf.strings.regex_full_match(x1[0],'.*abc.*'):
      return (tf.strings.regex_replace(x1[0], &quot;abc&quot;, &quot;xyz&quot;),x1[1]),x2
  
    return x1,x2
  
  aa = ['/content/abc.png','/content/abc.png','/content/pqr.png','/content/pqr.png']
  bb = [1,2,3,4]
  cc = [1,2,3,4]
  
  xx = tf.data.Dataset.from_tensor_slices(((aa,bb),cc))
  
  for x in xx.take(-1):
    print(x)
  
  print('#--------')
  
  xx = xx.map(fn)
  
  for x in xx.take(-1):
    print(x)
  
  &gt;&gt;&gt;
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/abc.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/abc.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;)
  #--------
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/xyz.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/xyz.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;)
  </code></pre>
  <p>But if I try to use these paths to fetch the image, the following happens</p>
  <pre><code>def fn(x1,x2):  
    if tf.strings.regex_full_match(x1[0],'.*png'):
      img = tf.keras.preprocessing.image.load_img(x1[0])
      img  = tf.keras.preprocessing.image.img_to_array(img)
      img = tf.cast(img,dtype=tf.float32)
      img = img / 255.
    return (img,x1[1]),x2
  
  
  aa = ['/content/abc.png','/content/abc.png','/content/pqr.png','/content/pqr.png']
  bb = [1,2,3,4]
  cc = [1,2,3,4]
  
  xx = tf.data.Dataset.from_tensor_slices(((aa,bb),cc))
  
  for x in xx.take(-1):
    print(x)
  
  print('#--------')
  
  xx = xx.map(fn)
  for x in xx.take(-1):
    print(x)
  &gt;&gt;&gt;((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/abc.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=1&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/abc.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=2&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=3&gt;)
  ((&lt;tf.Tensor: shape=(), dtype=string, numpy=b'/content/pqr.png'&gt;, &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;), &lt;tf.Tensor: shape=(), dtype=int32, numpy=4&gt;)
  #--------
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-8-6e29d6482ac7&gt; in &lt;module&gt;
       19 print('#--------')
       20 
  ---&gt; 21 xx = xx.map(fn)
       22 for x in xx.take(-1):
       23   print(x)
  
  10 frames
  /usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
      690       except Exception as e:  # pylint:disable=broad-except
      691         if hasattr(e, 'ag_error_metadata'):
  --&gt; 692           raise e.ag_error_metadata.to_exception(e)
      693         else:
      694           raise
  
  TypeError: in user code:
  
      File &quot;&lt;ipython-input-6-6e29d6482ac7&gt;&quot;, line 3, in fn  *
          img = tf.keras.preprocessing.image.load_img(x1[0])
      File &quot;/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image.py&quot;, line 314, in load_img
          target_size=target_size, interpolation=interpolation)
      File &quot;/usr/local/lib/python3.7/dist-packages/keras_preprocessing/image/utils.py&quot;, line 113, in load_img
          with open(path, 'rb') as f:
  
      TypeError: expected str, bytes or os.PathLike object, not Tensor
  </code></pre>
  <p>This seems to because if you print the file path, you get <code>Tensor(&quot;args_0:0&quot;, shape=(), dtype=string)</code>. I understand that the map function is not executed eagerly so it doesn't print specific values. But I'm not able to figure out why I'm not able to use the string to do anything.</p>
  <p>I tried using <code>img = tf.keras.preprocessing.image.load_img(x1[0].numpy())</code>, but this just throws an error saying <code>AttributeError: 'Tensor' object has no attribute 'numpy'</code></p>
  <p>PS: I understand I can use functions like <code>flow_from_directory</code>, but I need to combine images with text and other numerical outputs, and using file paths and <code>tf.data.Dataset</code> is the easiest thing.</p>