Skip to content

Instantly share code, notes, and snippets.

@gbaeke
Last active December 30, 2018 21:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gbaeke/b25849f3813e9eb984ee691659d1d05a to your computer and use it in GitHub Desktop.
Python script to perform inference with an Azure Machine Learning scoring container for ResNet50v2
import keras
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import decode_predictions
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image as image_utils
import numpy as np
import requests
import json
# channels last or first
print(keras.backend.image_data_format())
def pred(img):
# load and process the image
print("Loading and preprocessing image...", img)
print()
image = image_utils.load_img(img, target_size=(224, 224))
image = image_utils.img_to_array(image)
print("Array shape", image.shape)
# we want the channels first so (3, 224, 224)
image = np.moveaxis(image, -1, 0)
print("Array shape afer moveaxis: ", image.shape)
# the first dimension needs to be batch size
image = np.expand_dims(image, axis=0)
print("Array shape after expand_dims", image.shape)
# construct JSON payload
input_data = "{\"data\": " + str(image.tolist()) + "}"
# send the request to the scoring service created by Azure Machine Learning
headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ 'AUTHKEY')}
resp = requests.post('http://SERVICE_IP/api/v1/service/onnxgpu/score', input_data, headers=headers)
print("prediction time (as measured by the scoring container)", json.loads(resp.text)["time"])
# what is the category?
response=json.loads(resp.text)
result=np.array(response["result"])
prediction= decode_predictions(result.reshape((1,1000)))
return prediction
# predict for cat
cat=pred("cat.jpg")
print("Probably a: ",cat[0][0][1], cat[0][0][2])
# predict for car
car=pred("car.jpg")
print("Probably a: ",car[0][0][1], car[0][0][2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment