如何修复ValueError:检查目标Keras时出错

时间:2019-12-14 18:37:06

标签: python keras conv-neural-network

首先,我是CNN和Keras的新手。我正在尝试创建基于U-Net架构的神经网络。这是使用Keras的python模型:

inputs = Input((512, 512, 11))

    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs)
    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)

    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    pool1 = Dropout(0.1)(pool1)

    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)

    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    pool2 = Dropout(0.1)(pool2)

    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)

    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    pool3 = Dropout(0.1)(pool3)

    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)

    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
    drop4 = Dropout(0.1)(conv4)

    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)

    up6 = Conv2DTranspose(512, 2, strides = (2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv5)
    up6 = concatenate([drop4,up6])
    up6 = Dropout(0.1)(up6)

    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(up6)
    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)

    up7 = Conv2DTranspose(256, 2, strides = (2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)
    up7 = concatenate([conv3,up7])
    up7 = Dropout(0.1)(up7)

    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(up7)
    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)

    up8 = Conv2DTranspose(128, 2, strides = (2, 2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)
    up8 = concatenate([conv2,up8])
    up8 = Dropout(0.1)(up8)

    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(up8)
    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)

    up9 = Conv2DTranspose(64, 2,  strides = (2, 2),activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)
    up9 = concatenate([conv1,up9])
    up9 = Dropout(0.1)(up9)

    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(up9)
    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)

    conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)

    model = Model(input = [inputs], output = [conv10])

    model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy'])

    return model

