gpt4 book ai didi

python - 如何修复简单自动编码器中的尺寸错误?

转载 作者:行者123 更新时间:2023-11-30 09:32:03 26 4
gpt4 key购买 nike

我是 python 和自动编码器的新手。我只想构建一个简单的自动编码器来开始,但我不断收到此错误:

ValueError: Error when checking target: expected conv2d_39 to have 4 dimensions, but got array with shape (32, 3)

除了 flow_from_directory 方法之外,还有更好的方法来获取我自己的数据吗?我构建了像 this 这样的自动编码器,但我去掉了一些层。

我不知道,但我是否向自动编码器提供从 flow_from_directory 方法生成的元组?有没有办法将此元组转换为自动编码器接受的格式?

import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import Dropout, Flatten, Dense, Input, Conv2D,
UpSampling2D, MaxPooling2D
from keras.optimizers import RMSprop

IMG_WIDTH, IMG_HEIGHT = 112, 112
input_img = Input(shape=(IMG_WIDTH, IMG_HEIGHT,3))

#encoder
def encoder(input_img):
# 1x112x112x3
conv1 = Conv2D(32,(3,3), activation='relu', padding='same')
(input_img)
# 32x112x112
pool1 = MaxPooling2D(pool_size=(2,2))(conv1)
# 32x56x56
return pool1

#decoder
def decoder(pool1):
# 32x56x56
up1 = UpSampling2D((2,2))(pool1)
# 32x112x112
decoded = Conv2D(1,(3,3),activation='sigmoid',padding='same')(up1)
# 1x112x112
return decoded

autoencoder = Model(input_img, decoder(encoder(input_img)))
autoencoder.compile(loss='mean_squared_error', optimizer=RMSprop())

datagen = ImageDataGenerator(rescale=1./255)

training_set = datagen.flow_from_directory(
r'C:\Users\user\Desktop\dataset\train',
target_size=(112,112),
batch_size=32,
class_mode='categorical')

test_set = datagen.flow_from_directory(
r'C:\Users\user\Desktop\dataset\validation',
target_size=(112,112),
batch_size=32,
class_mode='categorical')

history = autoencoder.fit_generator(
training_set,
steps_per_epoch=2790,
epochs=5,
validation_data=test_set,
validation_steps=1145)

以下是模型摘要:

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_14 (InputLayer) (None, 112, 112, 3) 0
_________________________________________________________________
conv2d_42 (Conv2D) (None, 112, 112, 32) 896
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 56, 56, 32) 0
_________________________________________________________________
up_sampling2d_4 (UpSampling2 (None, 112, 112, 32) 0
_________________________________________________________________
conv2d_43 (Conv2D) (None, 112, 112, 1) 289
=================================================================
Total params: 1,185
Trainable params: 1,185
Non-trainable params: 0
_________________________________________________________________

我正在处理 512x496 OCT 图像。

最佳答案

由于您正在构建自动编码器,因此模型的输出必须与输入相同,因此您的代码存在两个问题:

  1. 您必须设置 class_mode 'input' 的生成器参数让生成的标签与生成的输入相同。

  2. 最后一层必须有 3 个滤波器,因为输入图像有 3 个 channel :decoded = Conv2D(3, ...) .

关于python - 如何修复简单自动编码器中的尺寸错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53884001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com