cog-imperial / OMLT

Represent trained machine learning models as Pyomo optimization formulations
Other
257 stars 56 forks source link

Error building MIP model from ONNX file #104

Closed hhijazi closed 1 year ago

hhijazi commented 1 year ago

I get the following error when trying to read the attached onnx file: File "/usr/local/lib/python3.10/site-packages/omlt/neuralnet/activations/relu.py", line 51, in bigm_relu_activation_constraint layer_block.z[output_index].setub(max(0, ub)) TypeError: '>' not supported between instances of 'NoneType' and 'int' MatMul_Add.onnx.zip

tsaycal commented 1 year ago

Thanks for the question @hhijazi. My colleague @juan-campos has tried:

onnx_model = onnx.load(‘MatMul_Add.onnx’)
network_definition = load_onnx_neural_network(onnx_model)

without getting issues. Are you including variable bounds? Perhaps the issue is with these.

hhijazi commented 1 year ago

I did the following:

model = onnx.load("MatMul_Add.onnx")

net = load_onnx_neural_network(model)

formulation = FullSpaceNNFormulation(net)

mip = pyo.ConcreteModel()

mip.nn = OmltBlock()

mip.nn.build_formulation(formulation)
juan-campos commented 1 year ago

@hhijazi I think the issue is that your model contains ReLU activations. In this case for the MIP reformulation you would need to add some bounds for the inputs. For example I tried :

input_bounds = {} for i in range(2): input_bounds[i] = (0, 1) model = onnx.load("MatMul_Add.onnx") network_definition = load_onnx_neural_network(model, None, input_bounds) mip = pyo.ConcreteModel() mip.nn = OmltBlock() mip.nn.build_formulation(formulation) mip.obj = pyo.Objective(expr=(-(mip.nn.outputs[0]))) pyo.SolverFactory('gurobi', solver_io="python").solve(mip, tee=True)

and it works. I hope this helps, if not let me know

Juan

hhijazi commented 1 year ago

Ok, thanks Juan! It would be nice to specify this in the doc or the Readme, or perhaps I just missed it...