Azure/counterfit

Error opening terminal.py

pstarconsult opened this issue · 8 comments

Describe the bug
While in Counterfit conda env, I attempted to invoke 'python examples/terminal/terminal.py' but get error that Torch has no module 'nn'. I also get this error when running the code in Python (where I import counterfit and run the sample code). See Additional context below.

POSSIBLE FIX: I see the below statement in another app I am using. The error in this issue may be due to not creating the import alias (?):

import torch.nn as nn

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://github.com/Azure/counterfit/blob/main/examples/DEMO2-creditfraud.md
  2. Run 'python examples/terminal/terminal.py'
  3. The stack trace listed below occurs.
  4. Or, run the sample code where imports are performed in PY file and attacks are called in PY file (see Additional context).

Expected behavior
The terminal should allow use of the creditfraud shell as below
counterfit> set_target creditfraud
creditfraud>

Stack trace
(counterfit) paul@paul-ThinkPad-T480s:~/Desktop/Counterfit-AML/counterfit$ python examples/terminal/terminal.py
Traceback (most recent call last):
File "examples/terminal/terminal.py", line 9, in
from examples.terminal.core.terminal import Terminal
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/examples/terminal/core/terminal.py", line 6, in
from counterfit import CFPrint
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/counterfit/init.py", line 9, in
from . import core, data, frameworks, reporting, targets
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/counterfit/frameworks/init.py", line 5, in
from .art import art
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/counterfit/frameworks/art/art.py", line 17, in
from art.utils import compute_success_array, random_targets
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/init.py", line 7, in
from art import attacks
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/attacks/init.py", line 8, in
from art.attacks import evasion
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/attacks/evasion/init.py", line 4, in
from art.attacks.evasion.adversarial_patch.adversarial_patch import AdversarialPatch
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/attacks/evasion/adversarial_patch/adversarial_patch.py", line 31, in
from art.attacks.evasion.adversarial_patch.adversarial_patch_numpy import AdversarialPatchNumpy
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/attacks/evasion/adversarial_patch/adversarial_patch_numpy.py", line 37, in
from art.estimators.estimator import BaseEstimator, NeuralNetworkMixin
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/estimators/init.py", line 17, in
from art.estimators import certification
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/estimators/certification/init.py", line 9, in
from art.estimators.certification import deep_z
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/estimators/certification/deep_z/init.py", line 4, in
from art.estimators.certification.deep_z.deep_z import ZonoDenseLayer
File "/home/paul/anaconda3/envs/counterfit/lib/python3.8/site-packages/art/estimators/certification/deep_z/deep_z.py", line 29, in
class ZonoDenseLayer(torch.nn.Module):
AttributeError: module 'torch' has no attribute 'nn'

Screenshots
N/A

Desktop (please complete the following information):

  • OS: Linux Ubuntu
  • Version: 22.04.1 LTS

Additional context
I followed install instructions from current site (today). I was using the older version where counterfit.py was used (if that helps) I installed into a completely new dir, conda env, etc.

