【问题标题】:Custom DataGenerator tensorflow error 'ValueError: Failed to find data adapter that can handle input'自定义 DataGenerator tensorflow 错误“ValueError:无法找到可以处理输入的数据适配器”
【发布时间】:2021-10-19 03:59:41
【问题描述】:

我有一个 2 通道 imagelike 文件,我从中切割补丁作为卷积自动编码器的训练/验证数据集。我正在使用 TensorFlow 的自定义数据生成器为每个批次和时期使用不同的数据。

这是我的CustomDataGenerator 课程:

class CustomDataGenerator(tf.keras.utils.Sequence):

    def __init__(self, file, sample_size, batch_size=32, width=28, height=28, resolution=(28, 28)):
        'Initialization'
        self.sample_size = sample_size
        self.batch_size = batch_size
        self.resolution = resolution
        self.width = width
        self.height = height


    def __len__(self):
        'Denotes the number of batches per epoch'
        return int(np.floor(self.sample_size / self.batch_size))

    def __getitem__(self, index):
        'Generate one batch of data'

        batch = []
        for i in range(self.batch_size):
           ....

        x = np.asarray(batch)
        x = tf.transpose(x, [0, 2, 3, 1])
        return x, x

和培训代码:

...
    train_gen = data_generator.CustomDataGenerator(file=file, sample_size=10000)
    val_gen = data_generator.CustomDataGenerator(file=file, sample_size=2000)
    history = autoencoder.fit(train_gen, epochs=100, validation_data=val_gen)
...

当我运行它抛出的代码时:

ValueError: Failed to find data adapter that can handle input: <class 'data_generator.CustomDataGenerator'>, <class 'NoneType'>

在训练期间在model.fit 行中。

张量流 ==2.5.0,keras ==2.4.3

【问题讨论】:

  • 你在哪里定义data_generator
  • 与训练文件在同一文件夹中
  • 您是否从tensorflow.keras.* 而非keras.* 导入了所有层和函数?有时混合使用这些库会导致问题。
  • 这是我对 data_generator.py 的导入部分: import numpy as np import keras import utils import tensorflow as tf import h5py from numpy import random
  • import keras改成from tensorflow import keras再检查一遍。

标签: tensorflow keras autoencoder


【解决方案1】:

您的 __getitem__ 方法必须返回 X、Y 对。你为什么要返回 X, X?

然后将 train_gen 传递给 model.fit() 进行训练。该错误是由于您将 X 作为 Y 参数发送到您的 model.fit()

PS:你可以使用on_epoch_end。该函数将在每个 epoch 结束时被 fit 方法调用。

【讨论】:

  • 我正在使用自动编码器的数据生成器,其中输入 X 和标签 y 相同
  • 顺便说一句,在不执行 autoencoder.fit() 的情况下运行 next(train_gen) 会得到什么?
  • TypeError: 'CustomDataGenerator' 对象不是迭代器
猜你喜欢
  • 2020-01-12
  • 2020-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-30
  • 2020-10-14
  • 2023-03-30
  • 2022-06-22
相关资源
最近更新 更多