|
30 | 30 | from tqdm.auto import trange |
31 | 31 |
|
32 | 32 | import numpy as np |
33 | | -import tensorflow as tf |
34 | 33 |
|
35 | 34 | from art.estimators.estimator import BaseEstimator, NeuralNetworkMixin |
36 | 35 | from art.estimators.classification.classifier import ClassifierMixin |
37 | 36 | from art.attacks.attack import EvasionAttack |
38 | 37 |
|
39 | 38 | if TYPE_CHECKING: |
| 39 | + # pylint: disable=C0412 |
| 40 | + import tensorflow as tf |
40 | 41 | from art.utils import CLASSIFIER_NEURALNETWORK_TYPE |
41 | 42 |
|
42 | 43 | logger = logging.getLogger(__name__) |
@@ -212,6 +213,8 @@ def generate_mask( |
212 | 213 | of perturbation region. |
213 | 214 | :return mask: Array with 1s on the features we will modify on this batch and 0s elsewhere. |
214 | 215 | """ |
| 216 | + import tensorflow as tf # lgtm [py/repeated-import] |
| 217 | + |
215 | 218 | mask = np.zeros_like(x) |
216 | 219 | for i in range(len(x)): |
217 | 220 | if y[i] == 1: |
@@ -252,6 +255,8 @@ def update_embeddings(self, embeddings: "tf.Tensor", gradients: "tf.Tensor", mas |
252 | 255 | :param mask: Tensor with 1s on the embeddings we modify, 0s elsewhere. |
253 | 256 | :return embeddings: Updated embeddings wrt the adversarial objective. |
254 | 257 | """ |
| 258 | + import tensorflow as tf # lgtm [py/repeated-import] |
| 259 | + |
255 | 260 | if self.use_sign: |
256 | 261 | gradients = tf.sign(gradients) |
257 | 262 | embeddings = embeddings + self.l_r * gradients * mask |
@@ -282,6 +287,8 @@ def get_adv_malware( |
282 | 287 |
|
283 | 288 | :return data: Numpy array with valid data samples. |
284 | 289 | """ |
| 290 | + import tensorflow as tf # lgtm [py/repeated-import] |
| 291 | + |
285 | 292 | for i, label in enumerate(labels): |
286 | 293 | if label == 1: |
287 | 294 | total_diff = 0 |
@@ -490,6 +497,8 @@ def generate( # pylint: disable=W0221 |
490 | 497 | the start of perturbation region. |
491 | 498 | :return x: our adversarial examples. |
492 | 499 | """ |
| 500 | + import tensorflow as tf # lgtm [py/repeated-import] |
| 501 | + |
493 | 502 | # make copy so original data is not modified. |
494 | 503 | adv_x = x.copy() |
495 | 504 | if sample_sizes is None: |
|
0 commit comments