Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ONNX exported models for inference #221

Merged
merged 3 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@ util/api_tester/proto/

#flatbuffers
wrappers/testing/fbs/
wrappers/python/fbs/
wrappers/python/fbs/

#ONNX example
examples/models/onnx_resnet50/cpu_codegen/Function_0_codegen.cpp
examples/models/onnx_resnet50/resnet.onnx
3 changes: 2 additions & 1 deletion docs/wrappers/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ PERSISTENCE=0
These values can also be provided or overriden on the command line when building the image.

# Step 3 - Build your image
Use ```s2i build``` to create your Docker image from source code. You will need Docker installed on the machine and optionally git if your source code is in a public git repo. You can choose from two python builder images
Use ```s2i build``` to create your Docker image from source code. You will need Docker installed on the machine and optionally git if your source code is in a public git repo. You can choose from three python builder images

* Python 2 : seldonio/seldon-core-s2i-python2:0.1
* Python 3 : seldonio/seldon-core-s2i-python3:0.1
* Python 3 plus ONNX support via [Intel nGraph](https://github.com/NervanaSystems/ngraph) : seldonio/seldon-core-s2i-python3-ngraph-onnx:0.1

Using s2i you can build directly from a git repo or from a local source folder. See the [s2i docs](https://github.com/openshift/source-to-image/blob/master/docs/cli.md#s2i-build) for further details. The general format is:

Expand Down
4 changes: 4 additions & 0 deletions examples/models/onnx_resnet50/.s2i/environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MODEL_NAME=ONNXResNet
API_TYPE=REST
SERVICE_TYPE=MODEL
PERSISTENCE=0
5 changes: 5 additions & 0 deletions examples/models/onnx_resnet50/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


clean:
rm -rf cpu_codegen
rm resnet.onnx
39 changes: 39 additions & 0 deletions examples/models/onnx_resnet50/ONNXResNet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from torch.autograd import Variable
import torch.onnx
import torchvision
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
import ngraph as ng
import numpy as np
from ngraph_onnx.onnx_importer.importer import import_onnx_file

class ONNXResNet(object):

def __init__(self):
print("Loading model")
# Import the ONNX file
models = import_onnx_file('resnet.onnx')
# Create an nGraph runtime environment
runtime = ng.runtime(backend_name='CPU')
# Select the first model and compile it to a callable function
model = models[0]
self.resnet = runtime.computation(model['output'], *model['inputs'])
print("Model loaded")

#Do a test run to warm up and check all is ok
print("Running test on img of Zebra as warmup")
img = image.load_img('zebra.jpg', target_size=(224, 224))
img = image.img_to_array(img)
x = np.expand_dims(img.copy(), axis=0)
x = preprocess_input(x,mode='torch')
x = x.transpose(0,3,1,2)
preds = self.resnet(x)
print(decode_predictions(preds, top=5))

def predict(self,X,features_names):
print(X.shape)
X = preprocess_input(X,mode='torch')
preds = self.resnet(X)
print(decode_predictions(preds, top=5))
return preds
22 changes: 22 additions & 0 deletions examples/models/onnx_resnet50/contract.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"features":[
{
"name":"x",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,1],
"shape":[3,224,224]
}
],
"targets":[
{
"name":"class",
"dtype":"FLOAT",
"ftype":"continuous",
"range":[0,1],
"shape":[100]
}
]
}


Loading