Ptolemy-DL / Ptolemy

Other
120 stars 3 forks source link

KeyError when running generate_adversarial_examples.py #6

Open ZexueHe opened 3 years ago

ZexueHe commented 3 years ago

I'm trying to re-implement the Resnet-18 + Cifar-100 experiment. I followed the steps in readme. And in Generate per-class activation paths, I downloaded the provided resnet_18_cifar100_compact_BwCU.zip in Google Drive and unzipped it.

In the next step of Generated Adversarial Examples, I comment out the default VGG+Imagenet part in generate_adversarial_examples.py and uncomment back the Resnet-18+Cifar-100 part. Then I runpython -m nninst.backend.tensorflow.attack.generate_adversarial_examples.

However, I kept getting the same KeyError (perhaps) for each class of each image:

KeyError: "The name 'dense_2/BiasAdd:0' refers to a Tensor which does not exist. The operation, 'dense_2/BiasAdd', does not exist in the graph."

One example trace (the rest errors are similar just with a different class id and a different image id):

## raise exception from class 91, image 8:
Traceback (most recent call last):
  File "/data2/zexue/Ptolemy/src/nninst/backend/tensorflow/attack/common.py", line 1567, in generate_examples_fn
    example_io.save()
  File "/data2/zexue/Ptolemy/src/nninst/utils/fs.py", line 63, in save
    obj = self.init_fn()
  File "/data2/zexue/Ptolemy/src/nninst/backend/tensorflow/attack/common.py", line 1156, in get_example
    **kwargs,
  File "/data2/zexue/Ptolemy/src/nninst/backend/tensorflow/attack/foolbox_attack.py", line 80, in foolbox_generate_adversarial_example
    adversarial = attack(image, label=label, unpack=False)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/foolbox/attacks/base.py", line 97, in wrapper
    _ = call_fn(self, a, label=None, unpack=None, **kwargs)
  File "/data2/zexue/Ptolemy/src/nninst/backend/tensorflow/attack/foolbox_attacks/adaptive.py", line 171, in __call__
    layer_tensor = tf.get_default_graph().get_tensor_by_name(layer_name)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3654, in get_tensor_by_name
    return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3478, in as_graph_element
    return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3520, in _as_graph_element_locked
    "graph." % (repr(name), repr(op_name)))
KeyError: "The name 'dense_2/BiasAdd:0' refers to a Tensor which does not exist. The operation, 'dense_2/BiasAdd', does not exist in the graph."

This program kept running but only printed the above KeyError on the screen. I think I got errors for all images since there was no new file created under store/. Not sure why got this error cuz I did follow each step in the readme. Would be appreciated if you can share some info!!

uchuhimo commented 3 years ago

