keras:值错误:找不到可以处理输入的数据适配器

ax6ht2ek  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(228)

我有一个深度学习模型,我试图用简单的输入来测试它。

history = model.fit(X_train, y_train, epochs=10, validation_data=(X_valid, y_valid))

我收到此错误:

Traceback (most recent call last):
  File "/usr/lib64/python3.6/contextlib.py", line 99, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/miran045/reine097/projects/ResNet34/venv/lib64/python3.6/site-packages/tensorflow/python/ops/variable_scope.py", line 2833, in variable_creator_scope
    yield
  File "/home/miran045/reine097/projects/ResNet34/venv/lib/python3.6/site-packages/keras/engine/training.py", line 1148, in fit
    steps_per_execution=self._steps_per_execution)
  File "/home/miran045/reine097/projects/ResNet34/venv/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 1383, in get_data_handler
    return DataHandler(*args, **kwargs)
  File "/home/miran045/reine097/projects/ResNet34/venv/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 1137, in __init__
    adapter_cls = select_data_adapter(x, y)
  File "/home/miran045/reine097/projects/ResNet34/venv/lib/python3.6/site-packages/keras/engine/data_adapter.py", line 979, in select_data_adapter
    _type_name(x), _type_name(y)))
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'numpy.ndarray'>"}), (<class 'list'> containing values of types {"<class 'int'>"})
python-BaseException

下面是代码:

from functools import partial

import numpy as np
import tensorflow as tf
from tensorflow import keras

DefaultConv3D = partial(keras.layers.Conv3D, kernel_size=3, strides=1,
                        padding="SAME", use_bias=False)

class ResidualUnit(keras.layers.Layer):
    def __init__(self, filters, strides=1, activation="relu", **kwargs):
        super().__init__(**kwargs)
        self.activation = keras.activations.get(activation)
        self.main_layers = [
            DefaultConv3D(filters, strides=strides),
            keras.layers.BatchNormalization(),
            self.activation,
            DefaultConv3D(filters),
            keras.layers.BatchNormalization()]
        self.skip_layers = []
        if strides > 1:
            self.skip_layers = [
                DefaultConv3D(filters, kernel_size=1, strides=strides),
                keras.layers.BatchNormalization()]

    def call(self, inputs):
        Z = inputs
        for layer in self.main_layers:
            Z = layer(Z)
        skip_Z = inputs
        for layer in self.skip_layers:
            skip_Z = layer(skip_Z)
        return self.activation(Z + skip_Z)

def get_model():
    model = keras.models.Sequential()
    model.add(DefaultConv3D(64, kernel_size=7, strides=2,
                            input_shape=[1, 182, 218, 182]))
    model.add(keras.layers.BatchNormalization())
    model.add(keras.layers.Activation("relu"))
    model.add(keras.layers.MaxPool3D(pool_size=3, strides=2, padding="SAME"))
    prev_filters = 64
    for filters in [64] * 3 + [128] * 4 + [256] * 6 + [512] * 3:
        strides = 1 if filters == prev_filters else 2
        model.add(ResidualUnit(filters, strides=strides))
        prev_filters = filters
    model.add(keras.layers.GlobalAvgPool3D())
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(1))

    return model

def run():
    model = get_model()
    model.compile(loss="mean_squared_error", optimizer="adam", metrics=[tf.keras.metrics.MeanSquaredError()])

    x1 = np.random.rand(182, 218, 182)
    x2 = np.random.rand(182, 218, 182)
    x3 = np.random.rand(182, 218, 182)
    x4 = np.random.rand(182, 218, 182)
    x5 = np.random.rand(182, 218, 182)
    x6 = np.random.rand(182, 218, 182)
    X_train = [x1, x2, x3]
    y_train = [2]
    X_valid = [x4]
    y_valid = [2]
    X_test = [x5]
    y_test = [3]
    history = model.fit(X_train, y_train, epochs=10, validation_data=(X_valid, y_valid))
    print(history)
    score = model.evaluate(X_test, y_test)
    print(score)
    X_new = x6
    y_pred = model.predict(X_new)
    print(y_pred)

if __name__ == '__main__':
    run()

我在这里做错了什么?我如何转换我的训练和验证数据,使其工作。

hfwmuf9z

hfwmuf9z1#

您的x数据和y数据必须是数组,并且具有相同的样本数。下面是一个示例:

model = get_model()
model.compile(loss="mean_squared_error", optimizer="adam", metrics=[tf.keras.metrics.MeanSquaredError()])

x1 = np.random.rand(1, 182, 218, 182)
x2 = np.random.rand(1, 182, 218, 182)
x3 = np.random.rand(1, 182, 218, 182)
X_train = np.stack([x1, x2, x3])

y_train = np.array([2, 2, 2])
history = model.fit(X_train, y_train, epochs=1)

X_train具有3个样本,并且y_train也具有3个样本。

相关问题