Skip to content

Commit

Permalink
Included keras legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertSamoilescu committed Nov 29, 2024
1 parent 9eeb657 commit eee40d5
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 22 deletions.
5 changes: 2 additions & 3 deletions alibi/datasets/tensorflow.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Tuple, Union

import tensorflow.keras as keras
import numpy as np
from typing import Tuple, Union

import alibi.utils.legacy_keras as keras
from alibi.utils.data import Bunch


Expand Down
2 changes: 1 addition & 1 deletion alibi/explainers/backends/tensorflow/cfrl_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras

from alibi.explainers.backends.cfrl_base import CounterfactualRLDataset
from alibi.models.tensorflow.actor_critic import Actor, Critic
Expand Down
4 changes: 2 additions & 2 deletions alibi/explainers/tests/test_anchor_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def func(image: np.ndarray) -> np.ndarray:

@pytest.mark.parametrize('predict_fn', [lazy_fixture('models'), ], indirect=True)
@pytest.mark.parametrize('models',
[("mnist-cnn-tf2.2.0",), ("mnist-cnn-tf1.15.2.h5",), ("mnist-cnn-pt1.9.1.pt",)],
[("mnist-cnn-tf2.18.0.keras",), ("mnist-cnn-pt1.9.1.pt",)],
indirect=True,
ids='models={}'.format
)
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_sampler(predict_fn, models, mnist_data):

@pytest.mark.parametrize('predict_fn', [lazy_fixture('models'), ], indirect=True)
@pytest.mark.parametrize('models',
[("mnist-cnn-tf2.2.0",), ("mnist-cnn-tf1.15.2.h5",), ("mnist-cnn-pt1.9.1.pt",)],
[("mnist-cnn-tf2.18.0.keras",), ("mnist-cnn-pt1.9.1.pt",)],
indirect=True,
ids='models={}'.format
)
Expand Down
4 changes: 2 additions & 2 deletions alibi/explainers/tests/test_cfproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def tf_keras_iris_explainer(request, models, iris_data):
((True, 1., 3), True, 2)],
indirect=['tf_keras_iris_explainer'])
@pytest.mark.parametrize('models',
[('iris-ffn-tf2.2.0', 'iris-ae-tf2.2.0', 'iris-enc-tf2.2.0'),
[('iris-ffn-tf2.18.0.keras', 'iris-ae-tf2.18.0.keras', 'iris-enc-tf2.18.0.keras'),
('iris-ffn-tf1.15.2.h5', 'iris-ae-tf1.15.2.h5', 'iris-enc-tf1.15.2.h5')],
indirect=True)
def test_tf_keras_iris_explainer(disable_tf2, iris_data, tf_keras_iris_explainer, use_kdtree, k):
Expand Down Expand Up @@ -101,7 +101,7 @@ def tf_keras_adult_explainer(request, models, adult_data):
((True, 1., 3), True, 2, 'abdm')],
indirect=['tf_keras_adult_explainer'])
@pytest.mark.parametrize('models',
[('adult-ffn-tf2.2.0',), ('adult-ffn-tf1.15.2.h5',)],
[('adult-ffn-tf2.18.0.keras',), ('adult-ffn-tf1.15.2.h5',)],
ids='model={}'.format,
indirect=True)
def test_tf_keras_adult_explainer(disable_tf2, adult_data, tf_keras_adult_explainer, use_kdtree, k, d_type):
Expand Down
11 changes: 6 additions & 5 deletions alibi/explainers/tests/test_cfrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
from numpy.testing import assert_allclose
import tensorflow as tf
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras

from alibi.explainers import CounterfactualRLTabular
from alibi.explainers.backends.cfrl_base import get_hard_distribution
Expand Down Expand Up @@ -295,7 +295,7 @@ def call(self, input: Union[tf.Tensor, List[tf.Tensor]], **kwargs):
return explainer


