Trusted-AI/adversarial-robustness-toolbox

Error in **notebooks /expectation_over_transformation_classification_rotation.ipynb****

uni2237 opened this issue · 4 comments

Describe the bug

1)
There is an ModuleNotFoundError in cell 1

In [1]:
from art.classifiers import TensorFlowV2Classifier

Out [1]:

ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 13
     10 import imagenet_stubs
     11 from imagenet_stubs.imagenet_2012_labels import label_to_name, name_to_label
---> 13 from art.classifiers import TensorFlowV2Classifier
     14 from art.preprocessing.expectation_over_transformation import EoTImageRotationTensorFlow
     15 from art.attacks.evasion import ProjectedGradientDescent

ModuleNotFoundError: No module named 'art.classifiers' 

I suggest changing from art.classifiers import TensorFlowV2Classifier to from art.estimators.classification.tensorflow import TensorFlowV2Classifier


2)
There is an InvalidArgumentError in cell 19

In [19]:

# Generate adversarial examples
x_adv_eot = attack_eot.generate(x=x, y=y_target)

Out [19]:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[19], line 2
      1 # Generate adversarial examples
----> 2 x_adv_eot = attack_eot.generate(x=x, y=y_target)

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/attacks/evasion/projected_gradient_descent/projected_gradient_descent.py:200, in ProjectedGradientDescent.generate(self, x, y, **kwargs)
    185 """
    186 Generate adversarial samples and return them in an array.
    187 
   (...)
    197 :return: An array holding the adversarial examples.
    198 """
    199 logger.info("Creating adversarial samples.")
--> 200 return self._attack.generate(x=x, y=y, **kwargs)

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_tensorflow_v2.py:219, in ProjectedGradientDescentTensorFlowV2.generate(self, x, y, **kwargs)
    216 for rand_init_num in range(max(1, self.num_random_init)):
    217     if rand_init_num == 0:
    218         # first iteration: use the adversarial examples as they are the only ones we have now
--> 219         adv_x[batch_index_1:batch_index_2] = self._generate_batch(
    220             x=batch, targets=batch_labels, mask=mask_batch, eps=batch_eps, eps_step=batch_eps_step
    221         )
    222     else:
    223         adversarial_batch = self._generate_batch(
    224             x=batch, targets=batch_labels, mask=mask_batch, eps=batch_eps, eps_step=batch_eps_step
    225         )

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_tensorflow_v2.py:274, in ProjectedGradientDescentTensorFlowV2._generate_batch(self, x, targets, mask, eps, eps_step)
    272 for i_max_iter in range(self.max_iter):
    273     self._i_max_iter = i_max_iter
--> 274     adv_x = self._compute_tf(
    275         adv_x,
    276         x,
    277         targets,
    278         mask,
    279         eps,
    280         eps_step,
    281         momentum,
    282         self.num_random_init > 0 and i_max_iter == 0,
    283     )
    285 return adv_x

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_tensorflow_v2.py:441, in ProjectedGradientDescentTensorFlowV2._compute_tf(self, x, x_init, y, mask, eps, eps_step, momentum, random_init)
    438     x_adv = x
    440 # Get perturbation
--> 441 perturbation = self._compute_perturbation(x_adv, y, mask, self.decay, momentum)
    443 # Apply perturbation and clip
    444 x_adv = self._apply_perturbation(x_adv, perturbation, eps_step)

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/attacks/evasion/projected_gradient_descent/projected_gradient_descent_tensorflow_v2.py:316, in ProjectedGradientDescentTensorFlowV2._compute_perturbation(self, x, y, mask, decay, momentum)
    313 tol = 10e-8
    315 # Get gradient wrt loss; invert it if attack is targeted
--> 316 grad: tf.Tensor = self.estimator.loss_gradient(x, y) * tf.constant(
    317     1 - 2 * int(self.targeted), dtype=ART_NUMPY_DTYPE
    318 )
    320 # Write summary
    321 if self.summary_writer is not None:  # pragma: no cover

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/estimators/classification/tensorflow.py:1249, in TensorFlowV2Classifier.loss_gradient(self, x, y, training_mode, **kwargs)
   1247     x_grad = tf.convert_to_tensor(x)
   1248     tape.watch(x_grad)
-> 1249     x_input, y_input = self._apply_preprocessing(x_grad, y=y, fit=False)
   1250 else:
   1251     x_preprocessed, y_preprocessed = self._apply_preprocessing(x, y=y, fit=False)

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/estimators/tensorflow.py:195, in TensorFlowV2Estimator._apply_preprocessing(self, x, y, fit)
    193     else:
    194         if preprocess.apply_predict:
--> 195             x, y = preprocess.forward(x, y)
    197 # Convert torch tensors back to np arrays.
    198 if not input_is_tensor:

File ~/anaconda3/envs/art/lib/python3.8/site-packages/art/preprocessing/expectation_over_transformation/tensorflow.py:99, in EoTTensorFlowV2.forward(self, x, y)
     97     y_i = None
     98 x_preprocess, y_preprocess_i = self._transform(x_i, y_i)
---> 99 x_preprocess_list.append(tf.squeeze(x_preprocess, axis=0))
    101 if y is not None and y_preprocess_i is not None:
    102     y_preprocess_list.append(y_preprocess_i)

File ~/anaconda3/envs/art/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~/anaconda3/envs/art/lib/python3.8/site-packages/tensorflow/python/framework/ops.py:7209, in raise_from_not_ok_status(e, name)
   7207 def raise_from_not_ok_status(e, name):
   7208   e.message += (" name: " + name if name is not None else "")
-> 7209   raise core._status_to_exception(e) from None

InvalidArgumentError: {{function_node __wrapped__Squeeze_device_/job:localhost/replica:0/task:0/device:GPU:0}} Can not squeeze dim[0], expected a dimension of 1, got 299 [Op:Squeeze]

System information (please complete the following information):

  • OS : window
  • Python version : Python 3.8.17
  • ART version or commit number : adversarial-robustness-toolbox 1.15.0
  • TensorFlow / Keras / PyTorch / MXNet version : tensorflow 2.10.1 / keras 2.10.0 / torch 1.13.1+cpu

Hi @uni2237 Thank you very much for reporting this issue!

@Foxglove144 Thank you for working on a solution!

@Foxglove144 Could you please post a message to this issue, after that I can assign the issue to you.

Sure thing. I was already working on a fix for this notebook when the issue was posted :)