【问题标题】:ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`ValueError:应定义“密集”输入的最后一个维度。找到`无`
【发布时间】:2018-10-09 10:00:06
【问题描述】:

我的模型定义如下:

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 还很陌生,任何输入将不胜感激。

【问题讨论】:

  • 我的问题是为什么 Flatten 层会给出 (None, None) 的输出。

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


【解决方案1】:

您对输入图像的裁剪过多。 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)))

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2020-05-23
    相关资源
    最近更新 更多