mchd
mchd

Reputation: 3163

The dimension of a keras CNN reports 'None'

I am trying to follow through Kaggle Learn's Computer Vision tutorial. While testing out the code, the lecture used a file in it's example that it did not provide:

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

Because I didn't have this exact file, I decided to import it from Keras by adding ImageNet as its weight:

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

Aand when I try to add this base into my Keras NN:

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

I get this error:

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`.

Upvotes: 0

Views: 58

Answers (1)

faheem
faheem

Reputation: 634

you forgot to define input_shape.

here is how to include VGG16

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

Upvotes: 1

Related Questions