Skip to content

Commit 403623c

Browse files
authored
Merge pull request #2372 from Trusted-AI/dependabot/pip/scikit-learn-gte-0.22.2-and-lt-1.4.0
Update scikit-learn requirement from <1.2.0,>=0.22.2 to >=0.22.2,<1.4.0
2 parents 044f87e + 38e0c22 commit 403623c

28 files changed

+44
-40
lines changed

.github/workflows/ci-scikit-learn.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,18 @@ jobs:
2828
fail-fast: false
2929
matrix:
3030
include:
31-
- name: scikit-learn 0.24.2 (Python 3.9)
32-
framework: scikitlearn
33-
scikit-learn: 0.24.2
34-
python: 3.9
3531
- name: scikit-learn 1.1.3 (Python 3.9)
3632
framework: scikitlearn
3733
scikit-learn: 1.1.3
3834
python: 3.9
39-
- name: scikit-learn 1.2.2 (Python 3.9)
40-
framework: scikitlearn
41-
scikit-learn: 1.2.2
42-
python: 3.9
4335
- name: scikit-learn 1.2.2 (Python 3.10)
4436
framework: scikitlearn
4537
scikit-learn: 1.2.2
4638
python: '3.10'
39+
- name: scikit-learn 1.3.2 (Python 3.10)
40+
framework: scikitlearn
41+
scikit-learn: 1.3.2
42+
python: '3.10'
4743

4844
name: ${{ matrix.name }}
4945
steps:

