Search code examples
pythontensorflowdatasetmapping

How to convert image and mask path dataframe to Images in tensorflow?


Data frame Creation

# Creating DataFrame of image and mask
all_val_img = sorted([os.path.join(VAL_DIR,i) for i in os.listdir(VAL_DIR)])
all_val_mask = sorted([os.path.join(VAL_MASK_DIR,i) for i in os.listdir(VAL_MASK_DIR)])

#DataFrame
val_data_df = pd.DataFrame(zip(all_val_img,all_val_mask), columns = ['photos', 'mask'])

I have Data Frame that looks like this(below). and I want to create a tensor dataset out of it.

    photos  mask
4691    dataset/val2017/000000546556.jpg    dataset/panoptic_val2017/000000546556.png
1191    dataset/val2017/000000140286.jpg    dataset/panoptic_val2017/000000140286.png
3041    dataset/val2017/000000351823.jpg    dataset/panoptic_val2017/000000351823.png
2552    dataset/val2017/000000294163.jpg    dataset/panoptic_val2017/000000294163.png
3070    dataset/val2017/000000356169.jpg    dataset/panoptic_val2017/000000356169.png

I Converted the data frame into tensor data. and want to map function to make them image.

val_data = tf.data.Dataset.from_tensor_slices(val_data_df)

so wrote a function to map on the dataset.but it did not work.

def make_it_image(image, label):
    image_raw = tf.io.read_file(image)
    image = tf.image.decode_image(image_raw)

    label_raw = tf.io.read_file(label)
    label = tf.image.decode_image(label_raw)

    # normalize
    image = image /255
    label = label /255

    return image, label

when I mapped the function. Result was

val_data = val_data.map(make_it_image).cache().batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)

Error :

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-72-c6fd8ebb8233> in <module>
----> 1 val_data = val_data.map(make_it_image).cache().batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)

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

TypeError: in user code:


    TypeError: tf__make_it_image() missing 1 required positional argument: 'label'

OR

tell me how to create a dataset from two image directories one as image, one as mask?


Solution

  • Combine the data

    val_data = tf.data.Dataset.from_tensor_slices((np.array(all_val_img),
     np.array(all_val_mask)))
    

    map the dataset

    
    def make_image(x,y):
        image = tf.io.read_file(x)
        image = tf.image.decode_png(image, channels=3)
        
        image = image/255
    
        label = tf.io.read_file(y)
        label = tf.image.decode_png(label, channels=3)
        label = label/255
    
        return image,label
    
    
    val_data = val_data.map(make_image)
    

    and it works

    val_data
    # <MapDataset element_spec=(TensorSpec(shape=(None, None, 3), dtype=tf.float32, name=None), TensorSpec(shape=(None, None, 3), dtype=tf.float32, name=None))>