简体   繁体   English

alueError: 层顺序的输入 0 与 3D 自动编码器的层不兼容

[英]alueError: Input 0 of layer sequential is incompatible with the layer for 3D autoenccoder

I have a 3d image (32, 32, 32)in grayscale (is an image taken from a magnetic resonance image) and I'm trying to build a simple Autoencoder with it.我有一个灰度 3d 图像(32、32、32)(是从磁共振图像中拍摄的图像),我正在尝试用它构建一个简单的自动编码器。 The problem i'm getting is when I try to fit the model to the image (model.fit()) because I'm getting this error:我遇到的问题是当我尝试将 model 安装到图像(model.fit())时,因为我收到了这个错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=5, found ndim=4. ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=5,发现 ndim=4。 Full shape received: (32, 32, 32, 1)收到的完整形状:(32、32、32、1)

This is a.nii image.这是一个.nii 图像。 Taken from other posts that ask the same question for conv2d I tried to adapt some answers and I did reshape but I don't know why it's still expecting ndim=5, shouldn't be the ndim 5 the batch dimension that keras add internally??取自对 conv2d 提出相同问题的其他帖子,我尝试调整一些答案并且我确实重塑了但我不知道为什么它仍然期待 ndim=5,不应该是 ndim 5 keras 内部添加的批处理维度? ?

This is what I did:这就是我所做的:

cube = np.array(cube.get_fdata())
cube = cube.reshape(32, 32, 32, 1) 

This is the Autoencoder I built (It's my first time building it and for 3D images, so if there is something wrong with it please let me know):这是我构建的自动编码器(这是我第一次构建它并用于 3D 图像,所以如果有问题请告诉我):

sample_shape = (32, 32, 32, 1)
model = Sequential()
model.add(Conv3D(64, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform', input_shape=sample_shape))
model.add(MaxPooling3D(pool_size=(2, 2, 2), padding='same'))
model.add(Conv3D(32, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), padding='same'))
model.add(Conv3D(16, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), padding='same'))
model.add(Conv3D(8, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), padding='same'))
     
model.add(Conv3D(8, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(UpSampling3D(size=(2, 2, 2)))
model.add(Conv3D(16, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(UpSampling3D(size=(2, 2, 2)))
model.add(Conv3D(32, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(UpSampling3D(size=(2, 2, 2)))
model.add(Conv3D(64, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))
model.add(UpSampling3D(size=(2, 2, 2)))
model.add(Conv3D(3, kernel_size=(3, 3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'))

model.compile(optimizer='adam', loss='mean_squared_error', metrics=['accuracy'])
model.summary()

Thanks!谢谢!

Let's unpack each of the dimensions of your data.让我们解压缩数据的每个维度。

cube = np.array(cube.get_fdata())
cube = cube.reshape(32, 32, 32, 1)
# should be the line below. -1 means infer that number from the data
# cube = cube.reshape(-1, 32, 32, 32, 1)

The three 32s are your 3D data, and the 1 is because there is one channel in the input.三个32是你的3D数据,1是因为输入有一个通道。 You need to add another number before the first 32 to indicate batch size.您需要在前 32 之前添加另一个数字以指示批量大小。 In any neural network, you need to have batch size inside your input.在任何神经网络中,您都需要在输入中包含批量大小。 Keras, PyTorch, or any other ML library does not handle this for you. Keras、PyTorch 或任何其他 ML 库不会为您处理此问题。

Ex.前任。 let's consider the MNIST dataset.让我们考虑一下 MNIST 数据集。 Each number has a shape of (28, 28, 1) but when we run the data through our network, we have to reshape it to (1, 28, 28, 1) .每个数字的形状都是(28, 28, 1)但是当我们通过网络运行数据时,我们必须将其重塑为(1, 28, 28, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM