2

I am trying to use tensorflow tf.data to build pipline.

import tensorflow as tf
import numpy as np
import cv2

There are 6 images with name as 1.jpeg,2.jpeg upto 6.jpeg. I want to load the image using map() function of tf.data.

tfds = tf.data.Dataset

filename = [str(i)+'.jpeg' for i in range(1,7)]
label = [i for i in range(1,7)]

dataset = tfds.from_tensor_slices((filename,label))

data = dataset.batch(2,drop_remainder=True)

list(data.map(lambda x,y:x))

The output of the above code is :

[<tf.Tensor: shape=(2,), dtype=string, numpy=array([b'1.jpeg', b'2.jpeg'], dtype=object)>,
 <tf.Tensor: shape=(2,), dtype=string, numpy=array([b'3.jpeg', b'4.jpeg'], dtype=object)>,
 <tf.Tensor: shape=(2,), dtype=string, numpy=array([b'5.jpeg', b'6.jpeg'], dtype=object)>]

Now I am trying to read the image using the map() function of tf.data as

list(data.map(lambda x,y:cv2.imread(x)))

Here I got an error:

---------------------------------------------------------------------------
StagingError                              Traceback (most recent call last)
<ipython-input-8-b0c6910f376c> in <module>()
----> 1 list(data.map(lambda x,y:cv2.imread(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'):
--> 692           raise e.ag_error_metadata.to_exception(e)
    693         else:
    694           raise

StagingError: in user code:

    File "<ipython-input-8-b0c6910f376c>", line 1, in None  *
        lambda x,y:cv2.imread(x)))

    SystemError: <built-in function imread> returned NULL without setting an error

How I can read the image using tf.data using the map() function.

DextroLaev
  • 106
  • 6
  • I think the error is due to the byte format of the string. – DextroLaev May 15 '22 at 16:48
  • Hi @DextroLaev, `imread` expects a filepath but you are passing a tensor to it, hence the error. Kindly try again by providing the correct path to the files. Thank you! –  Oct 26 '22 at 07:33

0 Answers0