【问题标题】:Convolutional Autoencoder Not Training on (62,47,1) dataset, "Expected Shape Error"卷积自动编码器未在 (62,47,1) 数据集上进行训练,“预期形状错误”
【发布时间】:2020-02-06 00:56:29
【问题描述】:

我正在尝试在 The Wild 数据集中的人脸上实现卷积自动编码器,它由 62x47x3 形状的图像组成。

但是,mnist 数据集上的 Keras 卷积自动编码器示例不适用于我正在训练的这个新数据集。

它会抛出这个错误

Error when checking target: expected conv2d_102 to have shape (60, 44, 3) but got array with shape (62, 47, 3)

关于某个层接收错误的形状输入,即使在包含

padding='same'

应该使输入和输出形状相等的命令。

我尝试在网络中只使用灰度图像,但这并没有什么不同。

这是我正在使用的主要代码


import tensorflow
import keras
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Model, Sequential
from keras.layers import Dense, Conv2D, Dropout, BatchNormalization, Input, Reshape, Flatten, Deconvolution2D, Conv2DTranspose, MaxPooling2D, UpSampling2D, LeakyReLU
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import adam

from sklearn.datasets import fetch_lfw_people
from sklearn.model_selection import train_test_split

#importing the dataset in color cause that's dope
lfw_data = fetch_lfw_people(color=True)

#putting the data of images into a variable
x = lfw_data.images

#making a train and validation set
(x_train,x_test) = train_test_split(x, test_size=0.25)

#normalizing the pixel values
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

print(x_train.shape)

x_train = x_train.reshape(len(x_train), 62,47,3)
x_test = x_test.reshape(len(x_test), 62,47,3)

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(62, 47, 3))  # adapt this if using `channels_first` image data format

x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
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 = MaxPooling2D((2, 2), padding='same')(x)

# at this point the representation is (4, 4, 8) i.e. 128-dimensional

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
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')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.summary()

模型汇总输出为

Model: "model_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_18 (InputLayer)        (None, 62, 47, 3)         0         
_________________________________________________________________
conv2d_103 (Conv2D)          (None, 62, 47, 16)        448       
_________________________________________________________________
max_pooling2d_45 (MaxPooling (None, 31, 24, 16)        0         
_________________________________________________________________
conv2d_104 (Conv2D)          (None, 31, 24, 8)         1160      
_________________________________________________________________
max_pooling2d_46 (MaxPooling (None, 16, 12, 8)         0         
_________________________________________________________________
conv2d_105 (Conv2D)          (None, 16, 12, 8)         584       
_________________________________________________________________
max_pooling2d_47 (MaxPooling (None, 8, 6, 8)           0         
_________________________________________________________________
conv2d_106 (Conv2D)          (None, 8, 6, 8)           584       
_________________________________________________________________
up_sampling2d_42 (UpSampling (None, 16, 12, 8)         0         
_________________________________________________________________
conv2d_107 (Conv2D)          (None, 16, 12, 8)         584       
_________________________________________________________________
up_sampling2d_43 (UpSampling (None, 32, 24, 8)         0         
_________________________________________________________________
conv2d_108 (Conv2D)          (None, 30, 22, 16)        1168      
_________________________________________________________________
up_sampling2d_44 (UpSampling (None, 60, 44, 16)        0         
_________________________________________________________________
conv2d_109 (Conv2D)          (None, 60, 44, 1)         145       
=================================================================
Total params: 4,673
Trainable params: 4,673
Non-trainable params: 0
____________________________

当我尝试训练时

#train for 100 epochs
history = autoencoder.fit(x_train, x_train,epochs=100,batch_size=256, shuffle=True, validation_data=(x_test, x_test))

我收到此错误消息

Error when checking target: expected conv2d_102 to have shape (60, 44, 3) but got array with shape (62, 47, 3)

任何关于它为什么抛出这个错误的帮助或解释都会很棒!

【问题讨论】:

    标签: python keras convolution autoencoder


    【解决方案1】:

    这是因为池和填充不匹配。 您的数据形状为(62,47),但您的模型输出(60,44)。您需要适当地调整模型或数据。

    基于池的工作原理(除以 2),并且考虑到您有 3 个池,您的图像大小只有在 2^3 = 8 的倍数时才能正确匹配池。由于尺寸 64 和 48 非常接近您的图像的大小,似乎最简单的解决方案是为图像添加填充。

    所以,让你的数据大小为(64,48)。 - 这将允许多达 4 个池,而无需模型中的自定义填充。

    x_train = np.pad(x_train, ((0,0), (1,1), (0,1), (0,0)), mode='constant')
    x_test = np.pad(x_test, ((0,0), (1,1), (0,1), (0,0)), mode='constant')
    

    不要忘记将padding='same' 设置为所有层。有一个卷积错过了它(最后一个之前的那个)

    也许here 列出的某些模式可能比其他模式执行得更好。 (例如,我会尝试mode='edge'。)

    【讨论】:

    • 嗨丹尼尔,谢谢你的建议!不幸的是,当我将上面的 2 行添加到填充数组时,我收到以下错误消息。 “操作数无法与重新映射的形状一起广播 [original->remapped]: (3,2) 和请求的形状 (4,2)”不知道这是什么,因为我以前在填充时从未得到过这样的东西跨度>
    • 嗯,你可能也需要批量维度,比如((0,0), (1,1), (0,1), (0,0))。这些图像可能是 4D (batch, size1, size2, channels)。 - 查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-06
    • 2021-07-10
    • 2017-01-04
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2022-01-20
    • 2019-01-28
    相关资源
    最近更新 更多