This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function base64ToImg(b64string){ | |
const binaryData = Buffer.from(b64string, 'base64').toString('binary'); | |
const imgUrl = "/tmp/out.jpg" | |
await writeFile(imgUrl, binaryData, "binary"); | |
return imgUrl; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const session = new onnx.InferenceSession(); | |
await session.loadModel("model.onnx"); | |
const prediction = await session.run([inputTensor]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |