我正在尝试在Google colab上训练卷积神经网络来解决医学分类问题。数据集是用于训练的89 256x256x256图像和用于测试的11图像。当我尝试进行模型训练时,会出现以下错误:
import keras
from keras import optimizers
import keras.models
from keras.models import Sequential
import keras.layers
from keras.layers.convolutional import Conv3D
from keras.layers.convolutional import MaxPooling3D
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Dense
from keras import metrics
model = Sequential()
model.add(Conv3D(64, kernel_size=(3,3,3),
activation='relu',
input_shape=(10,1,256,256,256)))
model.add(Conv3D(64, (2,2,2), activation='relu'))
model.add(MaxPooling3D(pool_size=(2,2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
opt=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)
model.compile(opt, loss='categorical_crossentropy', metrics=['mae','acc'])
model.fit(x=train_data, y=train_labels,epochs=100, batch_size=10, verbose=2 ,callbacks=None, validation_split=0.0, validation_data=(validation_data,validation_labels), shuffle=True)
这是我得到的错误:
ValueError:输入0与conv3d_56层不兼容:预期ndim = 5,找到的ndim = 6
答案 0 :(得分:0)
假设您使用的是第一个data_format通道,那么您对第一个Conv3D图层的input_shape
推荐应为(CHANNELS, HEIGHT, WIDTH, DEPTH)
。但是您的输入形状元组的长度为5,而这不是Conv3D层所期望的。假设错误指定了batch_size(of 10),则进行以下更改应该可以解决问题
model.add(Conv3D(64, kernel_size=(3,3,3),
activation='relu',
input_shape=(1,256,256,256)))
如果您使用channels_last数据格式,则您的input_shape
应该是(HEIGHT, WIDTH, DEPTH, CHANNELS)
。并假设您的图片有1个频道,上面的行应该是
model.add(Conv3D(64, kernel_size=(3,3,3),
activation='relu',
input_shape=(256,256,256, 1)))