@pytest.mark.parametrize('models', [('iris-ae-tf2.2.0', 'iris-enc-tf2.2.0')], ids='model={}'.format, indirect=True)
@pytest.mark.parametrize('models', [('iris-ae-tf2.18.0.keras', 'iris-enc-tf2.18.0.keras')], ids='model={}'.format, indirect=True)
@pytest.mark.parametrize('rf_classifier',
[lazy_fixture('iris_data')],
indirect=True,
Expand All @@ -314,11 +314,12 @@ def test_explainer(tf_keras_iris_explainer, iris_data):
X_hat = decoder(Z)
assert isinstance(X_hat, list)

# Fit the explainer
# Fit the explainer#
print(iris_data['X_train'].shape)
explainer.fit(X=iris_data["X_train"])

# Construct explanation object.
explainer.explain(X=iris_data["X_test"], Y_t=np.array([2]), C=None)
# # Construct explanation object.
# explainer.explain(X=iris_data["X_test"], Y_t=np.array([2]), C=None)


@pytest.mark.parametrize('backend', ['tensorflow', 'pytorch'])
Expand Down
2 changes: 1 addition & 1 deletion alibi/explainers/tests/test_counterfactual.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_cf_explainer_iris(disable_tf2, cf_iris_explainer):
ids='target={}'.format,
indirect=True)
@pytest.mark.parametrize('models',
[('mnist-logistic-tf2.2.0',), ('mnist-logistic-tf1.15.2.h5',)],
[('mnist-logistic-tf2.18.0.keras',), ('mnist-logistic-tf1.15.2.h5',)],
ids='model={}'.format,
indirect=True)
def test_keras_mnist_explainer(disable_tf2, keras_mnist_cf_explainer, mnist_data):
Expand Down
2 changes: 1 addition & 1 deletion alibi/models/tensorflow/actor_critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import tensorflow as tf
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras


class Actor(keras.Model):
Expand Down
2 changes: 1 addition & 1 deletion alibi/models/tensorflow/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import tensorflow as tf
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras
from typing import List, Tuple, Union


Expand Down
2 changes: 1 addition & 1 deletion alibi/models/tensorflow/cfrl_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import tensorflow as tf
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras
from typing import List


Expand Down
2 changes: 1 addition & 1 deletion alibi/models/tensorflow/tests/test_autoencoder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pytest
import tensorflow.keras as keras
import alibi.utils.legacy_keras as keras
from alibi.models.tensorflow.autoencoder import HeAE


Expand Down
2 changes: 1 addition & 1 deletion alibi/tests/test_saving.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def ffn_classifier(request):

@pytest.fixture(scope='module')
def mnist_predictor():
model = alibi_testing.load('mnist-cnn-tf2.2.0')
model = alibi_testing.load('mnist-cnn-tf2.18.0.keras')
predictor = lambda x: model.predict(x) # noqa
return predictor

Expand Down
4 changes: 4 additions & 0 deletions alibi/utils/legacy_keras.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"

from tensorflow.keras import * # Re-export all tensorflow.keras symbols
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ def readme():

# `keras 3` becomes the default for `tensorflow >= 2.16.0``
# which is not yet supported by `transformers`
'tensorflow': ['tensorflow>=2.0.0, !=2.6.0, !=2.6.1, <2.16.0'],
'tensorflow': [
'tensorflow>=2.0.0, !=2.6.0, !=2.6.1, <2.19.0',
'tf-keras>=2.0.0,<2.19.0'
],
'torch': ['torch>=1.9.0, <3.0.0'],
'all': [
'ray>=0.8.7, <3.0.0',
'shap>=0.40.0, <0.44.0',
'numba>=0.50.0, !=0.54.0, <0.60.0',
'tensorflow>=2.0.0, !=2.6.0, !=2.6.1, <2.16.0',
'torch>=1.9.0, <3.0.0'
'tensorflow>=2.0.0, !=2.6.0, !=2.6.1, <2.19.0',
'tf-keras>=2.0.0,<2.19.0'
'torch>=1.9.0, <3.0.0',
]
}

Expand Down

0 comments on commit eee40d5

Please sign in to comment.