【问题标题】:Keras. Failed to find data adapter. Custom generator喀拉斯。找不到数据适配器。自定义生成器
【发布时间】: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


【解决方案1】:

@Dr.Snoopy 谢谢,我确实 pip 卸载了 keras 并替换了

from keras.layers

from tensorflow.keras.layers

在其他模块中。现在工作正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 2012-08-24
    • 2014-04-14
    • 2019-11-10
    • 2014-07-21
    • 2017-12-14
    相关资源
    最近更新 更多