model.summary()

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 512, 512, 11) 0                                            
__________________________________________________________________________________________________
conv2d_1 (Conv2D)               (None, 512, 512, 64) 6400        input_1[0][0]                    
__________________________________________________________________________________________________
conv2d_2 (Conv2D)               (None, 512, 512, 64) 36928       conv2d_1[0][0]                   
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D)  (None, 256, 256, 64) 0           conv2d_2[0][0]                   
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 256, 256, 64) 0           max_pooling2d_1[0][0]            
__________________________________________________________________________________________________
conv2d_3 (Conv2D)               (None, 256, 256, 128 73856       dropout_1[0][0]                  
__________________________________________________________________________________________________
conv2d_4 (Conv2D)               (None, 256, 256, 128 147584      conv2d_3[0][0]                   
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D)  (None, 128, 128, 128 0           conv2d_4[0][0]                   
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 128, 128, 128 0           max_pooling2d_2[0][0]            
__________________________________________________________________________________________________
conv2d_5 (Conv2D)               (None, 128, 128, 256 295168      dropout_2[0][0]                  
__________________________________________________________________________________________________
conv2d_6 (Conv2D)               (None, 128, 128, 256 590080      conv2d_5[0][0]                   
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D)  (None, 64, 64, 256)  0           conv2d_6[0][0]                   
__________________________________________________________________________________________________
dropout_3 (Dropout)             (None, 64, 64, 256)  0           max_pooling2d_3[0][0]            
__________________________________________________________________________________________________
conv2d_7 (Conv2D)               (None, 64, 64, 512)  1180160     dropout_3[0][0]                  
__________________________________________________________________________________________________
conv2d_8 (Conv2D)               (None, 64, 64, 512)  2359808     conv2d_7[0][0]                   
__________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D)  (None, 32, 32, 512)  0           conv2d_8[0][0]                   
__________________________________________________________________________________________________
conv2d_9 (Conv2D)               (None, 32, 32, 1024) 4719616     max_pooling2d_4[0][0]            
__________________________________________________________________________________________________
conv2d_10 (Conv2D)              (None, 32, 32, 1024) 9438208     conv2d_9[0][0]                   
__________________________________________________________________________________________________
dropout_4 (Dropout)             (None, 64, 64, 512)  0           conv2d_8[0][0]                   
__________________________________________________________________________________________________
conv2d_transpose_1 (Conv2DTrans (None, 64, 64, 512)  2097664     conv2d_10[0][0]                  
__________________________________________________________________________________________________
concatenate_1 (Concatenate)     (None, 64, 64, 1024) 0           dropout_4[0][0]                  
                                                                 conv2d_transpose_1[0][0]         
__________________________________________________________________________________________________
dropout_5 (Dropout)             (None, 64, 64, 1024) 0           concatenate_1[0][0]              
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 64, 64, 512)  4719104     dropout_5[0][0]                  
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 64, 64, 512)  2359808     conv2d_11[0][0]                  
__________________________________________________________________________________________________
conv2d_transpose_2 (Conv2DTrans (None, 128, 128, 256 524544      conv2d_12[0][0]                  
__________________________________________________________________________________________________
concatenate_2 (Concatenate)     (None, 128, 128, 512 0           conv2d_6[0][0]                   
                                                                 conv2d_transpose_2[0][0]         
__________________________________________________________________________________________________
dropout_6 (Dropout)             (None, 128, 128, 512 0           concatenate_2[0][0]              
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 128, 128, 256 1179904     dropout_6[0][0]                  
__________________________________________________________________________________________________
conv2d_14 (Conv2D)              (None, 128, 128, 256 590080      conv2d_13[0][0]                  
__________________________________________________________________________________________________
conv2d_transpose_3 (Conv2DTrans (None, 256, 256, 128 131200      conv2d_14[0][0]                  
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 256, 256, 256 0           conv2d_4[0][0]                   
                                                                 conv2d_transpose_3[0][0]         
__________________________________________________________________________________________________
dropout_7 (Dropout)             (None, 256, 256, 256 0           concatenate_3[0][0]              
__________________________________________________________________________________________________
conv2d_15 (Conv2D)              (None, 256, 256, 128 295040      dropout_7[0][0]                  
__________________________________________________________________________________________________
conv2d_16 (Conv2D)              (None, 256, 256, 128 147584      conv2d_15[0][0]                  
__________________________________________________________________________________________________
conv2d_transpose_4 (Conv2DTrans (None, 512, 512, 64) 32832       conv2d_16[0][0]                  
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 512, 512, 128 0           conv2d_2[0][0]                   
                                                                 conv2d_transpose_4[0][0]         
__________________________________________________________________________________________________
dropout_8 (Dropout)             (None, 512, 512, 128 0           concatenate_4[0][0]              
__________________________________________________________________________________________________
conv2d_17 (Conv2D)              (None, 512, 512, 64) 73792       dropout_8[0][0]                  
__________________________________________________________________________________________________
conv2d_18 (Conv2D)              (None, 512, 512, 64) 36928       conv2d_17[0][0]                  
__________________________________________________________________________________________________
conv2d_19 (Conv2D)              (None, 512, 512, 1)  65          conv2d_18[0][0]                  
==================================================================================================
Total params: 31,036,353
Trainable params: 31,036,353
Non-trainable params: 0

要训练模型,我使用两个numpy数组:

x_train,每个元素的形状都为(512、512、11)

y_train,每个元素的形状都为(512,512)

我的问题是conv2d_19的形状输出为(512,512,1)

我正在寻找丢弃conv2d_19输出中最后一个维的正确方法,以解决错误,而不必更改y_train中每个元素的维。我不知道我的解释是否正确。

这是错误:

Traceback (most recent call last):
  File "main.py", line 14, in <module>
    history = model.fit(x_train, y_train, batch_size=32, epochs=1)
  File "/home/luis/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1154, in fit
    batch_size=batch_size)
  File "/home/luis/.local/lib/python3.6/site-packages/keras/engine/training.py", line 621, in _standardize_user_data
    exception_prefix='target')
  File "/home/luis/.local/lib/python3.6/site-packages/keras/engine/training_utils.py", line 135, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking target: expected conv2d_19 to have 4 dimensions, but got array with shape (32, 512, 512)

2 个答案:

答案 0 :(得分:0)

使y_train与模型的输出具有相同的形状。那就是(512, 512, 1)
您的y_train当前为(512,512)

y_train = y_train.reshape((512,512,1))

或者在模型中

....
conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)
conv10 = Reshape((512,512))(conv10)
....

答案 1 :(得分:0)

来自https://keras.io/backend/#reshape

from keras import backend as K
out = K.reshape(conv10, (512,512))

然后使用out而不是conv10编译模型