我有一个2通道的图像类文件,我从中切割补丁作为卷积自编码器的训练/验证数据集。我使用TensorFlow的自定义数据生成器为每个批处理和epoch使用不同的数据。
这是我的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
线训练。
tensorflow =2.5.0, keras =2.4.3
您的__getitem__
方法必须返回X, Y对。为什么你要返回X,而不是X ?
PS:您可以使用on_epoch_end
。此函数将在每个epoch结束时由fit方法调用。