【问题标题】:How to convert tensorflow.python.data.ops.dataset_ops.BatchDataset to a numpy array?如何将 tensorflow.python.data.ops.dataset_ops.BatchDataset 转换为 numpy 数组?
【发布时间】:2021-12-14 23:52:45
【问题描述】:

我有一个图像数据集

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

我使用以下函数对这些图像进行了预处理

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))

它的输出为Found 1469 files belonging to 2 classes.

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

如何将此train_data 转换为 numpy 数组?

更新:

我试过了

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

但它给出了以下输出


2021-11-01 08:48:15.079479: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
2021-11-01 08:48:25.085070: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:175] Filling up shuffle buffer (this may take a while): 250 of 11760
2021-11-01 08:48:35.132351: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:175] Filling up shuffle buffer (this may take a while): 558 of 11760
2021-11-01 08:48:45.122079: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:175] Filling up shuffle buffer (this may take a while): 843 of 11760
2021-11-01 08:48:55.135867: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:175] Filling up shuffle buffer (this may take a while): 1160 of 11760
2021-11-01 08:49:05.080678: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:175] Filling up shuffle buffer (this may take a while): 1455 of 11760
2021-11-01 08:49:05.657894: I tensorflow/core/kernels/data/shuffle_dataset_op.cc:228] Shuffle buffer filled.
2021-11-01 08:49:05.665031: W tensorflow/core/framework/cpu_allocator_impl.cc:80] Allocation of 1155268608 exceeds 10% of free system memory.

注意: 找到属于 2 个类的 1469 个文件。

【问题讨论】:

    标签: python-3.x numpy keras tensorflow2.0 image-recognition


    【解决方案1】:

    tf.keras.utils.image_dataset_from_directory 返回一个 tf.data.Dataset,这是一个花哨的生成器,它产生的值与你在 python 中所期望的一样,唯一的区别是它产生 tensorflow Tensor 对象,所以你只需要将它们转换为 numpy对象使用numpy()方法

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

    编辑:

    数据集是批处理的,这意味着您将始终分批读取文件。当您定义 tf.keras.utils.image_dataset_from_directory 数据集时,您可能指定了参数 batch_size=29。如果您想一次读取整个数据集,您可以使用 batch_size=735 但请注意 tensorflow 的 Dataset 旨在用作驱动器的生成器。如果您可以将数据集放入内存中,那么您最好自己阅读文件,例如使用tf.keras.utils.load_img

    【讨论】:

    • 我猜这不是解决问题,我已经发布了截图
    • @SamarPratapSingh 我编辑了我的答案,如果您还有其他问题,请告诉我
    • 现在显示内存溢出错误。
    • 可能是因为您的数据集不适合您的内存,这就是使用批处理数据集生成器的原因。
    • 它们占用的空间是否比 numpy 数组少?
    猜你喜欢
    • 2021-12-18
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2016-07-01
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多