image-processing - keras 在拟合方法 : expected model_2 to have shape (None, 252, 252, 1) 中出错,但得到了形状为 (300, 128, 128, 3) 的数组

标签 image-processing machine-learning keras autoencoder

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

运行此模型时,我收到以下错误 autoencoder_model.fit:

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 层。

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

将此用于您的编码器,并让我们知道它是否有效:

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)

关于image-processing - keras 在拟合方法 : expected model_2 to have shape (None, 252, 252, 1) 中出错,但得到了形状为 (300, 128, 128, 3) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47859737/

相关文章:

image - 在matlab中提取图像的运动模糊

java - 如何检查曲线是否相似

image - 如何通过图像处理检测两条垂直线?

machine-learning - 神经网络的异常结果

python - Keras:无输入的自定义图层

opencv - 比较扫描图像并检测非常小的可见变化

machine-learning - 如何处理C4.5(J48)决策树中缺失的属性值?

machine-learning - 使用 Keras 进行文本分类 : How to add custom features?

python - Keras 函数式 API 有什么特别之处?

python - 是否可以从保存的 CNN 中提取分配给训练图像的类概率?