Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

The deep learning models convertor

Project description

onnx2keras

ONNX to Keras deep neural network converter.

GitHub License Python Version Downloads PyPI

Requirements

Tensorflow 2.0 (also tested with 1.13).

API

onnx_to_keras(onnx_model, input_names, input_shapes=None, name_policy=None, verbose=True, change_ordering=False)

onnx_model: ONNX model to convert

input_names: list with graph input names

input_shapes: override input shapes (experimental)

name_policy: ['renumerate', 'short', 'default'] override layer names (experimental)

verbose: detailed output

change_ordering change ordering to HWC (experimental)

Return: Keras model

Getting started

ONNX model

import onnx
from onnx2keras import onnx_to_keras

# Load ONNX model
onnx_model = onnx.load('resnet18.onnx')

# Call the converter (input - is the main model input name, can be different for your model)
k_model = onnx_to_keras(onnx_model, ['input'])

Keras model will be stored to the k_model variable. So simple, isn't it?

PyTorch model

Using ONNX as intermediate format, you can convert PyTorch model as well.

import numpy as np
import torch
import onnx
from torchvision.models.resnet import resnet18
from onnx2keras import onnx_to_keras, check_torch_keras_error


if __name__ == '__main__':
    model = resnet18()
    model.eval()

    input_np = np.random.uniform(0, 1, (1, 3, 224, 224))
    input_var = torch.FloatTensor(input_np)
    output = model(input_var)

    torch.onnx.export(model, (input_var), "resnet18.onnx",
                      verbose=True,
                      input_names=['input'],
                      output_names=['output']
    )

    onnx_model = onnx.load('resnet18.onnx')
    k_model = onnx_to_keras(onnx_model, ['input'])

    error = check_torch_keras_error(model, k_model, input_np)

    print('Error: {0}'.format(error))  #  1e-6 :)

License

This software is covered by MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for onnx2keras, version 0.0.14
Filename, size File type Python version Upload date Hashes
Filename, size onnx2keras-0.0.14.tar.gz (14.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page