剩余网中的预期密度小于预期的尺寸

时间:2019-01-08 18:22:51

标签: python tensorflow deep-learning

我想对在线找到的残差网络进行建模(https://gist.github.com/mjdietzx/0cb95922aac14d446a6530f87b3a04ce),但收到以下错误消息:

ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (670, 224, 224, 1)

是否可以更改最后一个密集层的尺寸?我的model.fit写错了吗? 224x224是图片的大小。 1是图像通道,670是数据量。

我的模型拟合:

model.fit(X_train, Y_train, validation_split=0.1, batch_size=16, epochs=50)

网络的最后一行:

batch_normalization_1272 (Batch (None, 7, 7, 2048)   8192        conv2d_1779[0][0]                
__________________________________________________________________________________________________
    add_384 (Add)                   (None, 7, 7, 2048)   0           leaky_re_lu_1173[0][0]           
                                                                     batch_normalization_1272[0][0]   
    __________________________________________________________________________________________________
    leaky_re_lu_1176 (LeakyReLU)    (None, 7, 7, 2048)   0           add_384[0][0]                    
    __________________________________________________________________________________________________
    global_average_pooling2d_19 (Gl (None, 2048)         0           leaky_re_lu_1176[0][0]           
    __________________________________________________________________________________________________
    dense_1 (Dense)                (None, 1)            2049        global_average_pooling2d_19[0][0]
    ==================================================================================================
    Total params: 66,938,625
    Trainable params: 66,870,401
    Non-trainable params: 68,224
    __________________________________________________________________________________________________
    None

1 个答案:

答案 0 :(得分:0)

您可能没有将正确的形状传递给model.fit()。网络输入应类似于:

x_train = np.zeros(shape=(670, img_height, img_width, img_channels))
y_train = np.zeros(shape=(670, 1))

model.fit(x_train, y_train, batch_size=128) # works fine

如果需要更改输出形状:

1)更改网络的输出(假设您有10个班级)

x = layers.GlobalAveragePooling2D()(x)
x = layers.Dense(10)(x)

2)确保将正确的数据输入模型:

x_train = np.zeros(shape=(670, img_height, img_width, img_channels))
y_train = np.zeros(shape=(670, 10))

model = models.Model(inputs=[image_tensor], outputs=[network_output])
model.compile('adam', loss='mse')
model.fit(x_train, y_train, batch_size=128)