Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 卷积自动编码器未在(62,47,1)数据集上进行训练;预期形状误差“;_Python_Keras_Convolution_Autoencoder - Fatal编程技术网

Python 卷积自动编码器未在(62,47,1)数据集上进行训练;预期形状误差“;

Python 卷积自动编码器未在(62,47,1)数据集上进行训练;预期形状误差“;,python,keras,convolution,autoencoder,Python,Keras,Convolution,Autoencoder,我试图在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' 使输入和输出

我试图在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)

任何关于它为什么抛出此错误的帮助或解释都将非常有用

这是因为池和填充物不匹配。 您的数据具有形状
(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'
设置为所有层。有一个卷积遗漏了它(最后一个之前的卷积)


也许列出的某些模式可能比其他模式性能更好。(例如,我想试试
mode='edge'

嘿,丹尼尔,谢谢你的建议!不幸的是,当我将上面的2条线添加到焊盘阵列时,会收到以下错误消息。“操作数不能与重新映射的形状[原始->重新映射]:(3,2)和请求的形状(4,2)一起广播:”不确定这是什么,因为我以前在填充MMM时从未得到过类似的结果,您可能也需要批处理维度,如
((0,0)、(1,1)、(0,1)、(0,0))
。图像可能是4D
(批次、尺寸1、尺寸2、通道)
-请参阅更新的答案。