Home > Software design >  How to convert tensorflow.python.data.ops.dataset_ops.BatchDataset to a numpy array?
How to convert tensorflow.python.data.ops.dataset_ops.BatchDataset to a numpy array?

Time:11-01

I am having an image dataset as

|-Train
| |-Defective
  | |-images
| |-Not_Defective
  | |-images

I preprocessed these images using the following function

dir='../input/railwaytrackv4/Dataset _ Railway Track Fault Detection-20210713T183411Z-001/Dataset _ Railway Track Fault Detection/Train'
train_data=tf.keras.utils.image_dataset_from_directory(directory=dir,
                                                        labels='inferred',
                                                        batch_size=32,
                                                        image_size=(256, 256))

It gave output as Found 1469 files belonging to 2 classes.

And type(train_data) = tensorflow.python.data.ops.dataset_ops.BatchDataset

How to convert this train_data to a numpy array?

CodePudding user response:

tf.keras.utils.image_dataset_from_directory returns a tf.data.Dataset which is a fancy generator and it yields values as you would expect in python, the only difference being that it yields tensorflow Tensor objects so you just need to convert them to numpy objects using numpy() method

x, y = next(train_data)
x = x.numpy()
y = y.numpy()
for x, y in train_data:
   x = x.numpy()
   y = y.numpy()

CodePudding user response:

enter image description here

I have 1470 pictures in my training set but it's showing only 29 images.

  • Related