Skip to content

Instantly share code, notes, and snippets.

View naxty's full-sized avatar

naxty

  • oxolo
  • Karlsruhe, Germany
View GitHub Profile
@naxty
naxty / Dockerfile
Created September 9, 2019 06:04
Dockerfile for Seldon Core Python Wrapper
FROM python:3.7-slim
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN mkdir /app
WORKDIR /app
RUN mkdir model
COPY model/model.onnx model/model.onnx
COPY EmotionModel.py EmotionModel.py
@naxty
naxty / EmotionModel.py
Created September 9, 2019 06:03
Seldon Core Python Wrapper for Emotion Detections
from ngraph_onnx.onnx_importer.importer import import_onnx_file
import ngraph as ng
import numpy as np
class EmotionModel(object):
def __init__(self):
model = import_onnx_file("model/model.onnx")
runtime = ng.runtime(backend_name="CPU")
self.inference = runtime.computation(model)
@naxty
naxty / ngraph_inference.py
Created September 2, 2019 08:10
nGraph Inference snippet of an ONNX model
from ngraph_onnx.onnx_importer.importer import import_onnx_file
import ngraph as ng
model = import_onnx_file('model.onnx')
# Create an nGraph runtime environment
runtime = ng.runtime(backend_name='CPU')
# Compile the model to callable function
emotion_cnn = runtime.computation(model)
# Run the prediction
emotion_cnn(tensor)
const input = require("./lib/input");
const process = require("./lib/process");
const inference = require('./lib/inference');
module.exports.predict = async (event) => {
let body = JSON.parse(event.body);
let base64String = body.image;
let img = await input(base64String);
let preprocessedData = await process(img.data, 224, 224);
let predictions = await inference.predict(preprocessedData);
const dataFromImage = ndarray(new Float32Array(data), [width, height, 4]);
ops.divseq(dataFromImage, 255);
ops.subseq(dataFromImage.pick(0, null, null), 0.485);
ops.divseq(dataFromImage.pick(0, null, null), 0.229);
ops.subseq(dataFromImage.pick(1, null, null), 0.456);
ops.divseq(dataFromImage.pick(1, null, null), 0.224);
ops.subseq(dataFromImage.pick(2, null, null), 0.406);
ops.divseq(dataFromImage.pick(2, null, null), 0.225);
# [224,224,4] => [1,3,224,224]
const { createCanvas, loadImage } = require('canvas')
async function getImageData(url, modelWidth, modelHeight) {
let canvas = createCanvas(modelWidth, modelHeight);
let ctx = canvas.getContext('2d');
await loadImage(url).then((image) => {
ctx.drawImage(image, 0, 0)
})
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
return imageData;
async function base64ToImg(b64string){
const binaryData = Buffer.from(b64string, 'base64').toString('binary');
const imgUrl = "/tmp/out.jpg"
await writeFile(imgUrl, binaryData, "binary");
return imgUrl;
}
model = FixResNet50(models.resnet.Bottleneck, [3, 4, 6, 3])
from torch.utils.model_zoo import load_url as load_state_dict_from_url
state_dict = load_state_dict_from_url(model_url,
progress=True)
model.load_state_dict(state_dict)
from torch.autograd import Variable
dummy_input = Variable(torch.randn(1, 3, 224, 224))
torch.onnx.export(model, dummy_input, "model.onnx")
@naxty
naxty / inference_onnx.js
Created September 1, 2019 15:48
Inference with ONNX.js
const session = new onnx.InferenceSession();
await session.loadModel("model.onnx");
const prediction = await session.run([inputTensor]);
@naxty
naxty / tensorflow_onnx_predict.py
Created August 26, 2019 09:12
Prediction with onnx model
import numpy as np
from IPython.display import display
from PIL import Image
img = Image.open('images/five.png').resize((28, 28)).convert('L')
display(img)
output = tf_rep.run(np.asarray(img, dtype=np.float32)[np.newaxis, np.newaxis, :, :])
print('The digit is classified as ', np.argmax(output))