Skip to content

Instantly share code, notes, and snippets.

@gbaeke gbaeke/run.py
Last active Dec 30, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.