Skip to content

Commit

Permalink
-- Bulk commit releaseing ONNXMLTools
Browse files Browse the repository at this point in the history
  • Loading branch information
Shauheen Zahirazami committed Feb 23, 2018
1 parent ddef3ce commit 192eb17
Show file tree
Hide file tree
Showing 136 changed files with 11,448 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,2 @@
include onnxmltools/utils/*.js
include onnxmltools/utils/*.css
43 changes: 43 additions & 0 deletions README.md
@@ -0,0 +1,43 @@
<p align="center"><img width="40%" src="docs/ONNXMLTools_logo_main.png" /></p>

# Introduction
ONNXMLTools enables you to convert models from different machine learning toolkits into [ONNX](https://onnx.ai). Currently the following toolkits are supported:
* Apple CoreML
* scikit-learn (subset of models convertible to ONNX)

# Getting Started
Clone this repository on your local machine.

## Install
Currently you can install ONNXMLTools from source:
```
pip install git+https://github.com/onnx/onnxmltools
```

## Dependancies
This package uses NumPy as well as ProtoBuf. Also If you are converting a model from Scikit-learn or Apple CoreML you need the following packages installed respectively:
1. scikit-learn
2. CoreMLTools

## Example
Here is a simple example to convert a CoreML model:
```
import onnxmltools
import coremltools
model_coreml = coremltools.utils.load_spec("image_recognition.mlmodel")
model_onnx = onnxmltools.convert.convert_coreml(model_coreml, "Image_Reco")
# Save as text
onnxmltools.utils.save_text(model_onnx, "image_recognition.json")
# Save as protobuf
onnxmltools.utils.save_model(model_onnx, "image_recognition.onnx")
```


# License
[MIT License](LICENSE)

## Acknowledgments
The initial version of this package was developed by the following engineers and data scientists at Microsoft during winter 2017: Zeeshan Ahmed, Wei-Sheng Chin, Aidan Crook, Xavier Dupre, Costin Eseanu, Tom Finley, Lixin Gong, Scott Inglis, Pei Jiang, Ivan Matantsev, Prabhat Roy, M. Zeeshan Siddiqui, Shouheng Yi, Shauheen Zahirazami, Yiwen Zhu.
61 changes: 61 additions & 0 deletions README.rst
@@ -0,0 +1,61 @@

.. image:: https://...../docs/ONNXMLTools_logo_main.png

Introduction
============

ONNXMLTools enables you to convert models from different machine
learning toolkits into `ONNX <https://onnx.ai>`_.
Currently the following toolkits are supported:

* Apple CoreML
* scikit-learn
(subset of models convertible to ONNX)

Install
=======

::

pip install git+https://github.com/onnx/onnxmltools

Dependancies
============

`scikit-learn <http://scikit-learn.org/stable/>`_ is needed to convert
a scikit-learn model, `coremltools <https://pypi.python.org/pypi/coremltools>`_
for Apple CoreML.

Example
=======

Here is a simple example to convert a CoreML model:


::

import onnxmltools
import coremltools

model_coreml = coremltools.utils.load_spec("image_recognition.mlmodel")
model_onnx = onnxmltools.convert.convert_coreml(model_coreml, "Image_Reco")

# Save as text
onnxmltools.utils.save_text(model_onnx, "image_recognition.json")

# Save as protobuf
onnxmltools.utils.save_model(model_onnx, "image_recognition.onnx")

License
=======

MIT License

Acknowledgments
===============

The initial version of this package was developed by the following
developers and data scientists at Microsoft during winter 2017:
Zeeshan Ahmed, Wei-Sheng Chin, Aidan Crook, Xavier Dupre, Costin Eseanu,
Tom Finley, Lixin Gong, Scott Inglis, Pei Jiang, Ivan Matantsev,
Prabhat Roy, M. Zeeshan Siddiqui, Shouheng Yi, Shauheen Zahirazami, Yiwen Zhu.
Binary file added docs/ONNXMLTools_logo_main.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions onnxmltools/.gitignore
@@ -0,0 +1 @@
/__init__.py
2 changes: 2 additions & 0 deletions onnxmltools/convert/__init__.py
@@ -0,0 +1,2 @@
from .main import convert_coreml
from .main import convert_sklearn
62 changes: 62 additions & 0 deletions onnxmltools/convert/common/ConvertContext.py
@@ -0,0 +1,62 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------

class ConvertContext:
'''
The ConvertContext provides data about the conversion, specifically keeping a mapping of the old->new names as
well as provides helper functions for generating unique names
'''

#Named mapping that allows for specifying an override to the name of the node
_name_override_map = {
'ArrayFeatureExtractor':'AFE',
'DictVectorizer':'DV',
'FeatureVectorizer': 'FV',
'OneHotEncoder': 'OHE'
}

def __init__(self):
self._unique_name_set = set()
self.top_level_inputs = []
self.top_level_outputs = []

def get_unique_name(self, name):
return self.__generate_name(name)

def __generate_name(self, name):
if name in self._name_override_map:
_name = self._name_override_map[name]
else:
_name = name

count = 1
gen_name = _name
while gen_name in self._unique_name_set:
gen_name = "{}.{}".format(_name, count)
count += 1
self._unique_name_set.add(gen_name)
return gen_name


class ExtendedConvertContext(ConvertContext):
'''
The ConvertContext provides data about the conversion, specifically keeping a mapping of the old->new names as
well as provides helper functions for generating unique names
'''

def __init__(self):
ConvertContext.__init__(self)
self._outputs = []

@property
def outputs(self):
return self._outputs

def add_output(self, output):
self._outputs.append(output)

def clear_outputs(self):
self._outputs.clear()
54 changes: 54 additions & 0 deletions onnxmltools/convert/common/ModelBuilder.py
@@ -0,0 +1,54 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------

import sys
from uuid import uuid4
from ..common import model_util
from ...proto import onnx_proto
from ... import __domain__
from ... import __producer__
from ... import __producer_version__
from ... import __model_version__


class ModelBuilder:
def __init__(self, name=None, doc_string=''):
self._name = str(uuid4().hex) if name is None else name
self._doc_string = doc_string
self._inputs = []
self._outputs = []
self._nodes = []
self._initializers = []
self._values = []

def add_inputs(self, inputs):
self._inputs.extend(inputs)

def add_outputs(self, outputs):
self._outputs.extend(outputs)

def add_nodes(self, nodes):
self._nodes.extend(nodes)

def add_initializers(self, initializers):
self._initializers.extend(initializers)

def add_values(self, values):
self._values.extend(values)

def make_model(self):
return model_util.make_model(self._name,
onnx_proto.IR_VERSION,
__producer__,
__producer_version__,
__domain__,
__model_version__,
self._doc_string,
self._nodes,
self._inputs,
self._outputs,
self._values,
self._initializers)
64 changes: 64 additions & 0 deletions onnxmltools/convert/common/Node.py
@@ -0,0 +1,64 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------

class Node:
def __init__(self,
onnx_node,
inputs,
outputs,
initializers,
values):

self._onnx_node = onnx_node
self._inputs = inputs
self._outputs = outputs
self._initializers = initializers
self._values = values

@property
def name(self):
return self._onnx_node.name

@property
def attributes(self):
return self._onnx_node.attribute

@property
def input_names(self):
return self._onnx_node.input

@input_names.setter
def input_names(self, value):
self._onnx_node.input = value

@property
def output_names(self):
return self._onnx_node.output

@output_names.setter
def output_names(self, value):
self._onnx_node.output = value

@property
def inputs(self):
return self._inputs

@property
def outputs(self):
return self._outputs

@property
def initializers(self):
return self._initializers

@property
def values(self):
return self._values

@property
def onnx_node(self):
return self._onnx_node

0 comments on commit 192eb17

Please sign in to comment.