Last active
November 30, 2017 20:09
-
-
Save prashantgpt91/385db5d7ef145e2ef97d8756eeee28df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
from keras.applications.vgg16 import VGG16 | |
# load the model | |
model = VGG16() | |
# load an image from file | |
image = load_img('2.jpg', target_size=(224, 224)) | |
# convert the image pixels to a numpy array | |
image = img_to_array(image) | |
# reshape data for the model | |
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) | |
''' | |
prepare the image for the VGG model | |
When the VGG model was trained in 2014, | |
the creators subtracted the average of each | |
of the three (R,G,B) channels first, so that | |
the data for each channel had a mean of zero. | |
Furthermore, their software that expected | |
the channels to be in B,G,R order, | |
whereas Python by default uses R,G,B. | |
We need to preprocess our data to make these two changes | |
''' | |
image = preprocess_input(image) | |
# predict the probability across all output classes | |
yhat = model.predict(image) | |
# convert the probabilities to class labels | |
label = decode_predictions(yhat) | |
# retrieve the most likely result, e.g. highest probability | |
label = label[0][0] | |
# print the classification | |
print('%s (%.2f%%)' % (label[1], label[2]*100)) | |
Sample Output | |
Staffordshire_bullterrier (48.21%) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment