keras 中的 fit 方法错误:期望 model_2 的形状为 (None, 252, 252, 1),但得到的数组形状为 (300, 128, 128, 3)

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

运行此模型时,我在 autoencoder_model.fit 这一行遇到了以下错误:

ValueError: 在检查目标时出错:期望 model_2 的形状为 (None, 252, 252, 1),但得到的数组形状为 (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')])

回答:

由于自编码器试图重建原始图像,似乎您重建的图像尺寸与原始图像不同,这是因为编码器中只有 两个 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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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