Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching test models to Keras3 format #146

Merged
merged 10 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
python-version: ["3.8", "3.9", "3.10"]
python-version: ["3.9", "3.10", "3.11", "3.12"]

steps:
- uses: "actions/checkout@v2"
Expand Down Expand Up @@ -49,4 +49,4 @@ jobs:
- name: "Upload coverage to Codecov"
uses: "codecov/codecov-action@v4"
with:
fail_ci_if_error: true
fail_ci_if_error: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
.vscode/settings.json
256 changes: 134 additions & 122 deletions docs/notebooks/neuralnet/auto-thermal-reformer-relu.ipynb

Large diffs are not rendered by default.

414 changes: 218 additions & 196 deletions docs/notebooks/neuralnet/auto-thermal-reformer.ipynb

Large diffs are not rendered by default.

236 changes: 112 additions & 124 deletions docs/notebooks/neuralnet/graph_neural_network_formulation.ipynb

Large diffs are not rendered by default.

418 changes: 225 additions & 193 deletions docs/notebooks/neuralnet/import_network.ipynb

Large diffs are not rendered by default.

711 changes: 544 additions & 167 deletions docs/notebooks/neuralnet/mnist_example_convolutional.ipynb

Large diffs are not rendered by default.

774 changes: 509 additions & 265 deletions docs/notebooks/neuralnet/mnist_example_dense.ipynb

Large diffs are not rendered by default.

1,446 changes: 709 additions & 737 deletions docs/notebooks/neuralnet/neural_network_formulations.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ testing =
linear-tree
matplotlib
pandas
keras==2.9.0
keras>=3.0
onnx
onnxruntime
onnxmltools
Expand Down
8 changes: 4 additions & 4 deletions tests/io/test_keras_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
not keras_available, reason="Test only valid when keras is available"
)
def test_keras_reader(datadir):
nn = keras.models.load_model(datadir.file("keras_linear_131"), compile=False)
nn = keras.models.load_model(datadir.file("keras_linear_131.keras"), compile=False)
net = load_keras_sequential(nn)

layers = list(net.layers)
Expand All @@ -21,7 +21,7 @@ def test_keras_reader(datadir):
assert layers[2].weights.shape == (3, 1)

nn = keras.models.load_model(
datadir.file("keras_linear_131_sigmoid"), compile=False
datadir.file("keras_linear_131_sigmoid.keras"), compile=False
)
net = load_keras_sequential(nn)
layers = list(net.layers)
Expand All @@ -32,7 +32,7 @@ def test_keras_reader(datadir):
assert layers[2].weights.shape == (3, 1)

nn = keras.models.load_model(
datadir.file("keras_linear_131_sigmoid_output_activation"), compile=False
datadir.file("keras_linear_131_sigmoid_output_activation.keras"), compile=False
)
net = load_keras_sequential(nn)
layers = list(net.layers)
Expand All @@ -42,7 +42,7 @@ def test_keras_reader(datadir):
assert layers[1].weights.shape == (1, 3)
assert layers[2].weights.shape == (3, 1)

nn = keras.models.load_model(datadir.file("big"), compile=False)
nn = keras.models.load_model(datadir.file("big.keras"), compile=False)
net = load_keras_sequential(nn)
layers = list(net.layers)
assert len(layers) == 5
Expand Down
Binary file not shown.
Binary file removed tests/models/big/saved_model.pb
Binary file not shown.
Binary file removed tests/models/big/variables/variables.index
Binary file not shown.
Binary file added tests/models/keras_linear_131.keras
Binary file not shown.
Binary file removed tests/models/keras_linear_131/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/models/keras_linear_131_relu.keras
Binary file not shown.
Binary file removed tests/models/keras_linear_131_relu/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/models/keras_linear_131_sigmoid.keras
Binary file not shown.
Binary file removed tests/models/keras_linear_131_sigmoid/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/models/keras_linear_2353.keras
Binary file not shown.
Binary file removed tests/models/keras_linear_2353/saved_model.pb
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 16 additions & 14 deletions tests/neuralnet/test_keras.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,54 +108,56 @@ def _test_keras_linear_big(keras_fname, reduced_space=False):

@pytest.mark.skipif(not keras_available, reason="Need keras for this test")
def test_keras_linear_131_full(datadir):
_test_keras_linear_131(datadir.file("keras_linear_131"))
_test_keras_linear_131(datadir.file("keras_linear_131_sigmoid"))
_test_keras_linear_131(datadir.file("keras_linear_131_sigmoid_output_activation"))
_test_keras_linear_131(datadir.file("keras_linear_131.keras"))
_test_keras_linear_131(datadir.file("keras_linear_131_sigmoid.keras"))
_test_keras_linear_131(
datadir.file("keras_linear_131_sigmoid_softplus_output_activation")
datadir.file("keras_linear_131_sigmoid_output_activation.keras")
)
_test_keras_linear_131(
datadir.file("keras_linear_131_sigmoid_softplus_output_activation.keras")
)


@pytest.mark.skipif(not keras_available, reason="Need keras for this test")
def test_keras_linear_131_reduced(datadir):
_test_keras_linear_131(datadir.file("keras_linear_131"), reduced_space=True)
_test_keras_linear_131(datadir.file("keras_linear_131.keras"), reduced_space=True)
_test_keras_linear_131(
datadir.file("keras_linear_131_sigmoid"),
datadir.file("keras_linear_131_sigmoid.keras"),
reduced_space=True,
)
_test_keras_linear_131(
datadir.file("keras_linear_131_sigmoid_output_activation"),
datadir.file("keras_linear_131_sigmoid_output_activation.keras"),
reduced_space=True,
)
_test_keras_linear_131(
datadir.file("keras_linear_131_sigmoid_softplus_output_activation"),
datadir.file("keras_linear_131_sigmoid_softplus_output_activation.keras"),
reduced_space=True,
)


