【发布时间】:2021-08-24 02:44:12
【问题描述】:
我正在尝试使用自定义生成器来拟合我的 keras 模型。错误:
Failed to find data adapter that can handle input: <class 'custom_generator.MyCustomGenerator'>, <class 'NoneType'>
我正在尝试使用的代码(尝试了 fit 和 fit_generator 两种方法)
my_gen = MyCustomGenerator(path_to_dataset, batch_size=10)
model.fit(my_gen, verbose=1, epochs=10, validation_freq=0.1, callbacks=tensorboard_callback)
这是我的自定义生成器类。
我试过导入 tensorflow.keras.utils.Sequence
和
tensorflow.python.keras.utils.data_utils.Sequence - 仍然没有结果
import math
import tensorflow as tf
import numpy as np
import glob
from process_image import process_images
import keras
class MyCustomGenerator(tf.keras.utils.Sequence):
def __init__(self, path_to_data, batch_size):
self.path_to_data = path_to_data
self.batch_size = batch_size
self.files = []
for file in glob.glob(f'{path_to_data}/*.npy'):
self.files.append(file)
def __len__(self):
return math.ceil(len(self.files) / self.batch_size)
def __getitem__(self, idx):
if idx * self.batch_size % 500 == 0:
data = np.load(self.files[int(idx // (500 / self.batch_size))], allow_pickle=True)
self.data = data
batch_x = self.data[idx * self.batch_size: (idx + 1) * self.batch_size, 0]
batch_y = self.data[idx * self.batch_size: (idx + 1) * self.batch_size, 1]
#batch_x = process_images(batch_x)
return np.array(batch_x), np.array(batch_y)
keras 2.4.3
张量流 2.5.0rc1
在这个帖子中发现了非常相似的问题
Keras fit generator - ValueError: Failed to find data adapter that can handle input
但解决方案对我不起作用
如果有任何帮助,我将不胜感激
【问题讨论】:
-
看看你的导入,你正在混合从 tf.keras 和 keras 导入,它们不是同一个库,会产生像这样的奇怪错误。
标签: python tensorflow keras error-handling generator