Python Keras卷积自编码器.ValueError: logits和label必须具有相同的形状



我从这里取了大部分代码。我对自动编码器很陌生。

下面是代码:
input_img = keras.Input(shape=(64, 55,1))
x = keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
decoded = keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
onlyfiles = [f for f in listdir("newnew") if isfile(join("newnew", f))][:2000]
lis=[]
for i in onlyfiles:
image = Image.open('newnew/'+i)
data = np.asarray(image)
lis.append(data)

lis = np.array(lis)
lis = lis.astype("float32")
lis = lis / np.max(lis)
print(lis.shape)
lis = lis.reshape((len(lis),64,55,1))
print(lis.shape)
test = lis[:100]
train = lis[100:]
autoencoder.fit(train,train,
epochs=50,
batch_size=128,
shuffle=True,
validation_data=(test,test))

因为我只是想让代码运行,所以我将其限制为只有2000张图像。对于上下文,图像是灰度的,已经是55x64了。

我收到的错误信息是:

ValueError: logits和label必须具有相同的形状((None, 64, 56, 1) vs (None, 64, 55, 1))

在堆栈溢出上阅读类似的问题后,我已经对我从自动编码器中获取的网站进行了一个更改,通过将额外的padding='same'添加到其中一条缺失的行。我相信问题的根源是在层的某个地方,因为最初在添加额外的"padding=same"之前,错误消息说:

ValueError: logits和label必须具有相同的形状((None, 60, 52,1) vs (None, 64, 55,1))

不幸的是,正如我所说,我是自动编码器的新手,仍然不能完全理解层中的所有数字。任何帮助都将是感激的:)

由于自动编码器的输出与输入相同,因此输入和输出形状应该相同。模型中的输入形状为(None, 64,55,1),输出形状为(None, 64,56,1),从而导致错误。

为了解决这个问题,在模型中添加一个Cropping2D图层,如下所示。

input_img = keras.Input(shape=(64, 55,1))
x = keras.layers.Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = keras.layers.MaxPooling2D((2, 2), padding='same')(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
x = keras.layers.Conv2D(16, (3, 3), activation='relu',padding='same')(x)
x = keras.layers.UpSampling2D((2, 2))(x)
decoded = keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoded= keras.layers.Cropping2D(((0,0),(0,1)))(decoded)
autoencoder = keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy') 
autoencoder.summary()

输出:

Model: "model_11"
_________________________________________________________________
Layer (type)                Output Shape              Param #   
=================================================================
input_15 (InputLayer)       [(None, 64, 55, 1)]       0         

conv2d_98 (Conv2D)          (None, 64, 55, 16)        160       

max_pooling2d_42 (MaxPoolin  (None, 32, 28, 16)       0         
g2D)                                                            

conv2d_99 (Conv2D)          (None, 32, 28, 8)         1160      

max_pooling2d_43 (MaxPoolin  (None, 16, 14, 8)        0         
g2D)                                                            

conv2d_100 (Conv2D)         (None, 16, 14, 8)         584       

max_pooling2d_44 (MaxPoolin  (None, 8, 7, 8)          0         
g2D)                                                            

conv2d_101 (Conv2D)         (None, 8, 7, 8)           584       

up_sampling2d_42 (UpSamplin  (None, 16, 14, 8)        0         
g2D)                                                            

conv2d_102 (Conv2D)         (None, 16, 14, 8)         584       

up_sampling2d_43 (UpSamplin  (None, 32, 28, 8)        0         
g2D)                                                            

conv2d_103 (Conv2D)         (None, 32, 28, 16)        1168      

up_sampling2d_44 (UpSamplin  (None, 64, 56, 16)       0         
g2D)                                                            

conv2d_104 (Conv2D)         (None, 64, 56, 1)         145       

cropping2d_13 (Cropping2D)  (None, 64, 55, 1)         0         

=================================================================
Total params: 4,385
Trainable params: 4,385
Non-trainable params: 0
_________________________________________________________________ 

相关内容

  • 没有找到相关文章

最新更新