Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 自动编码器适用于MNIST,但适用于较大尺寸的图像_Python_Tensorflow_Keras_Conv Neural Network_Autoencoder - Fatal编程技术网

Python 自动编码器适用于MNIST,但适用于较大尺寸的图像

Python 自动编码器适用于MNIST,但适用于较大尺寸的图像,python,tensorflow,keras,conv-neural-network,autoencoder,Python,Tensorflow,Keras,Conv Neural Network,Autoencoder,我已经尝试过为MNIST构建自动编码器。Autoencoder工作,然后我尝试更改图像,结果将形状从28,28,1输入到150,150,3,我收到以下错误: ValueError:检查目标时出错:预期conv2d_6具有 形状(1481481),但得到了形状为(1501503)的数组 自动编码器架构: input_img = Input(shape=(150, 150, 3)) x = Conv2D(16, (3, 3), activation='relu', padding='same')(

我已经尝试过为MNIST构建自动编码器。Autoencoder工作,然后我尝试更改图像,结果将形状从
28,28,1
输入到
150,150,3
,我收到以下错误:

ValueError:检查目标时出错:预期conv2d_6具有 形状(1481481),但得到了形状为(1501503)的数组

自动编码器架构:

input_img = Input(shape=(150, 150, 3))

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)

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=Adam(0.01), loss='binary_crossentropy')
列车设置:

autoencoder.fit(x_train, y_train,
                epochs=50,
                batch_size=512,
                shuffle=True,
                validation_data=(x_test, y_test))
我的数据形状如下所示:

x_train shape: (4022, 150, 150, 3)
y_train shape: (4022, 150, 150, 3)
x_test shape: (447, 150, 150, 3)
y_test shape: (447, 150, 150, 3)
到我的工作区的协作链接:


我认为您的最终解码层应该解码到三个通道,而不是一个通道

decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
应该是

decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)

否?

使用此代码,它将正常工作

input_img = Input(shape=(150, 150, 3))
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)
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 = ZeroPadding2D(padding=(1, 1), input_shape=(148, 148, 16))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='valid')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer=Adam(0.01), loss='binary_crossentropy')
我添加了零填充,并将最后一层conv更改为输出3个通道

这将打印以下摘要

Layer (type)                 Output Shape              Param #   
=================================================================
input_10 (InputLayer)        (None, 150, 150, 3)       0         
_________________________________________________________________
conv2d_58 (Conv2D)           (None, 150, 150, 16)      448       
_________________________________________________________________
max_pooling2d_25 (MaxPooling (None, 75, 75, 16)        0         
_________________________________________________________________
conv2d_59 (Conv2D)           (None, 75, 75, 8)         1160      
_________________________________________________________________
max_pooling2d_26 (MaxPooling (None, 38, 38, 8)         0         
_________________________________________________________________
conv2d_60 (Conv2D)           (None, 38, 38, 8)         584       
_________________________________________________________________
max_pooling2d_27 (MaxPooling (None, 19, 19, 8)         0         
_________________________________________________________________
conv2d_61 (Conv2D)           (None, 19, 19, 8)         584       
_________________________________________________________________
up_sampling2d_25 (UpSampling (None, 38, 38, 8)         0         
_________________________________________________________________
conv2d_62 (Conv2D)           (None, 38, 38, 8)         584       
_________________________________________________________________
up_sampling2d_26 (UpSampling (None, 76, 76, 8)         0         
_________________________________________________________________
zero_padding2d_4 (ZeroPaddin (None, 78, 78, 8)         0         
_________________________________________________________________
conv2d_63 (Conv2D)           (None, 76, 76, 16)        1168      
_________________________________________________________________
up_sampling2d_27 (UpSampling (None, 152, 152, 16)      0         
_________________________________________________________________
conv2d_64 (Conv2D)           (None, 150, 150, 3)       435       
=================================================================
Total params: 4,963
Trainable params: 4,963
Non-trainable params: 0
_________________________________________________________________
None