keras - 如何在 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 卷积。
它是怎么发生的?
解决方案
您可以增加最后一个过滤器的大小来(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
推荐阅读
- python - 使用 now.strftime() 的 Python 时间模块输出错误
- xamarin - 如何在 Xamarin 中使用开关删除标签和条目
- python - 页面访问后 Heroku django 应用程序崩溃(H10)
- java - 如何将 MIDI 数据实时显示到 GUI?
- html - 为什么我的特定 CSS 代码更改没有反映在我的浏览器上?
- c - 如何在c中将结构变量作为参数传递
- amazon-web-services - 有没有办法在“一个”请求中对 AWSALB cookie 中的“所有”目标组的会话粘性信息进行编码?
- python - 在 Python 中将值从字典写入 CSV
- bots - TypeError:无法读取未定义的不和谐机器人 js 说命令的属性“加入”
- java - 创建自定义命令(即发送原始字节)到 Spring Data Redis?