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

4

12

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

Cataphyll answered 24/11, 2017 at 14:0 Comment(0)
M
10

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.

Maund answered 24/11, 2017 at 14:14 Comment(3)
Good to know how to do it, would be even better with a link to a jupyter notebook where it's already done.Level
@Maund . Thanks. I found this be the easiest way to list all class labels when running Keras from R.Measly
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)Valuer
L
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

Level answered 15/1, 2018 at 2:15 Comment(0)
C
0

If you edit your code a little bit you could get a list of all top predictions for the example you provided. Tensorflow decode_predictions returns a list of list class predictions tuples. So first, add top=1000 argument as @YSelf recommended to label = decode_predictions(yhat, top=1000) Then change label = label[0][0] to label = label[0][:] to select all of the predictions. label would look like this:

[('n04252225', 'snowplow', 0.4144803),
('n03796401', 'moving_van', 0.09205707),
('n04461696', 'tow_truck', 0.08912289),
('n03930630', 'pickup', 0.07173037),
('n04467665', 'trailer_truck', 0.048759833),
('n02930766', 'cab', 0.043586567),
('n04037443', 'racer', 0.036957625),....)]

From here you need to make a tuple unpacking. If you want to just get the list of 1000 classes you could just call [y for (x,y,z) in label] and you will have your list of all the 1000 classes. Output would look like this:

['snowplow',
'moving_van',
'tow_truck',
'pickup',
'trailer_truck',
'cab',
'racer',....]
Clavate answered 30/7, 2018 at 23:57 Comment(0)
T
0

this single line shall print out all class names and indices: decode_predictions(np.expand_dims(np.arange(1000), 0), top=1000)

Traceetracer answered 21/11, 2020 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.