gpt4 book ai didi

python - keras CAE 检查目标 : expected conv2d_7 to have shape (252, 252、3) 时出错,但得到形状为 (256、256、3) 的数组

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:03 24 4
gpt4 key购买 nike

我设置了卷积层和池化层,然后deconvolute和de-pool,256 * 256 * 3图像的输入形状,但最后出现形状错误:

def build_auto_encode_model(shape=(256,256,3)):

input_img = Input(shape=shape)

x = Convolution2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(3, (3, 3), activation='sigmoid', padding='same')(x)

encoder = Model(inputs=input_img, outputs=encoded)

autoencoder = Model(inputs=input_img, outputs=decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
return encoder, autoencoder

def train_auto_encode_model(encoder_model_path="./data/encoder.h5"):
X = np.load("data/train.npy")
X_train = X[int(round(X.shape[0] * 0.2)):, :]
X_test = X[0:int(round(X.shape[0] * 0.2)), :]

encoder, autoencoder = build_auto_encode_model()
autoencoder.fit(X_train, X_train, epochs=10, batch_size=64, shuffle=True, validation_data=(X_test, X_test))
encoder.save(encoder_model_path)

这是我得到的错误:

Error when checking target: expected conv2d_7 to have shape (252, 252, 3) but got array with shape (256, 256, 3)

data shape

错误回溯:

error

最佳答案

‌通过使用 autoencoder.summary(),您会看到最后一个 Conv2D 层的输出形状是 (None, 252, 252, 3);所以形状 (256,256,3) 的标签不兼容。这个问题的原因是你忘记设置前一个 Conv2D 层的 padding 参数。通过将其设置为 'same' 可以解决此问题:

x = Convolution2D(16, (3, 3), activation='relu', padding='same')(x)  

关于python - keras CAE 检查目标 : expected conv2d_7 to have shape (252, 252、3) 时出错,但得到形状为 (256、256、3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699867/

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