gpt4 book ai didi

keras - 在keras的自动编码器的最后一层裁剪

转载 作者:行者123 更新时间:2023-12-04 03:09:27 25 4
gpt4 key购买 nike

我有形状为 391 x 400 的图像。我尝试按照描述使用自动编码器 here .

具体来说,我使用了如下代码:

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K

input_img = Input(shape=(391, 400, 1)) # 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', padding='same')(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')

我得到以下信息:

ValueError: Error when checking target: expected conv2d_37 to have shape (None, 392, 400, 1) but got array with shape (500, 391, 400, 1)

我需要的是:将最后一层从 392 x 400 删除/裁剪/ reshape 为 391 x 400 的图层。

感谢您的帮助。

最佳答案

有一个图层叫做 Cropping2D。要将最后一层从 392 x 400 裁剪到 391 x 400,您可以通过以下方式使用它:

cropped = Cropping2D(cropping=((1, 0), (0, 0)))(decoded)
autoencoder = Model(input_img, cropped)

元组 ((1, 0), (0, 0)) 表示从顶部裁剪 1 行。如果您想从底部裁剪,请改用 ((0, 1), (0, 0))。你可以看到 documentation有关 cropping 参数的更多详细说明。

关于keras - 在keras的自动编码器的最后一层裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46441635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com