I also get this error when running the below code (from https://github.com/Azure/counterfit):

import counterfit
import counterfit.targets as targets

target = targets.CreditFraud()
target.load()
attack_name = 'hop_skip_jump'
new_attack = counterfit.Counterfit.build_attack(target, attack_name)
results = counterfit.Counterfit.run_attack(new_attack)

Hi @pstarconsult,

Interesting, the line of code triggering this issue is in ART while it imports Torch in deep_z.py#L29.

Do you think you can provide with the output of pip list -v from your system? I think the issue might be with the version of torch running. With that, I'll try to reproduce the issue and create a PR to fix the problem. 👍

Yes, I think you are correct. I filed a bug (and fixed it) with ART the other day. That issue was related to Torch either changing or removing functions and libs (this occurred when going from Torch 1.8 to 1.9 apparently). Here is the pip list. I see Torch 13 is in use (same as ART). Hmm, lol:

Package Version


adversarial-robustness-toolbox 1.12.1
asttokens 2.1.0
backcall 0.2.0
certifi 2022.9.24
charset-normalizer 2.1.1
debugpy 1.6.3
decorator 5.1.1
entrypoints 0.4
executing 1.2.0
idna 3.4
ipykernel 6.17.1
ipython 8.6.0
jedi 0.18.1
joblib 1.2.0
jupyter_client 7.4.5
jupyter_core 5.0.0
llvmlite 0.39.1
matplotlib-inline 0.1.6
nest-asyncio 1.5.6
numba 0.56.4
numpy 1.23.4
nvidia-cublas-cu11 11.10.3.66
nvidia-cuda-nvrtc-cu11 11.7.99
nvidia-cuda-runtime-cu11 11.7.99
nvidia-cudnn-cu11 8.5.0.96
packaging 21.3
pandas 1.5.1
parso 0.8.3
pexpect 4.8.0
pickleshare 0.7.5
Pillow 9.3.0
pip 22.0.2
platformdirs 2.5.3
prompt-toolkit 3.0.32
psutil 5.9.4
ptyprocess 0.7.0
pure-eval 0.2.2
Pygments 2.13.0
pyparsing 3.0.9
python-dateutil 2.8.2
pytz 2022.6
pyzmq 24.0.1
requests 2.28.1
scikit-learn 1.0.2
scipy 1.9.3
setuptools 59.6.0
six 1.16.0
stack-data 0.6.0
threadpoolctl 3.1.0
torch 1.13.0
torchvision 0.14.0
tornado 6.2
tqdm 4.64.1
traitlets 5.5.0
typing_extensions 4.4.0
urllib3 1.26.12
wcwidth 0.2.5
wheel 0.38.4

@dlmgary Sorry, I see that you asked for pip list -v (I just ran pip list). Do you prefer the former?

Hey @pstarconsult. I'm looking at the imports you have there and I see that Counterfit is missing as an installed package.

Can you try these installation instructions and see if the problem persists? https://github.com/Azure/counterfit#option-2-set-up-an-anaconda-python-environment-and-install-locally. I think you might be missing the pip install .[dev] step.

After that, you should see Counterfit as an installed package when doing pip list.

(counterfit) ----------------------------------------------------------------------------
~/src/counterfit (main*) »    pip list
Package                        Version
------------------------------ -----------
absl-py                        1.3.0
adversarial-robustness-toolbox 1.12.1
aiohttp                        3.8.3
aiosignal                      1.3.1
alabaster                      0.7.12
alembic                        1.8.1
astunparse                     1.6.3
async-timeout                  4.0.2
attrs                          22.1.0
augly                          0.1.10
autopage                       0.5.1
azure-core                     1.26.1
azure-storage-blob             12.1.0
Babel                          2.11.0
beautifulsoup4                 4.11.1
bert-score                     0.3.12
bleach                         5.0.1
bpemb                          0.3.4
bs4                            0.0.1
cachetools                     5.2.0
certifi                        2022.9.24
cffi                           1.15.1
cfgv                           3.3.1
chardet                        3.0.4
charset-normalizer             2.1.1
click                          8.1.3
cliff                          4.0.0
cloudpickle                    2.2.0
cmaes                          0.9.0
cmd2                           2.2.0
cmd2-ext-test                  2.0.0
colorama                       0.4.6
colorlog                       6.7.0
commonmark                     0.9.1
conllu                         4.5.2
contourpy                      1.0.6
counterfit                     1.1.0                # <----- This should show       
cryptography                   38.0.3
cycler                         0.11.0
datasets                       1.2.1
decorator                      5.1.1
defusedxml                     0.7.1
Deprecated                     1.2.13
dill                           0.3.6
distlib                        0.3.6
docopt                         0.6.2
docutils                       0.17.1
editdistance                   0.6.1
entrypoints                    0.4
fastjsonschema                 2.16.2
filelock                       3.8.0
flair                          0.10
Flask                          2.0.0
flatbuffers                    22.10.26
fonttools                      4.38.0
frozenlist                     1.3.3
fsspec                         2022.11.0
ftfy                           6.1.1
future                         0.18.2
gast                           0.4.0
gdown                          3.12.2
gensim                         4.2.0
google-auth                    2.14.1
google-auth-oauthlib           0.4.6
google-pasta                   0.2.0
greenlet                       2.0.1
grpcio                         1.50.0
gym                            0.26.2
gym-notices                    0.0.8
h5py                           3.1.0
huggingface-hub                0.10.1
hyperopt                       0.2.3
identify                       2.5.8
idna                           2.10
imagesize                      1.4.1
importlib-metadata             5.0.0
importlib-resources            5.10.0
iniconfig                      1.1.1
iopath                         0.1.10
isodate                        0.6.1
itsdangerous                   2.1.2
Janome                         0.4.2
Jinja2                         3.1.2
joblib                         1.2.0
jsonschema                     4.17.0
jupyter_client                 7.4.5
jupyter_core                   5.0.0
jupyterlab-pygments            0.2.2
keras                          2.10.0
Keras-Preprocessing            1.1.2
kiwisolver                     1.4.4
konoha                         4.6.3
langdetect                     1.0.9
language-tool-python           2.7.1
lemminflect                    0.2.3
libclang                       14.0.6
lightgbm                       3.3.1
livereload                     2.6.3
llvmlite                       0.39.1
lru-dict                       1.1.8
lxml                           4.9.1
Mako                           1.2.3
Markdown                       3.4.1
MarkupSafe                     2.1.1
matplotlib                     3.6.1
mistune                        2.0.4
more-itertools                 8.8.0
mpld3                          0.3
msrest                         0.7.1
multidict                      6.0.2
multiprocess                   0.70.14
nbclient                       0.7.0
nbconvert                      7.2.4
nbformat                       5.7.0
nbsphinx                       0.8.9
nest-asyncio                   1.5.6
networkx                       2.2
nlpaug                         1.1.3
nltk                           3.7
nodeenv                        1.7.0
num2words                      0.5.12
numba                          0.56.4
numpy                          1.23.4
nvidia-cublas-cu11             11.10.3.66
nvidia-cuda-nvrtc-cu11         11.7.99
nvidia-cuda-runtime-cu11       11.7.99
nvidia-cudnn-cu11              8.5.0.96
oauthlib                       3.2.2
onnxruntime                    1.10.0
opt-einsum                     3.3.0
optuna                         2.10.1
orjson                         3.6.4
overrides                      3.1.0
packaging                      21.3
pandas                         1.5.1
pandocfilters                  1.5.0
pbr                            5.11.0
Pillow                         9.2.0
pip                            22.2.2
pkgutil_resolve_name           1.3.10
platformdirs                   2.5.3
pluggy                         1.0.0
portalocker                    2.6.0
pre-commit                     2.20.0
prettytable                    3.5.0
prompt-toolkit                 3.0.32
protobuf                       3.19.6
py                             1.11.0
pyarrow                        10.0.0
pyasn1                         0.4.8
pyasn1-modules                 0.2.8
pycparser                      2.21
pyDeprecate                    0.3.2
pygame                         2.1.2
pyglet                         2.0.0
Pygments                       2.13.0
pyparsing                      3.0.9
pyperclip                      1.8.2
pyrsistent                     0.19.2
PySocks                        1.7.1
pytest                         6.2.5
python-dateutil                2.8.2
python-Levenshtein             0.12.2
python-magic                   0.4.27
pytz                           2022.6
PyYAML                         6.0
pyzmq                          24.0.1
questionary                    1.9.0
recommonmark                   0.7.1
regex                          2022.10.31
requests                       2.24.0
requests-oauthlib              1.3.1
rich                           12.0.1
rsa                            4.9
scikit-learn                   1.0.2
scipy                          1.9.2
segtok                         1.5.11
sentencepiece                  0.1.95
setuptools                     65.5.0
six                            1.16.0
smart-open                     6.2.0
snowballstemmer                2.2.0
soupsieve                      2.3.2.post1
Sphinx                         5.3.0
sphinx-autobuild               2021.3.14
sphinx-copybutton              0.5.0
sphinx-markdown-tables         0.0.17
sphinx-rtd-theme               1.1.1
sphinxcontrib-applehelp        1.0.2
sphinxcontrib-devhelp          1.0.2
sphinxcontrib-htmlhelp         2.0.0
sphinxcontrib-jsmath           1.0.1
sphinxcontrib-qthelp           1.0.3
sphinxcontrib-serializinghtml  1.1.5
SQLAlchemy                     1.4.43
sqlitedict                     2.0.0
stevedore                      4.1.1
tabulate                       0.9.0
tensorboard                    2.10.1
tensorboard-data-server        0.6.1
tensorboard-plugin-wit         1.8.1
tensorboardX                   2.5.1
tensorflow                     2.10.0
tensorflow-estimator           2.10.0
tensorflow-hub                 0.12.0
tensorflow-io-gcs-filesystem   0.27.0
tensorflow-text                2.10.0
termcolor                      2.1.0
terminaltables                 3.1.10
textattack                     0.3.3
threadpoolctl                  3.1.0
tinycss2                       1.2.1
tokenizers                     0.13.2
toml                           0.10.2
torch                          1.13.0
torchfile                      0.1.0
torchmetrics                   0.10.2
torchvision                    0.14.0
tornado                        6.2
tqdm                           4.49.0
traitlets                      5.3.0
transformers                   4.24.0
typing_extensions              4.4.0
urllib3                        1.25.11
virtualenv                     20.16.6
wcwidth                        0.2.5
webencodings                   0.5.1
Werkzeug                       2.2.2
wheel                          0.37.1
Wikipedia-API                  0.5.4
word2number                    1.1
wrapt                          1.14.1
xxhash                         3.1.0
yarl                           1.8.1
zipp                           3.10.0

Let me know if this helps!

Oops, I accidentally ran my pip list from the ART dir. Here is my pip list from CF (sorry). I see CF in the list. It was actually always there since I did a local install initially. Let me know if this helps things on your end.

Package Version


absl-py 1.3.0
adversarial-robustness-toolbox 1.12.1
aiohttp 3.8.3
aiosignal 1.3.1
alabaster 0.7.12
alembic 1.8.1
astunparse 1.6.3
async-timeout 4.0.2
attrs 22.1.0
augly 0.1.10
autopage 0.5.1
azure-core 1.26.1
azure-storage-blob 12.1.0
Babel 2.11.0
beautifulsoup4 4.11.1
bert-score 0.3.12
bleach 5.0.1
bpemb 0.3.4
bs4 0.0.1
cachetools 5.2.0
certifi 2022.9.24
cffi 1.15.1
cfgv 3.3.1
chardet 3.0.4
charset-normalizer 2.1.1
click 8.1.3
cliff 4.0.0
cloudpickle 2.2.0
cmaes 0.9.0
cmd2 2.2.0
cmd2-ext-test 2.0.0
colorama 0.4.6
colorlog 6.7.0
commonmark 0.9.1
conllu 4.5.2
contourpy 1.0.6
counterfit 1.1.0
cryptography 38.0.3
cycler 0.11.0
datasets 1.2.1
decorator 5.1.1
defusedxml 0.7.1
Deprecated 1.2.13
dill 0.3.6
distlib 0.3.6
docopt 0.6.2
docutils 0.17.1
editdistance 0.6.1
entrypoints 0.4
fastjsonschema 2.16.2
filelock 3.8.0
flair 0.10
Flask 2.0.0
flatbuffers 22.10.26
fonttools 4.38.0
frozenlist 1.3.3
fsspec 2022.11.0
ftfy 6.1.1
future 0.18.2
gast 0.4.0
gdown 3.12.2
gensim 4.2.0
google-auth 2.14.1
google-auth-oauthlib 0.4.6
google-pasta 0.2.0
greenlet 2.0.1
grpcio 1.50.0
gym 0.26.2
gym-notices 0.0.8
h5py 3.1.0
huggingface-hub 0.10.1
hyperopt 0.2.3
identify 2.5.8
idna 2.10
imagesize 1.4.1
importlib-metadata 5.0.0
importlib-resources 5.10.0
iniconfig 1.1.1
iopath 0.1.10
isodate 0.6.1
itsdangerous 2.1.2
Janome 0.4.2
Jinja2 3.1.2
joblib 1.2.0
jsonschema 4.17.0
jupyter_client 7.4.5
jupyter_core 5.0.0
jupyterlab-pygments 0.2.2
keras 2.10.0
Keras-Preprocessing 1.1.2
kiwisolver 1.4.4
konoha 4.6.3
langdetect 1.0.9
language-tool-python 2.7.1
lemminflect 0.2.3
libclang 14.0.6
lightgbm 3.3.1
livereload 2.6.3
llvmlite 0.39.1
lru-dict 1.1.8
lxml 4.9.1
Mako 1.2.3
Markdown 3.4.1
MarkupSafe 2.1.1
matplotlib 3.6.1
mistune 2.0.4
more-itertools 8.8.0
mpld3 0.3
msrest 0.7.1
multidict 6.0.2
multiprocess 0.70.14
nbclient 0.7.0
nbconvert 7.2.4
nbformat 5.7.0
nbsphinx 0.8.9
nest-asyncio 1.5.6
networkx 2.2
nlpaug 1.1.3
nltk 3.7
nodeenv 1.7.0
num2words 0.5.12
numba 0.56.4
numpy 1.23.4
oauthlib 3.2.2
onnxruntime 1.10.0
opt-einsum 3.3.0
optuna 2.10.1
orjson 3.6.4
overrides 3.1.0
packaging 21.3
pandas 1.5.1
pandocfilters 1.5.0
pbr 5.11.0
Pillow 9.2.0
pip 22.2.2
pkgutil_resolve_name 1.3.10
platformdirs 2.5.3
pluggy 1.0.0
portalocker 2.6.0
pre-commit 2.20.0
prettytable 3.5.0
prompt-toolkit 3.0.32
protobuf 3.19.6
py 1.11.0
pyarrow 10.0.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycparser 2.21
pyDeprecate 0.3.2
pygame 2.1.2
pyglet 2.0.0
Pygments 2.13.0
pyparsing 3.0.9
pyperclip 1.8.2
pyrsistent 0.19.2
PySocks 1.7.1
pytest 6.2.5
python-dateutil 2.8.2
python-Levenshtein 0.12.2
python-magic 0.4.27
pytorch-lightning 1.6.0
pytz 2022.6
PyYAML 6.0
pyzmq 24.0.1
questionary 1.9.0
recommonmark 0.7.1
regex 2022.10.31
requests 2.24.0
requests-oauthlib 1.3.1
rich 12.0.1
rsa 4.9
scikit-learn 1.0.2
scipy 1.9.2
segtok 1.5.11
sentencepiece 0.1.95
setuptools 65.5.0
six 1.16.0
smart-open 6.2.0
snowballstemmer 2.2.0
soupsieve 2.3.2.post1
Sphinx 5.3.0
sphinx-autobuild 2021.3.14
sphinx-copybutton 0.5.0
sphinx-markdown-tables 0.0.17
sphinx-rtd-theme 1.1.1
sphinxcontrib-applehelp 1.0.2
sphinxcontrib-devhelp 1.0.2
sphinxcontrib-htmlhelp 2.0.0
sphinxcontrib-jsmath 1.0.1
sphinxcontrib-qthelp 1.0.3
sphinxcontrib-serializinghtml 1.1.5
SQLAlchemy 1.4.43
sqlitedict 2.0.0
stevedore 4.1.1
tabulate 0.9.0
tensorboard 2.10.1
tensorboard-data-server 0.6.1
tensorboard-plugin-wit 1.8.1
tensorboardX 2.5.1
tensorflow 2.10.0
tensorflow-estimator 2.10.0
tensorflow-hub 0.12.0
tensorflow-io-gcs-filesystem 0.27.0
tensorflow-text 2.10.0
termcolor 2.1.0
terminaltables 3.1.10
textattack 0.3.3
threadpoolctl 3.1.0
tinycss2 1.2.1
tokenizers 0.13.2
toml 0.10.2
torch 1.10.0
torchfile 0.1.0
torchmetrics 0.10.2
torchvision 0.11.1
tornado 6.2
tqdm 4.49.0
traitlets 5.3.0
transformers 4.24.0
typing_extensions 4.4.0
urllib3 1.25.11
virtualenv 20.16.6
wcwidth 0.2.5
webencodings 0.5.1
Werkzeug 2.2.2
wheel 0.37.1
Wikipedia-API 0.5.4
word2number 1.1
wrapt 1.14.1
xxhash 3.1.0
yarl 1.8.1
zipp 3.10.0

Hey @pstarconsult, I haven't been able to reproduce the issue. I installed all the dependencies from pip that you shared and Counterfit behaved as expected.

The version of Torch you have installed is 1.10.0. I checked the source code for Torch in that version and I see that Module is define in torch.nn, so torch.nn.Module should work (PyToch 1.10.0 - PyTorch/torch/nn/init.py). I also found Module ‘torch’ has no attribute ‘Module’ question in the PyTorch discussion.

Could you try installing Counterfit again in a fresh Conda environment and confirm if the issue persists? https://github.com/Azure/counterfit#installation-with-conda

$ conda update -c conda-forge --all -y
$ conda create --yes -n counterfit python=3.8.0
$ conda activate counterfit
$ pip install .[dev]
$ python -c "import nltk;  nltk.download('stopwords')"

Then, you can import Counterfit and run the command line interface with:

$ conda activate counterfit
$ python -c "import counterfit; print(counterfit.__version__)"

or

$ conda activate counterfit
$ counterfit

Hoping this helps a bit! 👍

I followed your instructions and it works. Not sure what happened here but likely something with installing the new version of CF somewhere. So, we can close this bug. Thank you for running it on your end!

Fantastic! Then marking this issue as resolved. 👍