Skip to main content

The deep learning models convertor

Project description


ONNX to Keras deep neural network converter.

GitHub License Python Version Downloads PyPI


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: 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()

    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",

    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 :)


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.

Source Distribution

onnx2keras-0.0.11.tar.gz (13.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page