【问题标题】:The dimension of a keras CNN reports 'None'keras CNN 的维度报告“无”
【发布时间】:2020-11-16 07:29:26
【问题描述】:

我正在尝试学习 Kaggle Learn 的计算机视觉教程。在测试代​​码时,讲座在它的示例中使用了一个它没有提供的文件:

pretrained_base = tf.keras.models.load_model(
    '../input/cv-course-models/cv-course-models/vgg16-pretrained-base',
)
pretrained_base.trainable = False

因为我没有这个确切的文件,所以我决定通过添加 ImageNet 作为其权重从 Keras 导入它:

pretrained_base = VGG16(weights='imagenet', include_top=False)
pretrained_base.trainable = False

当我尝试将此基础添加到我的 Keras NN 中时:

model = keras.Sequential([
                          pretrained_base,
                          layers.Flatten(),
                          layers.Dense(6, activation = 'relu'),
                          layers.Dense(1, activation = 'sigmoid'),                          
])

我收到此错误:

ValueError                                Traceback (most recent call last)
<ipython-input-10-4dd4b7ce29df> in <module>()
      3                           layers.Flatten(),
      4                           layers.Dense(6, activation = 'relu'),
----> 5                           layers.Dense(1, activation = 'sigmoid'),
      6 ])

7 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py in build(self, input_shape)
   1166     last_dim = tensor_shape.dimension_value(input_shape[-1])
   1167     if last_dim is None:
-> 1168       raise ValueError('The last dimension of the inputs to `Dense` '
   1169                        'should be defined. Found `None`.')
   1170     self.input_spec = InputSpec(min_ndim=2, axes={-1: last_dim})

ValueError: The last dimension of the inputs to `Dense` should be defined. Found `None`.

【问题讨论】:

    标签: python tensorflow keras deep-learning computer-vision


    【解决方案1】:

    你忘了定义 input_shape。

    这里是如何包含 VGG16

    pretrained_base = VGG16(weights='imagenet', include_top=False, input_shape=(224,224,3))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2019-12-14
      相关资源
      最近更新 更多