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
This model achieves 92.7% top-5 test accuracy on the ImageNet dataset which contains 14 million images belonging to 1000 classes.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With