votes up 7

The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

"""   When loading pretrained weights, `classifier_activation` can only
      be `None` or `"softmax"`.

  Returns:
    A `keras.Model` instance.
  """
  if not (weights in {'imagenet', None} or tf.io.gfile.exists(weights)):
    raise ValueError('The `weights` argument should be either '
                     '`None` (random initialization), `imagenet` '
                     '(pre-training on ImageNet), '
                     'or the path to the weights file to be loaded.')

  if weights == 'imagenet' and include_top and classes != 1000:
    raise ValueError('If using `weights` as `"imagenet"` with `include_top`'
                     ' as true, `classes` should be 1000')

  #

Ways to fix

votes up 1 votes down

the function tf.keras.applications.densenet.DenseNet121 is used to instantiate the Densenet121 architecture. Its instantiation depends on the value of parameter weights.

  • None: it means random initialization
  • 'imagenet': pre-training on ImageNet
  • path: the path to the weights file to be loaded

If invalid value is given to this parameter this value error is raised.

Reproducing the error:

pipenv install tensorflow

import tensorflow as tf
model = tf.keras.applications.densenet.DenseNet121(weights='imagent',classes=1000)

The error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-4cfee471c7c3> in <module>()
      1 
      2 import tensorflow as tf
----> 3 model = tf.keras.applications.densenet.DenseNet121(weights='imagent',classes=1000)

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/applications/densenet.py in DenseNet121(include_top, weights, input_tensor, input_shape, pooling, classes)
    331   """Instantiates the Densenet121 architecture."""
    332   return DenseNet([6, 12, 24, 16], include_top, weights, input_tensor,
--> 333                   input_shape, pooling, classes)
    334 
    335 

/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/applications/densenet.py in DenseNet(blocks, include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation)
    197   """
    198   if not (weights in {'imagenet', None} or file_io.file_exists_v2(weights)):
--> 199     raise ValueError('The `weights` argument should be either '
    200                      '`None` (random initialization), `imagenet` '
    201                      '(pre-training on ImageNet), '

ValueError: The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.

Fix:

The weight should be given a valid value. The cause of the error in the sample code is a typo in the value.

It should be "imagenet".

import tensorflow as tf
model = tf.keras.applications.densenet.DenseNet121(weights='imagenet',classes=1000)

Output:

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/densenet/densenet121_weights_tf_dim_ordering_tf_kernels.h5
33193984/33188688 [==============================] - 0s 0us/step

Jul 15, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix