TeamHG-Memex / eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
http://eli5.readthedocs.io
MIT License
2.75k stars 331 forks source link

[WIP] Explain PyTorch neural nets with Grad-CAM #327

Open teabolt opened 5 years ago

teabolt commented 5 years ago

This PR explains image and text classifiers built in PyTorch using the Grad-CAM method, building on #315 and #325.

Images example: Using the pretrained mobilenet_v2 network from torchvision and calling eli5.show_prediction(model, doc, image=img)

We get the classical explanation for 'dog': Screenshot from 2019-07-29 22-12-23

Text example: Using an example model from https://www.kaggle.com/ziliwang/pytorch-text-cnn for an insincere question classification task (https://www.kaggle.com/c/quora-insincere-questions-classification/overview), we can write eli5.show_prediction(model, doc, tokens=tokens, layer=layer, relu=False).

To get an explanation like this (green = 'insincere', red = 'neutral'): Screenshot from 2019-07-29 21-52-42

This PR only provides basic PyTorch support.

TODO items:

codecov-io commented 5 years ago

Codecov Report

Merging #327 into master will decrease coverage by 3.19%. The diff coverage is 71.95%.

@@            Coverage Diff            @@
##           master     #327     +/-   ##
=========================================
- Coverage   97.31%   94.12%   -3.2%     
=========================================
  Files          49       56      +7     
  Lines        3134     3472    +338     
  Branches      584      645     +61     
=========================================
+ Hits         3050     3268    +218     
- Misses         44      162    +118     
- Partials       40       42      +2
Impacted Files Coverage Δ
eli5/keras/__init__.py 100% <ø> (ø) :arrow_up:
eli5/keras/explain_prediction.py 98.7% <100%> (+2.31%) :arrow_up:
eli5/pytorch/__init__.py 100% <100%> (ø)
eli5/base.py 100% <100%> (ø) :arrow_up:
eli5/nn/__init__.py 100% <100%> (ø)
eli5/__init__.py 84.61% <100%> (+1.28%) :arrow_up:
eli5/keras/gradcam.py 100% <100%> (ø) :arrow_up:
eli5/nn/text.py 100% <100%> (ø)
eli5/pytorch/gradcam.py 24.13% <24.13%> (ø)
eli5/pytorch/explain_prediction.py 29.16% <29.16%> (ø)
... and 13 more
codecov-commenter commented 4 years ago

Codecov Report

Merging #327 into master will decrease coverage by 3.20%. The diff coverage is 71.95%.

@@            Coverage Diff             @@
##           master     #327      +/-   ##
==========================================
- Coverage   97.32%   94.12%   -3.21%     
==========================================
  Files          49       56       +7     
  Lines        3142     3472     +330     
  Branches      585      645      +60     
==========================================
+ Hits         3058     3268     +210     
- Misses         44      162     +118     
- Partials       40       42       +2     
Impacted Files Coverage Δ
eli5/keras/__init__.py 100.00% <ø> (ø)
eli5/pytorch/gradcam.py 24.13% <24.13%> (ø)
eli5/pytorch/explain_prediction.py 29.16% <29.16%> (ø)
eli5/pytorch/image.py 30.00% <30.00%> (ø)
eli5/formatters/image.py 98.75% <92.85%> (-1.25%) :arrow_down:
eli5/nn/gradcam.py 95.83% <95.83%> (ø)
eli5/__init__.py 84.61% <100.00%> (+1.28%) :arrow_up:
eli5/base.py 100.00% <100.00%> (ø)
eli5/keras/explain_prediction.py 98.70% <100.00%> (+2.31%) :arrow_up:
eli5/keras/gradcam.py 100.00% <100.00%> (ø)
... and 18 more