Skip to content

Instantly share code, notes, and snippets.

View naxty's full-sized avatar

naxty

  • oxolo
  • Karlsruhe, Germany
View GitHub Profile
@naxty
naxty / keras_core_ml.py
Created August 15, 2019 15:32
Keras to Core ML export
import coremltools
model = coremltools.converters.keras.convert(
model.h5',
input_names=['image'],
output_names=['output'],
class_labels=["0", "1"],
image_input_names='image',
red_bias = -1,
green_bias = -1,
blue_bias = -1,
@naxty
naxty / predict.swift
Created August 15, 2019 15:34
Predict with swift on image data
let model = model()
let inputImage = input.resize(to: CGSize(width: 224, height: 224))
guard let features = inputImage?.pixelBuffer() else {
complete(nil, "Error while creating the pixel buffer.")
return
}
guard let result = try? model.prediction(image: features) else {
complete(nil, "Error while performing the prediction.")
return
}
@naxty
naxty / onnx-dependencies.sh
Created August 26, 2019 09:08
Torch, torchvison, tensorflow, onnx, onnx-tf install
pip3 install torch torchvision tensorflow onnx onnx-tf
@naxty
naxty / pytorch_conv.py
Created August 26, 2019 09:09
Neural Network in PyTorch with two convolutional layer and two fully connected layer
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1)
@naxty
naxty / pytorch_onnx_export.py
Created August 26, 2019 09:10
Exporting a PyTorch model to onnx
from torch.autograd import Variable
trained_model = Net()
trained_model.load_state_dict(torch.load('output/mnist_cnn.pt'))
# Shape of input to the model
dummy_input = Variable(torch.randn(1, 1, 28, 28))
# Export the trained model to ONNX
torch.onnx.export(trained_model, dummy_input, "output/mnist.onnx")
@naxty
naxty / tensorflow_onnx_import.py
Created August 26, 2019 09:11
Import an onnx model in tensorflow
import onnx
from onnx_tf.backend import prepare
model = onnx.load('output/mnist.onnx')
tf_rep = prepare(model)
@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))
@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]);
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")
async function base64ToImg(b64string){
const binaryData = Buffer.from(b64string, 'base64').toString('binary');
const imgUrl = "/tmp/out.jpg"
await writeFile(imgUrl, binaryData, "binary");
return imgUrl;
}