python - 如何使用 Keras 从密集层转到 conv2D 层?
问题描述
我只是想按照标题所说的去做。这是我的代码:
def ConvAutoEncoder(train_data,test_data,n_epochs = 50,batchSize = 128,data_shape=(IMAGE_SIZE,IMAGE_SIZE,3)):
print('Training Neural Network')
input_img = Input(shape=data_shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
print(x.shape)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
print(encoded.shape)
# at this point the representation is (6, 6, 4 i.e. 128-dimensional
encoded = Flatten()(encoded)
encoded = Dense( 6*6*4,activation='relu')(encoded)
print(encoded.shape)
endoded = Reshape((6,6,4))(encoded)
print(encoded.shape)
x = Conv2D(4, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
print(x.shape)
decoded = Conv2D(3, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(train_data, train_data,
epochs=n_epochs,
batch_size=batchSize,
shuffle=True,
verbose=2,
validation_data=(test_data, test_data),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])
return autoencoder
但是,当我运行它时,Reshape 层根本不做任何事情,reshape 之前的输出形状是 (?,144),之后的形状也是 (?,144)。我使用 reshape 错误还是有其他方法可以将密集层连接到 conv2D 层?
解决方案
推荐阅读
- php - 如何根据新的分页更新解析shopify分页链接的字符串?
- javascript - 用html编写EJS时如何清除错误
- r - flex_dashboard shiny_prerendered 与 eventReactive 过滤
- javascript - 为什么这个 XML 脚本只显示一次消息?
- serverless-framework - 如何删除无服务器仪表板?
- java - 从嵌套可选检查中的列表中获取元素
- xml - 为每个重复的节点创建新的 xml
- tsql - 获取 SELECT 语句中列的最大文本宽度
- javascript - 验证 2 个表单数组输入,使它们的总和等于 100%
- javascript - React JS 将“禁用”类添加到多个索引