层“3dcnn”需要 1 个输入,但它收到了 16 个 > 输入张量

问题描述 投票:0回答:2

我有一个关于 keras 深度学习的问题。我编写了一个自定义数据生成器,因为我的内存不足,并且我需要加载 x x x 样本进行训练,因为我使用的是大尺寸的漂亮图像。我尝试了该论坛的几种解决方案,但由于它们是 3d 图像,因此无法在我的模型中使用。问题出在训练命令(fit)抛出错误:

ValueError:层“3dcnn”需要 1 个输入,但它收到了 16 个输入 输入张量。收到的输入:[]

代码如下:

def get_model(width=208, height=150, depth=50):
"""Build a 3D convolutional neural network model."""

inputs = keras.Input((width, height, depth, 1))

x = layers.Conv3D(filters=64, kernel_size=3, activation="relu")(inputs)
x = layers.MaxPool3D(pool_size=2)(x)
x = tf.keras.layers.BatchNormalization()(x)

x = layers.Conv3D(filters=64, kernel_size=3, activation="relu")(x)
x = layers.MaxPool3D(pool_size=2)(x)
x = tf.keras.layers.BatchNormalization()(x)

x = layers.Conv3D(filters=128, kernel_size=3, activation="relu")(x)
x = layers.MaxPool3D(pool_size=2)(x)
x = tf.keras.layers.BatchNormalization()(x)

x = layers.Conv3D(filters=256, kernel_size=3, activation="relu")(x)
x = layers.MaxPool3D(pool_size=2)(x)
x = tf.keras.layers.BatchNormalization()(x)

x = layers.GlobalAveragePooling3D()(x)
x = tf.keras.layers.Dense(units=512, activation="relu")(x)
x = layers.Dropout(0.3)(x)

outputs = tf.keras.layers.Dense(units=3, activation="softmax")(x)

# Define the model.
model = keras.Model(inputs, outputs, name="3dcnn")
return model



#Get ALL the training images to batch/split/iterate from batch size to batch size 
train_data_generator = CustomDataGenerator(
    batch_size = 16, 
    #dataset_directory = "E:\\NIFTI_train_codegenerator"
    dataset_directory = "NIFTI_train_codegenerator"
)
 
# get a batch of images
train_images,labels = next(iter(train_data_generator))

#validation_split=0.2,
epochs = 100
model.fit(
    train_images,
    labels,
    batch_size=16,
    epochs=epochs,
    shuffle=True,
    verbose=2,
    callbacks=[checkpoint_cb, early_stopping_cb],
)
tensorflow keras deep-learning
2个回答
0
投票

谢谢您的回答。如果我直接用 train_data_generator 提供它,我会得到同样的错误,甚至更糟。

#For each file in the batch size
for id in batch_IDs:
  path = os.path.join(self.directory, id,"la_4ch.nii.gz")
  #read the file nifty
  image = process_scan(path)
  image = np.expand_dims(image, axis = 0)
  #append the image and label
  images.append(np.array(image))


 model.fit(
     train_data_generator,
     batch_size=16,
     epochs=epochs,
     shuffle=True,
     verbose=2,
     callbacks=[checkpoint_cb, early_stopping_cb], )

ValueError:层“3dcnn”需要 1 个输入,但它收到了 16 个输入 张量。收到的输入:[


0
投票

解决方案是这样的:

#images must be returned as a single tensor otherwise error
#stack makes many inputs in a single one
return np.stack(images,axis=0),labels
© www.soinside.com 2019 - 2024. All rights reserved.