@pytest.mark.skipif(not keras_available, reason="Need keras for this test")
def test_keras_linear_131_relu(datadir):
_test_keras_mip_relu_131(
datadir.file("keras_linear_131_relu"),
datadir.file("keras_linear_131_relu.keras"),
)
_test_keras_complementarity_relu_131(
datadir.file("keras_linear_131_relu"),
datadir.file("keras_linear_131_relu.keras"),
)


@pytest.mark.skipif(not keras_available, reason="Need keras for this test")
def test_keras_linear_big(datadir):
_test_keras_linear_big(datadir.file("big"), reduced_space=False)
_test_keras_linear_big(datadir.file("big.keras"), reduced_space=False)


@pytest.mark.skip("Skip - this test is too big for now")
def test_keras_linear_big_reduced_space(datadir):
_test_keras_linear_big("./models/big", reduced_space=True)
_test_keras_linear_big("./models/big.keras", reduced_space=True)


@pytest.mark.skipif(not keras_available, reason="Need keras for this test")
def test_scaling_NN_block(datadir):
NN = keras.models.load_model(datadir.file("keras_linear_131_relu"))
NN = keras.models.load_model(datadir.file("keras_linear_131_relu.keras"))

model = pyo.ConcreteModel()

Expand Down Expand Up @@ -186,7 +188,7 @@ def obj(mdl):
result = pyo.SolverFactory("cbc").solve(model, tee=False)

x_s = (x - scale_x[0]) / scale_x[1]
y_s = NN.predict(x=[x_s])
y_s = NN.predict([np.array((x_s,))])
y = y_s * scale_y[1] + scale_y[0]

assert y - pyo.value(model.nn.outputs[0]) <= 1e-3
23 changes: 13 additions & 10 deletions tests/neuralnet/train_keras_models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest
import tensorflow.keras as keras
import keras

# from conftest import get_neural_network_data
from keras.layers import Conv2D, Dense
from keras.models import Model, Sequential
from pyomo.common.fileutils import this_file_dir
from tensorflow.keras.optimizers import Adamax
from keras.optimizers import Adamax

from omlt.io import write_onnx_model_with_bounds

Expand Down Expand Up @@ -40,7 +40,7 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/keras_linear_131")
nn.save(this_file_dir() + "/models/keras_linear_131.keras")

x, y, x_test = get_neural_network_data("131")
nn = Sequential(name="keras_linear_131_sigmoid")
Expand Down Expand Up @@ -72,7 +72,7 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/keras_linear_131_sigmoid")
nn.save(this_file_dir() + "/models/keras_linear_131_sigmoid.keras")

x, y, x_test = get_neural_network_data("131")
nn = Sequential(name="keras_linear_131_sigmoid_output_activation")
Expand Down Expand Up @@ -105,7 +105,9 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/keras_linear_131_sigmoid_output_activation")
nn.save(
this_file_dir() + "/models/keras_linear_131_sigmoid_output_activation.keras"
)

x, y, x_test = get_neural_network_data("131")
nn = Sequential(name="keras_linear_131_relu")
Expand Down Expand Up @@ -137,7 +139,7 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/keras_linear_131_relu")
nn.save(this_file_dir() + "/models/keras_linear_131_relu.keras")

x, y, x_test = get_neural_network_data("131")
nn = Sequential(name="keras_linear_131_relu_output_activation")
Expand Down Expand Up @@ -170,7 +172,7 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/keras_linear_131_relu_output_activation")
nn.save(this_file_dir() + "/models/keras_linear_131_relu_output_activation.keras")

x, y, x_test = get_neural_network_data("131")
nn = Sequential(name="keras_linear_131_sigmoid_softplus_output_activation")
Expand Down Expand Up @@ -204,7 +206,8 @@ def train_models():
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(
this_file_dir() + "/models/keras_linear_131_sigmoid_softplus_output_activation"
this_file_dir()
+ "/models/keras_linear_131_sigmoid_softplus_output_activation.keras"
)

x, y, x_test = get_neural_network_data("131")
Expand Down Expand Up @@ -263,7 +266,7 @@ def train_models():
history = nn.fit(
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)
nn.save(this_file_dir() + "/models/big")
nn.save(this_file_dir() + "/models/big.keras")

x, y, x_test = get_neural_network_data("2353")
nn = Sequential(name="keras_linear_2353")
Expand Down Expand Up @@ -306,7 +309,7 @@ def train_models():
x=x, y=y, validation_split=0.2, batch_size=16, verbose=1, epochs=15
)

nn.save(this_file_dir() + "/models/keras_linear_2353")
nn.save(this_file_dir() + "/models/keras_linear_2353.keras")


def train_conv():
Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

[tox]
minversion = 3.15
envlist = py36, py37, py38, py39, py310, lint
envlist = py36, py37, py38, py39, py310, py311, py312, lint

[gh-actions]
python =
3.6: py36
3.7: py37
3.8: lint, py38
3.9: py39
3.8: py38
3.9: lint, py39
3.10: py310
3.11: py311
3.12: py312

[testenv]
deps = pytest
Expand Down
Loading