Home > Software design >  Create Tensorflow Dataset with dataframe of images and labels
Create Tensorflow Dataset with dataframe of images and labels

Time:05-26

I want to create a dataset with tensorflow and feed this with images as array (dtype=unit8) and labels as string. The images and the according labels are stored in a dataframe and the columns named as Image as Array and Labels.

Image as Array (type = array) Labels (type = string)
img_1 'ok'
img_2 'not ok'
img_3 'ok'
img_4 'ok'

My challenge: I don't know how to feed the Dataset out of a dataframe, the most tutorials prefer the way to load the data from a directory.

Thank you in forward and I hope you can help me to load the images in the dataset.

CodePudding user response:

You can actually pass a dataframe directly to tf.data.Dataset.from_tensor_slices:

import tensorflow as tf
import numpy as np
import pandas as pd


df = pd.DataFrame(data={'images': [np.random.random((64, 64, 3)) for _ in range(100)],
                        'labels': ['ok', 'not ok']*50})

dataset = tf.data.Dataset.from_tensor_slices((list(df['images'].values), df['labels'].values)).batch(2)

for x, y in dataset.take(1):
  print(x.shape, y)
# (2, 64, 64, 3) tf.Tensor([b'ok' b'not ok'], shape=(2,), dtype=string)

CodePudding user response:

One of possibility is to use range to create index dataset and then map array and label together.

# array
img = np.random.rand(4, 2, 2, 2)
label = np.array(['ok', 'not ok', 'ok', 'ok'])

# convert to tf constant
img = tf.constant(img)
label = tf.constant(label)

# create dataset with 0 - 3 index
dataset = tf.data.Dataset.range(len(label))
# map dataset
dataset = dataset.map(lambda x: (img[x, :, :, :], label[x]))

output:

<MapDataset element_spec=(TensorSpec(shape=(2, 2, 2), dtype=tf.float64, name=None), TensorSpec(shape=(), dtype=tf.string, name=None))>

output list- second idx:

for i in dataset:
    print(list(i)[1])

tf.Tensor(b'ok', shape=(), dtype=string)
tf.Tensor(b'not ok', shape=(), dtype=string)
tf.Tensor(b'ok', shape=(), dtype=string)
tf.Tensor(b'ok', shape=(), dtype=string)
  • Related