Skip to content

Instantly share code, notes, and snippets.

@mmahesh

mmahesh/check.py Secret

Last active November 21, 2017 13:15
Show Gist options
  • Save mmahesh/9368e3ab1b2febd077aefaede32f1bdd to your computer and use it in GitHub Desktop.
Save mmahesh/9368e3ab1b2febd077aefaede32f1bdd to your computer and use it in GitHub Desktop.
#First download ILSVRC2012_test_00000002.JPEG to your folder to check
# python test_imagenet_tut.py --image test_img.JPEG
# Check 55 and 56 lines and you can see the results just by commenting out the other model
# import the necessary packages
from keras.preprocessing import image as image_utils
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
import numpy as np
import argparse
import cv2
from resnet_50 import resnet50_model
from keras.applications.resnet50 import ResNet50
import tensorflow as tf
import keras.backend.tensorflow_backend as ktf
def get_session(gpu_fraction=0.333):
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
allow_growth=True)
return tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
ktf.set_session(get_session())
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
args = vars(ap.parse_args())
# load the original image via OpenCV so we can draw on it and display
# it to our screen later
orig = cv2.imread(args["image"])
# load the input image using the Keras helper utility while ensuring
# that the image is resized to 224x224 pxiels, the required input
# dimensions for the network -- then convert the PIL image to a
# NumPy array
print("[INFO] loading and preprocessing image...")
image = image_utils.load_img(args["image"], target_size=(224, 224))
image = image_utils.img_to_array(image)
# our image is now represented by a NumPy array of shape (3, 224, 224),
# but we need to expand the dimensions to be (1, 3, 224, 224) so we can
# pass it through the network -- we'll also preprocess the image by
# subtracting the mean RGB pixel intensity from the ImageNet dataset
image = np.expand_dims(image, axis=0)
image = preprocess_input(image)
# load the VGG16 network
print("[INFO] loading network...")
model = resnet50_model(img_rows=224, img_cols=224, color_type=3, num_classes=1000)
# model = ResNet50(weights='imagenet')
#this second one returns Magpie for ILSVRC2012_test_00000002.JPEG but the first model doesnt
# classify the image
print("[INFO] classifying image...")
preds = model.predict(image)
P = decode_predictions(preds)
(inID, label, prob) = P[0][0]
# display the predictions to our screen
print("ImageNet ID: {}, Label: {}".format(inID, label))
# cv2.putText(orig, "Label: {}".format(label), (10, 30),
# cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# cv2.imshow("Classification", orig)
# cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment