我正在处理不同大小(x,y)的图像。在UpSampling2D之后使用MaxPooling2D时,由于x-dim不等于y-dim,因此无法很好地重建它。当x = y(例如28x28)时有效,但以我为例(388x45)。我怎么解决这个问题。

input_img = Input(shape=(388, 45, 1))

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)

x = UpSampling2D((2, 2))(x)
x = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

最佳答案

解决方案是在上采样层之后添加ZeroPadding2D以达到所需的形状。

实际上,如果您具有图像的形状((19,30)),则为了获得偶数,例如在第一个位置,您需要添加:

x = UpSampling2D((2, 2))(x) #say here the shape is (19,30) after upsampling but you need (20,30)
x = ZeroPadding2D(((1, 0), (0, 0)))(x) # change to ZeroPadding2D(((0, 0), (0, 1))) if you want second dimension to increase by 1


您可以在以下答案中找到ZeroPadding2D的完美用法:
segnet in keras: total size of new array must be unchanged error

关于python - Keras Conv2D解码器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59600141/

10-12 16:52