@ZexueHe The adaptive attack, as a prototype attack, is not as general as other attacks. You should adjust the layer_names in AdaptiveAttack for other models. You can comment out the configs for AdaptiveAttack to solve the problem.

    for generate_adversarial_fn, attack_name, attack_fn in [
        # [foolbox_generate_adversarial_example, "Adaptive_layer1", partial(AdaptiveAttack, layer_num=1)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer2", partial(AdaptiveAttack, layer_num=2)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer4", partial(AdaptiveAttack, layer_num=4)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer5", partial(AdaptiveAttack, layer_num=5)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer6", partial(AdaptiveAttack, layer_num=6)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer7", partial(AdaptiveAttack, layer_num=7)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer9", partial(AdaptiveAttack, layer_num=9)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer3", partial(AdaptiveAttack, layer_num=3)],
        # [foolbox_generate_adversarial_example, "Adaptive_layer8", partial(AdaptiveAttack, layer_num=8)],
        # [foolbox_generate_adversarial_example, "Adaptive_cos_layer9", partial(AdaptiveAttack, use_l2_loss=False, layer_num=9)],
       ...
ZexueHe commented 3 years ago

@uchuhimo Got another error during Generate activation paths for inputs in the dataset when running
python adversarial_example_path_generation.py --network=Resnet-18 --dataset=Cifar-100 --type=BwCU --theta==0.5 --alpha=None

Trace:

Using TensorFlow backend.
[0615 01:20:33 @fs.py:101] WRN Env var $TENSORPACK_DATASET not set, using /home/zexue/tensorpack_data for datasets.
[0615 01:20:33 @cifar.py:32] Found cifar10 data in /home/zexue/tensorpack_data/cifar10_data.
OMP: Info #155: KMP_AFFINITY: Initial OS proc set respected: 0-39
OMP: Info #216: KMP_AFFINITY: decoding x2APIC ids.
OMP: Info #157: KMP_AFFINITY: 40 available OS procs
OMP: Info #158: KMP_AFFINITY: Uniform topology
OMP: Info #287: KMP_AFFINITY: topology layer "LL cache" is equivalent to "socket".
OMP: Info #287: KMP_AFFINITY: topology layer "L3 cache" is equivalent to "socket".
OMP: Info #287: KMP_AFFINITY: topology layer "L2 cache" is equivalent to "core".
OMP: Info #287: KMP_AFFINITY: topology layer "L1 cache" is equivalent to "core".
OMP: Info #192: KMP_AFFINITY: 2 sockets x 10 cores/socket x 2 threads/core (20 total cores)
OMP: Info #218: KMP_AFFINITY: OS proc to physical thread map:
OMP: Info #172: KMP_AFFINITY: OS proc 0 maps to socket 0 core 0 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 20 maps to socket 0 core 0 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 1 maps to socket 0 core 1 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 21 maps to socket 0 core 1 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 2 maps to socket 0 core 2 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 22 maps to socket 0 core 2 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 3 maps to socket 0 core 3 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 23 maps to socket 0 core 3 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 4 maps to socket 0 core 4 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 24 maps to socket 0 core 4 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 5 maps to socket 0 core 8 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 25 maps to socket 0 core 8 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 6 maps to socket 0 core 9 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 26 maps to socket 0 core 9 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 7 maps to socket 0 core 10 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 27 maps to socket 0 core 10 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 8 maps to socket 0 core 11 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 28 maps to socket 0 core 11 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 9 maps to socket 0 core 12 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 29 maps to socket 0 core 12 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 10 maps to socket 1 core 0 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 30 maps to socket 1 core 0 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 11 maps to socket 1 core 1 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 31 maps to socket 1 core 1 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 12 maps to socket 1 core 2 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 32 maps to socket 1 core 2 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 13 maps to socket 1 core 3 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 33 maps to socket 1 core 3 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 14 maps to socket 1 core 4 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 34 maps to socket 1 core 4 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 15 maps to socket 1 core 8 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 35 maps to socket 1 core 8 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 16 maps to socket 1 core 9 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 36 maps to socket 1 core 9 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 17 maps to socket 1 core 10 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 37 maps to socket 1 core 10 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 18 maps to socket 1 core 11 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 38 maps to socket 1 core 11 thread 1 
OMP: Info #172: KMP_AFFINITY: OS proc 19 maps to socket 1 core 12 thread 0 
OMP: Info #172: KMP_AFFINITY: OS proc 39 maps to socket 1 core 12 thread 1 
OMP: Info #254: KMP_AFFINITY: pid 467 tid 467 thread 0 bound to OS proc set 0
Traceback (most recent call last):
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/data2/zexue/Ptolemy/src/nninst/backend/tensorflow/attack/foolbox_attack_resnet_18.py", line 93, in <module>
    ray_init()
  File "/data2/zexue/Ptolemy/src/nninst/utils/ray.py", line 50, in ray_init
    ray.init("dell-01:6378", **kwargs)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/ray/worker.py", line 1356, in init
    redis_address = services.address_to_ip(redis_address)
  File "/data2/zexue/anaconda3/envs/nninst/lib/python3.6/site-packages/ray/services.py", line 189, in address_to_ip
    ip_address = socket.gethostbyname(address_parts[0])
socket.gaierror: [Errno -2] Name or service not known

I tried all keys in ray.py

_tag_to_ray = {
    "r730": 6370,
    "dell": 6371,
    "gpu": 6372,
    "gpu_only": 6373,
    # "tmp": 6379,
}

but all failed with the same error. Would you mind sharing some info for this?

uchuhimo commented 3 years ago

@ZexueHe It use mode.distributed(), which needs to run on a Ray cluster. We should use mode.local() instead. Please pull master to fix it.