拟合方法中的 keras 错误:预期 model_2 具有形状 (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组 [英] keras error in fit method : expected model_2 to have shape (None, 252, 252, 1) but got array with shape (300, 128, 128, 3)

查看:27
本文介绍了拟合方法中的 keras 错误:预期 model_2 具有形状 (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一类分类构建一个图像分类器,其中我使用了自动编码器.

I am building a image classifier for one-class classification in which i've used autoencoder.

在运行这个模型时,我通过 autoencoder_model.fit 行收到这个错误:

While running this model I am getting this error by the line autoencoder_model.fit:

ValueError: 检查目标时出错:预期 model_2 具有形状 (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组

ValueError: Error when checking target: expected model_2 to have shape (None, 252, 252, 1) but got array with shape (300, 128, 128, 3)

num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')

labels[0:376]=0 
names = ['cats']

input_shape=img_data[0].shape

X_train, X_test = train_test_split(img_data, test_size=0.2, random_state=2)

inputTensor = Input(input_shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(inputTensor)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded_data = MaxPooling2D((2, 2), padding='same')(x)

encoder_model = Model(inputTensor,encoded_data)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional
encoded_input = Input((4,4,8))
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded_input)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded_data = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

decoder_model = Model(encoded_input,decoded_data)

autoencoder_input = Input(input_shape)
encoded = encoder_model(autoencoder_input)
decoded = decoder_model(encoded)
autoencoder_model = Model(autoencoder_input, decoded)
autoencoder_model.compile(optimizer='adadelta', enter code here`loss='binary_crossentropy')

autoencoder_model.fit(X_train, X_train,
        epochs=50,
        batch_size=32,
        validation_data=(X_test, X_test),
        callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

推荐答案

当自动编码器尝试重新创建原始图像时,您似乎正在重建与原始尺寸不同的图像,因为编码器中只有两个 MaxPool2D 层,解码器中只有三个 UpSampling2D 层.

As the auto-encoder tries to re-create the original images, it seems you are reconstructing an image with different dimensions than the original, due to the fact to have only two MaxPool2D layers in your encoder and three UpSampling2D layers in your decoder.

当自动编码器尝试评估重建的损失时,由于维度不匹配而遇到错误.

When the auto-encoder tries to evaluate the loss of the reconstruction, it runs into an error due to a dimension miss-match.

将此用于您的编码器,并告诉我们它是否有效:

Use this for your encoder and let us know if it works:

inputTensor = Input(input_shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(inputTensor)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded_data = MaxPooling2D((2, 2), padding='same')(x)

encoder_model = Model(inputTensor,encoded_data)

这篇关于拟合方法中的 keras 错误:预期 model_2 具有形状 (None, 252, 252, 1) 但得到形状为 (300, 128, 128, 3) 的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