Issue
I have a pandas dataframe and of its columns is "bbox" with value i.e. [[94.0, 58.0, 469.0, 362.0]]
. I want to convert this dataframe to a custom dataset with tf.data.Dataset.from_tensor_slices.
I want the bbox element to have a shape of (None,4) but it is created with shape (1,4) tf.Tensor([[ 94. 58. 469. 362.]], shape=(1, 4), dtype=float32)
and I don't know what I am doing wrong.
My dataset is created with this code:
myimages = pd.DataFrame.from_dict(train_data).to_dict("list")
myimages = tf.data.Dataset.from_tensor_slices(myimages)
Thanks everyone in advance for your time
Solution
You can just use tf.data.Dataset.map
and tf.squeeze
to get rid of the extra dimension:
import tensorflow as tf
import pandas as pd
train_data = {'names': ['some_image.jpg', 'other_image.jpg'],
'bbox': [[[94.0, 58.0, 469.0, 362.0]], [[94.0, 58.0, 469.0, 362.0]]]}
df = pd.DataFrame(train_data)
myimages = tf.data.Dataset.from_tensor_slices((df['names'].to_numpy(), df['bbox'].to_list()))
myimages = myimages.map(lambda x, y: (x, tf.squeeze(y, axis=0)))
for x, y in myimages:
print(x, y)
tf.Tensor(b'some_image.jpg', shape=(), dtype=string) tf.Tensor([ 94. 58. 469. 362.], shape=(4,), dtype=float32)
tf.Tensor(b'other_image.jpg', shape=(), dtype=string) tf.Tensor([ 94. 58. 469. 362.], shape=(4,), dtype=float32)
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.