art/attacks/evasion/adversarial_patch/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ def insert_transformed_patch(x: np.ndarray, patch: np.ndarray, image_coords: np.
6262
height, _ = cv2.findHomography(patch_coords, image_coords)
6363

6464
# warp patch to destination coordinates
65-
x_out = cv2.warpPerspective(patch, height, (x.shape[1], x.shape[0]), cv2.INTER_CUBIC)
65+
x_out = cv2.warpPerspective(patch, height, (x.shape[1], x.shape[0]), cv2.INTER_CUBIC) # type: ignore
6666

6767
# mask to aid with insertion
6868
mask = np.ones(patch.shape)
69-
mask_out = cv2.warpPerspective(mask, height, (x.shape[1], x.shape[0]), cv2.INTER_CUBIC)
69+
mask_out = cv2.warpPerspective(mask, height, (x.shape[1], x.shape[0]), cv2.INTER_CUBIC) # type: ignore
7070

7171
# save image before adding shadows
7272
x_neg_patch = np.copy(x)

art/attacks/evasion/graphite/graphite_blackbox.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _perturb(
346346
mask_copy = mask_array.copy()
347347
x_noise = cv2.resize(x_copy, self.noise_size)
348348
x_tar_noise = cv2.resize(x_tar_copy, self.noise_size)
349-
mask_noise = cv2.resize(mask_copy, self.noise_size)
349+
mask_noise = cv2.resize(mask_copy, self.noise_size).astype(float)
350350
mask_noise = np.where(mask_noise > 0.5, 1.0, 0.0)
351351

352352
if len(x_noise.shape) < 3:

art/attacks/evasion/graphite/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def apply_transformation(
155155
table = np.empty((256), np.uint8)
156156
for i in range(256):
157157
table[i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
158-
att_uint = cv2.LUT(att_uint, table)
158+
att_uint = cv2.LUT(att_uint, table) # type: ignore
159159
att = (att_uint / 255.0).astype(np.float32)
160160
att = np.clip(att, 0.0, 1.0)
161161

@@ -227,12 +227,12 @@ def add_noise(
227227
"""
228228
import cv2
229229

230-
theta_full = cv2.resize(theta, (x.shape[1], x.shape[0]))
230+
theta_full = cv2.resize(theta, (x.shape[1], x.shape[0])).astype(float)
231231
if len(theta_full.shape) < 3:
232232
theta_full = theta_full[:, :, np.newaxis]
233233
comb = x + lbd * theta_full
234234

235-
mask_full = cv2.resize(mask, (x.shape[1], x.shape[0]))
235+
mask_full = cv2.resize(mask, (x.shape[1], x.shape[0])).astype(float)
236236
if len(mask_full.shape) < 3:
237237
mask_full = mask_full[:, :, np.newaxis]
238238
mask_full = np.where(mask_full > 0.5, 1.0, 0.0)
@@ -334,7 +334,7 @@ def transform_wb(
334334
if blur != 0:
335335
kernel = np.zeros((blur * 2 - 1, blur * 2 - 1))
336336
kernel[blur - 1, blur - 1] = 1
337-
kernel = cv2.GaussianBlur(kernel, (blur, blur), 0)
337+
kernel = cv2.GaussianBlur(kernel, (blur, blur), 0).astype(float)
338338
kernel = kernel[blur // 2 : blur // 2 + blur, blur // 2 : blur // 2 + blur]
339339
kernel = kernel[np.newaxis, :, :]
340340
kernel = np.repeat(kernel[np.newaxis, :, :, :], x_adv.size()[1], axis=0)

requirements_test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
numpy>=1.18.5,<1.27
44
scipy==1.10.1
55
matplotlib==3.7.1
6-
scikit-learn>=0.22.2,<1.2.0
6+
scikit-learn>=0.22.2,<1.4.0
77
six==1.16.0
88
Pillow==10.1.0
99
tqdm==4.66.1

tests/attacks/inference/attribute_inference/test_black_box.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def transform_feature(x):
8686
# check accuracy
8787
train_acc = np.sum(inferred_train == x_train_feature.reshape(1, -1)) / len(inferred_train)
8888
test_acc = np.sum(inferred_test == x_test_feature.reshape(1, -1)) / len(inferred_test)
89-
assert pytest.approx(0.8285, abs=0.3) == train_acc
90-
assert pytest.approx(0.8888, abs=0.3) == test_acc
89+
assert pytest.approx(0.8285, abs=0.35) == train_acc
90+
assert pytest.approx(0.8888, abs=0.35) == test_acc
9191
print(model_type, train_acc, test_acc)
9292

9393
except ARTTestException as e:
@@ -184,8 +184,8 @@ def transform_feature(x):
184184
# check accuracy
185185
train_acc = np.sum(inferred_train == x_train_feature.reshape(1, -1)) / len(inferred_train)
186186
test_acc = np.sum(inferred_test == x_test_feature.reshape(1, -1)) / len(inferred_test)
187-
assert pytest.approx(0.8285, abs=0.3) == train_acc
188-
assert pytest.approx(0.8888, abs=0.3) == test_acc
187+
assert pytest.approx(0.8285, abs=0.35) == train_acc
188+
assert pytest.approx(0.8888, abs=0.35) == test_acc
189189
print(model_type, train_acc, test_acc)
190190

191191
except ARTTestException as e:
@@ -236,8 +236,8 @@ def transform_feature(x):
236236
# check accuracy
237237
train_acc = np.sum(inferred_train == x_train_feature.reshape(1, -1)) / len(inferred_train)
238238
test_acc = np.sum(inferred_test == x_test_feature.reshape(1, -1)) / len(inferred_test)
239-
assert pytest.approx(0.8285, abs=0.3) == train_acc
240-
assert pytest.approx(0.8888, abs=0.3) == test_acc
239+
assert pytest.approx(0.8285, abs=0.35) == train_acc
240+
assert pytest.approx(0.8888, abs=0.35) == test_acc
241241
print(model_type, train_acc, test_acc)
242242

243243
except ARTTestException as e:
@@ -286,8 +286,8 @@ def transform_feature(x):
286286
# check accuracy
287287
train_acc = np.sum(inferred_train == x_train_feature.reshape(1, -1)) / len(inferred_train)
288288
test_acc = np.sum(inferred_test == x_test_feature.reshape(1, -1)) / len(inferred_test)
289-
assert pytest.approx(0.8285, abs=0.3) == train_acc
290-
assert pytest.approx(0.8888, abs=0.3) == test_acc
289+
assert pytest.approx(0.8285, abs=0.35) == train_acc
290+
assert pytest.approx(0.8888, abs=0.35) == test_acc
291291
print(model_type, train_acc, test_acc)
292292

293293
except ARTTestException as e:

tests/utils.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import unittest
2929
import warnings
3030

31+
import sklearn
3132
import numpy as np
3233

3334
from art.estimators.classification.tensorflow import TensorFlowV2Classifier
@@ -1746,6 +1747,13 @@ def get_tabular_classifier_scikit_list(clipped=False, model_list_names=None):
17461747
ScikitlearnSVC,
17471748
)
17481749

1750+
sklearn_version = list(map(int, sklearn.__version__.split(".")))
1751+
sklearn_ge_1_3_0 = sklearn_version[0] == 1 and sklearn_version[1] >= 3
1752+
if sklearn_ge_1_3_0:
1753+
suffix = "-ge-1.3.0"
1754+
else:
1755+
suffix = ""
1756+
17491757
available_models = {
17501758
"decisionTreeClassifier": ScikitlearnDecisionTreeClassifier,
17511759
# "extraTreeClassifier": ScikitlearnExtraTreeClassifier,
@@ -1775,7 +1783,7 @@ def get_tabular_classifier_scikit_list(clipped=False, model_list_names=None):
17751783
os.path.join(
17761784
os.path.dirname(os.path.dirname(__file__)),
17771785
"utils/resources/models/scikit/",
1778-
"scikit-" + model_name + "-iris-clipped.pickle",
1786+
"scikit-" + model_name + "-iris-clipped" + suffix + ".pickle",
17791787
),
17801788
"rb",
17811789
)
@@ -1788,7 +1796,7 @@ def get_tabular_classifier_scikit_list(clipped=False, model_list_names=None):
17881796
os.path.join(
17891797
os.path.dirname(os.path.dirname(__file__)),
17901798
"utils/resources/models/scikit/",
1791-
"scikit-" + model_name + "-iris-unclipped.pickle",
1799+
"scikit-" + model_name + "-iris-unclipped" + suffix + ".pickle",
17921800
),
17931801
"rb",
17941802
)

utils/resources/create_model_weights.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
1616
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1717
# SOFTWARE.
18-
import tensorflow as tf
1918
import os
2019
import pickle
20+
21+
import tensorflow as tf
2122
from tensorflow.keras.models import Sequential
2223
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
2324
from sklearn.linear_model import LogisticRegression
24-
from art.estimators.classification import SklearnClassifier
2525
from sklearn.svm import SVC, LinearSVC
2626
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier
2727
from sklearn.ensemble import AdaBoostClassifier, BaggingClassifier, ExtraTreesClassifier
@@ -139,43 +139,43 @@ def create_scikit_model_weights():
139139
"linearSVC": LinearSVC(),
140140
}
141141

142-
clipped_models = {
143-
model_name: SklearnClassifier(model=model, clip_values=(0, 1)) for model_name, model in model_list.items()
144-
}
145-
unclipped_models = {model_name: SklearnClassifier(model=model) for model_name, model in model_list.items()}
142+
clipped_models = {model_name: model for model_name, model in model_list.items()}
143+
unclipped_models = {model_name: model for model_name, model in model_list.items()}
146144

147145
(x_train_iris, y_train_iris), (_, _), _, _ = load_dataset("iris")
148146

147+
y_train_iris = np.argmax(y_train_iris, axis=1)
148+
149149
for model_name, model in clipped_models.items():
150-
model.fit(x=x_train_iris, y=y_train_iris)
150+
model.fit(X=x_train_iris, y=y_train_iris)
151151
pickle.dump(
152152
model,
153153
open(
154154
os.path.join(
155155
os.path.dirname(os.path.dirname(__file__)),
156-
"utils/resources/models/scikit/",
157-
model_name + "iris_clipped.sav",
156+
"resources/models/scikit/",
157+
"scikit-" + model_name + "-iris-clipped-ge-1.3.0.pickle",
158158
),
159159
"wb",
160160
),
161161
)
162162

163163
for model_name, model in unclipped_models.items():
164-
model.fit(x=x_train_iris, y=y_train_iris)
164+
model.fit(X=x_train_iris, y=y_train_iris)
165165
pickle.dump(
166166
model,
167167
open(
168168
os.path.join(
169169
os.path.dirname(os.path.dirname(__file__)),
170-
"utils/resources/models/scikit/",
171-
model_name + "iris_unclipped.sav",
170+
"resources/models/scikit/",
171+
"scikit-" + model_name + "-iris-unclipped-ge-1.3.0.pickle",
172172
),
173173
"wb",
174174
),
175175
)
176176

177177

178178
if __name__ == "__main__":
179-
main_mnist_binary()
179+
# main_mnist_binary()
180180
create_scikit_model_weights()
181-
main_diabetes()
181+
# main_diabetes()

0 commit comments

Comments
 (0)