pytorch / captum

Model interpretability and understanding for PyTorch
https://captum.ai
BSD 3-Clause "New" or "Revised" License
4.8k stars 485 forks source link

Captum insight does not work in Collab or AWS Sagemaker #1140

Open nahidalam opened 1 year ago

nahidalam commented 1 year ago

🐛 Bug

When I run captum insight python -m captum.insights.example I get below error

KeyError: 'WERKZEUG_SERVER_FD'

To Reproduce

Steps to reproduce the behavior:

Run ! python -m captum.insights.example in colab/sagemaker

at colab

/usr/local/lib/python3.10/dist-packages/captum/insights/example.py:6: UserWarning: Deprecated. Please import from captum.insights.attr_vis.example instead.
  warnings.warn(
 * Debugger is active!
 * Debugger PIN: 685-810-276
Exception in thread Thread-1 (run_app):
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)

Fetch data and view Captum Insights at http://localhost:54105/
  File "/usr/local/lib/python3.10/dist-packages/captum/insights/attr_vis/server.py", line 84, in run_app

    app.run(port=port, use_reloader=False, debug=debug)
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 1191, in run
    run_simple(t.cast(str, host), port, self, **options)
  File "/usr/local/lib/python3.10/dist-packages/werkzeug/serving.py", line 1073, in run_simple
    fd = int(os.environ["WERKZEUG_SERVER_FD"])
  File "/usr/lib/python3.10/os.py", line 680, in __getitem__
    raise KeyError(key) from None
KeyError: 'WERKZEUG_SERVER_FD'

At AWS SageMaker

/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/site-packages/captum/insights/example.py:6: UserWarning: Deprecated. Please import from captum.insights.attr_vis.example instead.
  warnings.warn(

Fetch data and view Captum Insights at http://localhost:52191/

 * Debugger is active!
 * Debugger PIN: 128-530-525
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/threading.py", line 980, in _bootstrap_inner
    self.run()
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/site-packages/captum/insights/attr_vis/server.py", line 84, in run_app
    app.run(port=port, use_reloader=False, debug=debug)
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/site-packages/flask/app.py", line 1188, in run
    run_simple(t.cast(str, host), port, self, **options)
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/site-packages/werkzeug/serving.py", line 1069, in run_simple
    fd = int(os.environ["WERKZEUG_SERVER_FD"])
  File "/home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'WERKZEUG_SERVER_FD'
znacer commented 1 year ago

I have the same issue using a remote notebook. issue #1127 suggest to comment the line : os.environ["WERKZEUG_RUN_MAIN"] = "true" # hides starting message in your /home/ec2-user/anaconda3/envs/pytorch_p39/lib/python3.9/site-packages/captum/insights/attr_vis/server.py

This modification worked well on my side.