Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 361
Jürgen K. Avatar asked Nov 24 '17 14:11

Jürgen K.


People also ask

How many classes are there in VGG16?

This model achieves 92.7% top-5 test accuracy on the ImageNet dataset which contains 14 million images belonging to 1000 classes.

What is the output of VGG16?

I have observed that VGG16 model predict with an output dimension of (1,512) , i understand 512 is the Features as predicted by the VGG16. however the inception model outputs a dimension of 1,8,8,2048.

What is Include_top in VGG16?

include_top: whether to include the 3 fully-connected layers at the top of the network. weights: one of None (random initialization), 'imagenet' (pre-training on ImageNet), or the path to the weights file to be loaded.


Video Answer


2 Answers

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.

like image 60
YSelf Avatar answered Oct 11 '22 20:10

YSelf


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

like image 24
wordsforthewise Avatar answered Oct 11 '22 18:10

wordsforthewise