Skip to content

Instantly share code, notes, and snippets.

@iamatulsingh
Created December 28, 2019 20:26
Show Gist options
  • Save iamatulsingh/26bde609bc18aaa1113efb9566148ea5 to your computer and use it in GitHub Desktop.
Save iamatulsingh/26bde609bc18aaa1113efb9566148ea5 to your computer and use it in GitHub Desktop.
imageNet
import os
from keras.applications import ResNet50
from keras.applications import InceptionV3
from keras.applications import Xception
from keras.applications import VGG16
from keras.applications import VGG19
from keras.applications import imagenet_utils
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
from matplotlib import pyplot as plt
def predict(use_model="vgg16", image_path=r"images\137.jpg"):
MODELS = {
"vgg16": VGG16,
"vgg19": VGG19,
"inception": InceptionV3,
"xception": Xception,
"resnet": ResNet50
}
inputShape = (224, 224)
preprocess = imagenet_utils.preprocess_input
if use_model in ["inception", "xception"]:
inputShape = (299, 299)
preprocess = preprocess_input
print(f"[+] loading {use_model} ... ", end="")
selected_network = MODELS[use_model]
model = selected_network(weights="imagenet")
print("Done")
print(f"[#] processing {image_path} ... ", end="")
image = load_img(image_path, target_size=inputShape)
image = img_to_array(image)
print("Done")
image = np.expand_dims(image, axis=0)
image = preprocess(image)
print(f"[#] classifying image with '{use_model}'... ", end="")
predictions = model.predict(image)
top_predictions = imagenet_utils.decode_predictions(predictions)
print("Done")
print("\n[-] Top five predictions are:")
for (i, (imagenetID, label, prob)) in enumerate(top_predictions[0]):
print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))
(imagenetID, label, prob) = top_predictions[0][0]
return label, prob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment