1

I have a deep learning model that I'm trying to test with simple input. On this line:

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

I am getting this error:

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

Here is the code:

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()

What am I doing wrong here? How can I transform my training and validation data so that this works.

1 Answer 1

1

Your x data as well as your y data need to be arrays and have the same number of samples. Here is an example:

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 has 3 samples and so does y_train.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.