Keras-UpSampling2D图层似乎正在返回2D张量而不是4D

时间:2019-02-12 05:08:20

标签: python tensorflow keras conv-neural-network

我正在将1080x1616彩色照片馈入Keras的卷积神经网络。数据可以很好地通过网络馈送,直到到达最后一个卷积层为止,并抛出一个错误,说它期望使用4D张量,但是却得到(32,1)张量。我不知道这个数字是在哪里产生的。

这是我的模型架构:

model = keras.Sequential([
        Conv2D(32, (3,3), padding='same', activation='relu', data_format="channels_last", input_shape=(self.x_res,self.y_res,self.n_channels), kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2,2)),
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),   
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(self.n_channels, (1, 1), padding='same', activation='sigmoid', kernel_initializer=keras.initializers.he_normal())
    ])

这是来自model.summary()

Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 1080, 1616, 32)    896
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 540, 808, 32)      0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 540, 808, 64)      18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 270, 404, 64)      0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 270, 404, 128)     73856
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 135, 202, 128)     0
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 135, 202, 256)     295168
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 270, 404, 256)     0
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 270, 404, 128)     295040
_________________________________________________________________
up_sampling2d_2 (UpSampling2 (None, 540, 808, 128)     0
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 540, 808, 64)      73792
_________________________________________________________________
up_sampling2d_3 (UpSampling2 (None, 1080, 1616, 64)    0
_________________________________________________________________
conv2d_7 (Conv2D)            (None, 1080, 1616, 3)     195
=================================================================
Total params: 757,443
Trainable params: 757,443
Non-trainable params: 0
_________________________________________________________________
None

UpSampling2D层的Keras文档说:

Output shape

4D tensor

1 个答案:

答案 0 :(得分:2)

模型的输入应为形状(batch_size,x_res,y_res,n_channels),请进行检查。

以下是一个简单的测试:

import keras
from keras.layers import Conv2D, MaxPooling2D, UpSampling2D
import numpy as np

x_res = 1080
y_res = 1616
n_channels = 3

model = keras.Sequential([
        Conv2D(32, (3,3), padding='same', activation='relu', data_format="channels_last", input_shape=(x_res,y_res,n_channels), kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2,2)),
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        MaxPooling2D((2, 2)),
        Conv2D(256, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(128, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),   
        Conv2D(64, (3, 3), padding='same', activation='relu', kernel_initializer=keras.initializers.he_normal()),
        UpSampling2D((2, 2)),
        Conv2D(n_channels, (1, 1), padding='same', activation='sigmoid', kernel_initializer=keras.initializers.he_normal())
    ])

image = np.random.randn(1, x_res, y_res, n_channels)

model.predict(image)