Getting a list of all known classes of vgg-16 in keras

13,966

Solution 1

You could use decode_predictions and pass the total number of classes in the top=1000 parameter (only its default value is 5).

Or you could look at how Keras does this internally: It downloads the file imagenet_class_index.json (and usually caches it in ~/.keras/models/). This is a simple json file containing all class labels.

Solution 2

I think if you do something like this:

vgg16 = keras.applications.vgg16.VGG16(include_top=True,
                               weights='imagenet',
                               input_tensor=None,
                               input_shape=None,
                               pooling=None,
                               classes=1000)

vgg16.decode_predictions(np.arange(1000), top=1000)

Substitute your prediction array for np.arange(1000). Untested code so far.

Link to training labels here I think: http://image-net.org/challenges/LSVRC/2014/browse-synsets

Share:
13,966

Related videos on Youtube

Jürgen K.
Author by

Jürgen K.

Computer scientist

Updated on June 04, 2022

Comments

  • Jürgen K.
    Jürgen K. almost 2 years

    I use the pre-trained VGG-16 model from Keras.

    My working source code so far is like this:

    from keras.applications.vgg16 import VGG16
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.applications.vgg16 import preprocess_input
    from keras.applications.vgg16 import decode_predictions
    
    model = VGG16()
    
    print(model.summary())
    
    image = load_img('./pictures/door.jpg', target_size=(224, 224))
    image = img_to_array(image)  #output Numpy-array
    
    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
    
    image = preprocess_input(image)
    yhat = model.predict(image)
    
    label = decode_predictions(yhat)
    label = label[0][0]
    
    print('%s (%.2f%%)' % (label[1], label[2]*100))
    

    I wound out that the model is trained on 1000 classes. It there any possibility to get the list of the classes this model is trained on? Printing out all the prediction labels is not an option because there are only 5 returned.

    Thanks in advance

  • wordsforthewise
    wordsforthewise over 6 years
    Good to know how to do it, would be even better with a link to a jupyter notebook where it's already done.
  • R.S.
    R.S. about 6 years
    @YSelf . Thanks. I found this be the easiest way to list all class labels when running Keras from R.
  • TheSHETTY-Paradise
    TheSHETTY-Paradise over 5 years
    Hey! I'm writing my code on google colab, and when I'm writing model.decode_classifications(predictions, top = 1000) it says, AttributeError: 'Model' object has no attribute 'decode_predictions' prediction = model.predict(img) model.decode_predictions(predictions, top = 1000)