【发布时间】: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