ValueError:应该定义“Dense”输入的最后一个维度。找到了“无”

时间:2018-04-28 21:41:53

标签: python tensorflow keras deep-learning conv-neural-network

我的模型定义如下:

def build(data):
    model = Sequential()
    model.add(Cropping2D(cropping=((79, 145), (50, 250)), input_shape= 
                                                                   (160,320,3)))
    model.add(Lambda(lambda x: x/127.5 - 1.0))

    model.add(Conv2D(24, (2, 2), padding='same'))
    model.add(ELU())
    model.add(Conv2D(36, (2, 2), padding='same'))
    model.add(ELU())
    model.add(Conv2D(48, (2, 2), padding='same'))
    model.add(ELU())

    # Add a flatten layer
    model.add(Flatten())
    model.summary()
    model.add(Dense(100))
    model.add(ELU())
    model.add(Dense(50))
    model.add(ELU())
    model.add(Dense(10))
    model.add(ELU())
    model.add(Dense(1))

    return model

出现此错误:

  

ValueError:Dense输入的最后一个维度应该是   定义。找到None

我跑了model.summary()并获得了以下输出

Layer (type)                 Output Shape              Param #   
=================================================================
cropping2d_15 (Cropping2D)   (None, 0, 20, 3)          0         
_________________________________________________________________
lambda_23 (Lambda)           (None, 0, 20, 3)          0         
_________________________________________________________________
conv2d_47 (Conv2D)           (None, 0, 20, 24)         312       
_________________________________________________________________
elu_43 (ELU)                 (None, 0, 20, 24)         0         
_________________________________________________________________
conv2d_48 (Conv2D)           (None, 0, 20, 36)         3492      
_________________________________________________________________
elu_44 (ELU)                 (None, 0, 20, 36)         0         
_________________________________________________________________
conv2d_49 (Conv2D)           (None, 0, 20, 48)         6960      
_________________________________________________________________
elu_45 (ELU)                 (None, 0, 20, 48)         0         
_________________________________________________________________
flatten_12 (Flatten)         (None, None)              0         
=================================================================
Total params: 10,764
Trainable params: 10,764
Non-trainable params: 0

我对python很新,任何输入都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

您过多地裁剪了输入图像。 cropping参数为interpreted,如下所示:

  

如果2个元组中2个整数的元组:解释为(((top_crop,   bottom_crop),(left_crop,right_crop))

请考虑Keras文档中的以下示例:

# Crop the input 2D images or feature maps
model = Sequential()
model.add(Cropping2D(cropping=((2, 2), (4, 4)),
                     input_shape=(28, 28, 3)))
# now model.output_shape == (None, 24, 20, 3)

在代码中,您正在从顶部裁剪79像素,从底部裁剪145像素,而图像的高度仅为160像素。通过减少裁剪,您的代码可以正常运行,例如:

model.add(Cropping2D(cropping=((10, 10), (10, 10)), input_shape=(160,320,3)))