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
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')
#
Links to the raise (12)
https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/densenet.py#L200 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/efficientnet.py#L278 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/inception_resnet_v2.py#L120 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/inception_v3.py#L115 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/mobilenet.py#L171 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/mobilenet_v2.py#L196 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/mobilenet_v3.py#L165 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/nasnet.py#L153 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/resnet.py#L125 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/vgg16.py#L116 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/vgg19.py#L116 https://github.com/keras-team/keras/blob/4a978914d2298db2c79baa4012af5ceff4a4e203/keras/applications/xception.py#L117See also in the other packages (1)
(❌️ No answer)
tensorflow/the-weights-argument-should-b
Ways to fix
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 ImageNetpath:
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
Add a possible fix
Please authorize to post fix