The onnx_to_gurobi is a Python library that creates Gurobi models for neural networks in ONNX format.
Project description
Overview
The ONNX-To-Gurobi is a Python library that creates Gurobi models for neural networks in ONNX format.
The library has been designed to allow easy extensions, and it currently supports the following ONNX nodes:
- Add
- Sub
- MatMul
- Gemm
- ReLu
- Conv
- Unsqueeze
- MaxPool
- AveragePool
- BatchNormalization
- Flatten
- Identity
- Reshape
- Shape
- Concat
- Dropout
Installation
We highly recommend creating a virtual conda environment and installing the library within the environment by following the following steps:
1- Gurobi is not installed automatically. Please install it manually using:
conda install -c gurobi gurobi
2- Make sure to switch to Python 11 inside your environment using:
conda install python=11
3- Install the library using:
pip install onnxgurobi
Getting Started
The ONNXToGurobi class provides the central interface for converting an ONNX model into a Gurobi optimization model.
To get access to the class's methods and attributes, you need to import it using:
from onnx_to_gurobi.onnxToGurobi import ONNXToGurobi
The ONNXToGurobi class:
-
Parses the ONNX graph and constructs an internal representation of each operator and its corresponding tensor shapes.
-
Creates a Gurobi model along with the necessary variables and constraints.
-
Exposes all model components (decision variables, Gurobi Model object, node definitions, tensor shapes), allowing you to:
-
Set or fix input variables to specific values.
-
Introduce objectives.
-
Add your own constraints.
-
Solve the resulting MILP and then inspect or extract the outputs from the solution.
An overview of the class’s methods and attributes:
class ONNXToGurobi:
def build_model(self):
"""
Constructs the Gurobi model by creating variables and applying operator constraints.
"""
def get_gurobi_model(self):
"""
Retrieves the Gurobi model after all constraints have been added.
Returns:
gurobipy.Model: The constructed Gurobi model reflecting the ONNX graph.
"""
# Attributes:
self.model # The Gurobi Model object
self.variables # A dict mapping tensor names to Gurobi variables (or constants)
self.in_out_tensors_shapes # Shapes of all input and output tensors
self.nodes # Node definitions parsed from ONNX
self.initializers # Constant tensors extracted from the ONNX graph
How to Use
See example1.py for a simple example. See example2.py for a detailed adversarial example.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file onnxgurobi-0.1.3.tar.gz.
File metadata
- Download URL: onnxgurobi-0.1.3.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d86fd375feca2e3c2eb0eee50e42c2bacda2696ce0ce3c35f0a695ab5f936207
|
|
| MD5 |
a26be14daee5d07fc0f6d2123fbdf400
|
|
| BLAKE2b-256 |
6cca30e41002a6e2ef917a2c4a88b81548d57b44a3810b46a53db39b19e5cd72
|
File details
Details for the file onnxgurobi-0.1.3-py3-none-any.whl.
File metadata
- Download URL: onnxgurobi-0.1.3-py3-none-any.whl
- Upload date:
- Size: 52.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21c993a6f2fc324aba9f81be088188c0764d626cf0567adc8529bd2418965f49
|
|
| MD5 |
3d21f8b336ad59020e1702b6bc0ec6c4
|
|
| BLAKE2b-256 |
424f5c93cc72bf0b23f4333e81530bb6807a851bdb4759eca6a95baa50e49de1
|