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

你好,我正在构建一个用于单类分类的图像分类器,在使用自编码器运行该模型时,遇到了这个错误,错误发生在这一行(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].shapeX_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-dimensionalencoded_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')])

回答:

这只是解码器输出形状与训练数据形状之间的简单不匹配问题。(目标指的是输出)。

我看到你使用了两个最大池化层(将图像大小缩小到原来的四分之一),以及三个上采样层(将解码器的输入放大到原来的八倍)。

自编码器的最终输出太大,与你的数据不匹配。你需要调整模型,使输出形状与你的训练数据匹配。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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