首页 > 解决方案 > 如何在 Keras 的自动编码器中拟合维度

问题描述

我正在对 Mnist 图像数据(尺寸为 28*28)使用卷积自动编码器,这是我的代码

input_img = Input(shape=(28, 28, 1))

x = Convolution2D(16, (5, 5), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, (5, 5), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)


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

我收到一条错误消息(每一层都有 padding ='same')

ValueError: Error when checking target: expected conv2d_148 to have shape (32, 32, 1) but got array 
with shape (28, 28, 1)

这是我的模型摘要

层(类型)输出形状参数#

input_20 (InputLayer) (无, 28, 28, 1) 0


conv2d_142 (Conv2D) (无, 28, 28, 16) 416


max_pooling2d_64 (MaxPooling (无, 14, 14, 16) 0


conv2d_143 (Conv2D) (无, 14, 14, 8) 1160


max_pooling2d_65 (MaxPooling (无, 7, 7, 8) 0


conv2d_144 (Conv2D) (无, 7, 7, 8) 584


max_pooling2d_66 (MaxPooling (无, 4, 4, 8) 0


conv2d_145 (Conv2D) (无, 4, 4, 8) 584


up_sampling2d_64(上采样(无,8,8,8)0


conv2d_146 (Conv2D) (无, 8, 8, 8) 584


up_sampling2d_65(上采样(无,16,16,8)0


conv2d_147 (Conv2D) (无, 16, 16, 16) 3216


up_sampling2d_66(上采样(无,32、32、16)0


conv2d_148 (Conv2D) (无, 32, 32, 1) 145

总参数:6,689 可训练参数:6,689 不可训练参数:0

我知道如果我将第一层更改为 x = Convolution2D(16, (3, 3), activation='relu', padding='same')(input_img) 它可以工作,但我想使用 5*5 卷积。

它是怎么发生的?

标签: kerastf.kerasautoencoder

解决方案


您可以增加最后一个过滤器的大小来(5, 5)完成这项工作:

from tensorflow.keras.layers import *
from tensorflow.keras import Model, Input
import numpy as np

input_img = Input(shape=(28, 28, 1))

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

decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='valid')(x)

auto = Model(input_img, decoded)

auto.build(input_shape=(1, 28, 28, 1))
auto(np.random.rand(1, 28, 28, 1)).shape
TensorShape([1, 28, 28, 1])

或者,使用tf.keras.Conv2DTranspose


推荐阅读