gpt4 book ai didi

python - Keras 中的 Segnet : total size of new array must be unchanged error

转载 作者:太空狗 更新时间:2023-10-30 02:25:37 27 4
gpt4 key购买 nike

我正在用 Python 实现 Segnet。以下是代码。

img_w = 480
img_h = 360
pool_size = 2

def build_model(img_w, img_h, pool_size):
n_labels = 12

kernel = 3

encoding_layers = [
Conv2D(64, (kernel, kernel), input_shape=(img_h, img_w, 3), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(64, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
MaxPooling2D(pool_size = (pool_size,pool_size)),

Convolution2D(128, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(128, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
MaxPooling2D(pool_size = (pool_size,pool_size)),

Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
MaxPooling2D(pool_size = (pool_size,pool_size)),

Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
MaxPooling2D(pool_size = (pool_size,pool_size)),

Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
MaxPooling2D(pool_size = (pool_size,pool_size)),
]

autoencoder = models.Sequential()
autoencoder.encoding_layers = encoding_layers

for l in autoencoder.encoding_layers:
autoencoder.add(l)

decoding_layers = [
UpSampling2D(),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),

UpSampling2D(),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(512, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),

UpSampling2D(),
Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(256, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(128, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),

UpSampling2D(),
Convolution2D(128, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(64, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),

UpSampling2D(),
Convolution2D(64, (kernel, kernel), padding='same'),
BatchNormalization(),
Activation('relu'),
Convolution2D(n_labels, (1, 1), padding='valid', activation="sigmoid"),
BatchNormalization(),
]
autoencoder.decoding_layers = decoding_layers
for l in autoencoder.decoding_layers:
autoencoder.add(l)

autoencoder.add(Reshape((n_labels, img_h * img_w)))
autoencoder.add(Permute((2, 1)))
autoencoder.add(Activation('softmax'))



return autoencoder

model = build_model(img_w, img_h, pool_size)

但它返回错误。

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-051f06a53a14> in <module>()
----> 1 model = build_model(img_w, img_h, pool_size)

<ipython-input-20-c37fd94c8641> in build_model(img_w, img_h, pool_size)
119 autoencoder.add(l)
120
--> 121 autoencoder.add(Reshape((n_labels, img_h * img_w)))
122 autoencoder.add(Permute((2, 1)))
123 autoencoder.add(Activation('softmax'))
ValueError: total size of new array must be unchanged

我看不出错误的任何原因。当我将 img_w 和 img_h 更改为 256 时,此错误已解决,但问题是这不是图像大小或原始数据集,因此我无法使用它。如何解决?

最佳答案

问题是您正在执行 (2, 2) 下采样 5 次,所以让我们跟踪形状:

(360, 480) -> (180, 240) -> (90, 120) -> (45, 60) -> (22, 30) -> (11, 15)

现在上采样:

(11, 15) -> (22, 30) -> (44, 60) -> (88, 120) -> (176, 240) -> (352, 480)

因此,当您尝试使用原始形状reshape 输出时 - 由于模型不匹配而引发了问题。

可能的解决方案:

  1. 调整图像大小,使两个输入维度都可以被 32 整除(例如 (352, 480)(384, 480).

  2. 在第 3 次上采样后添加 ZeroPadding2D(((1, 0), (0, 0))) 以更改 (44, 60) 的形状到 (45, 60),什么将使您的网络以良好的输出形状结束。

其他问题:

请找出最后一个 MaxPooling2D 后面是第一个 Upsampling2D。这可能是个问题,因为这是您网络的无用瓶颈。

关于python - Keras 中的 Segnet : total size of new array must be unchanged error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48610981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com