keras CAE 检查目标时出错:期望 conv2d_7 的形状为 (252, 252, 3),但得到的数组形状为 (256, 256, 3)

我设置了卷积层和池化层,然后进行反卷积和反池化,输入形状为 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, autoencoderdef 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)  

Related Posts

Keras Dense层输入未被展平

这是我的测试代码: from keras import…

无法将分类变量输入随机森林

我有10个分类变量和3个数值变量。我在分割后直接将它们…

如何在Keras中对每个输出应用Sigmoid函数?

这是我代码的一部分。 model = Sequenti…

如何选择类概率的最佳阈值?

我的神经网络输出是一个用于多标签分类的预测类概率表: …

在Keras中使用深度学习得到不同的结果

我按照一个教程使用Keras中的深度神经网络进行文本分…

‘MatMul’操作的输入’b’类型为float32,与参数’a’的类型float64不匹配

我写了一个简单的TensorFlow代码,但不断遇到T…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注