Volver al ranking

hila-chefer/Transformer-Explainability

Jupyter Notebook

[CVPR 2021] Official PyTorch implementation for Transformer Interpretability Beyond Attention Visualization, a novel method to visualize classifications by Transformer based networks.

deep-learningvision-transformerbert-modelbertexplainabilitytransformer-interpretabilityperturbationattention-visualizationvisualize-classificationsvitattention-matrixcvpr2021
Crecimiento de estrellas
Estrellas
2k
Forks
260
Crecimiento semanal
Issues
12
5001k1.5k2k
dic 2020oct 2022sept 2024jul 2026
README

PyTorch Implementation of Transformer Interpretability Beyond Attention Visualization [CVPR 2021]

Check out our new advancements- Generic Attention-model Explainability for Interpreting Bi-Modal and Encoder-Decoder Transformers!

Faster, more general, and can be applied to any type of attention! Among the features:

  • We remove LRP for a simple and quick solution, and prove that the great results from our first paper still hold!
  • We expand our work to any type of Transformer- not just self-attention based encoders, but also co-attention encoders and encoder-decoders!
  • We show that VQA models can actually understand both image and text and make connections!
  • We use a DETR object detector and create segmentation masks from our explanations!
  • We provide a colab notebook with all the examples. You can very easily add images and questions of your own!


ViT explainability notebook:

Open In Colab

BERT explainability notebook:

Open In Colab

Updates

April 5 2021: Check out this new post about our paper! A great resource for understanding the main concepts behind our work.

March 15 2021: A Colab notebook for BERT for sentiment analysis added!

Feb 28 2021: Our paper was accepted to CVPR 2021!

Feb 17 2021: A Colab notebook with all examples added!

Jan 5 2021: A Jupyter notebook for DeiT added!

Introduction

Official implementation of Transformer Interpretability Beyond Attention Visualization.

We introduce a novel method which allows to visualize classifications made by a Transformer based model for both vision and NLP tasks. Our method also allows to visualize explanations per class.

Method consists of 3 phases:
  1. Calculating relevance for each attention matrix using our novel formulation of LRP.

  2. Backpropagation of gradients for each attention matrix w.r.t. the visualized class. Gradients are used to average attention heads.

  3. Layer aggregation with rollout.

Please notice our Jupyter notebook where you can run the two class specific examples from the paper.

alt text

To add another input image, simply add the image to the samples folder, and use the generate_visualization function for your selected class of interest (using the class_index={class_idx}), not specifying the index will visualize the top class.

Credits

ViT implementation is based on:

BERT implementation is taken from the huggingface Transformers library: https://huggingface.co/transformers/

ERASER benchmark code adapted from the ERASER GitHub implementation: https://github.com/jayded/eraserbenchmark

Text visualizations in supplementary were created using TAHV heatmap generator for text: https://github.com/jiesutd/Text-Attention-Heatmap-Visualization

Reproducing results on ViT

Section A. Segmentation Results

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/imagenet_seg_eval.py --method transformer_attribution --imagenet-seg-path /path/to/gtsegs_ijcv.mat

Link to download dataset.

In the exmaple above we run a segmentation test with our method. Notice you can choose which method you wish to run using the --method argument. You must provide a path to imagenet segmentation data in --imagenet-seg-path.

Section B. Perturbation Results

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/generate_visualizations.py --method transformer_attribution --imagenet-validation-path /path/to/imagenet_validation_directory

Notice that you can choose to visualize by target or top class by using the --vis-cls argument.

Now to run the perturbation test run the following command:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 baselines/ViT/pertubation_eval_from_hdf5.py --method transformer_attribution

Notice that you can use the --neg argument to run either positive or negative perturbation.

Reproducing results on BERT

  1. Download the pretrained weights:
  1. Download the dataset pkl file:
  1. Download the dataset:
  1. Now you can run the model.

Example:

CUDA_VISIBLE_DEVICES=0 PYTHONPATH=./:$PYTHONPATH python3 BERT_rationale_benchmark/models/pipeline/bert_pipeline.py --data_dir data/movies/ --output_dir bert_models/movies/ --model_params BERT_params/movies_bert.json

To control which algorithm to use for explanations change the method variable in BERT_rationale_benchmark/models/pipeline/bert_pipeline.py (Defaults to 'transformer_attribution' which is our method). Running this command will create a directory for the method in bert_models/movies/<method_name>.

In order to run f1 test with k, run the following command:

PYTHONPATH=./:$PYTHONPATH python3 BERT_rationale_benchmark/metrics.py --data_dir data/movies/ --split test --results bert_models/movies/<method_name>/identifier_results_k.json

Also, in the method directory there will be created .tex files containing the explanations extracted for each example. This corresponds to our visualizations in the supplementary.

Citing our paper

If you make use of our work, please cite our paper:

@InProceedings{Chefer_2021_CVPR,
    author    = {Chefer, Hila and Gur, Shir and Wolf, Lior},
    title     = {Transformer Interpretability Beyond Attention Visualization},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2021},
    pages     = {782-791}
}
Repositorios relacionados
tensorflow/tensorflow

An Open Source Machine Learning Framework for Everyone

C++Apache License 2.0tensorflowmachine-learning
tensorflow.org
196.5k75.7k
AUTOMATIC1111/stable-diffusion-webui

Stable Diffusion web UI

PythonPyPIGNU Affero General Public License v3.0deep-learningdiffusion
164.3k30.4k
huggingface/transformers

🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.

PythonPyPIApache License 2.0nlpnatural-language-processing
huggingface.co/transformers
162.8k34k
pytorch/pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration

PythonPyPIOtherneural-networkautograd
pytorch.org
101.8k28.4k
rasbt/LLMs-from-scratch

Implement a ChatGPT-like LLM in PyTorch from scratch, step by step

Jupyter NotebookOthergptlarge-language-models
amzn.to/4fqvn0D
99.5k15.3k
opencv/opencv

Open Source Computer Vision Library

C++Apache License 2.0opencvc-plus-plus
opencv.org
90.1k56.9k
Developer-Y/cs-video-courses

List of Computer Science courses with video lectures.

computer-sciencealgorithms
82.6k11.4k
d2l-ai/d2l-zh

《动手学深度学习》:面向中文读者、能运行、可讨论。中英文版被70多个国家的500多所大学用于教学。

PythonPyPIApache License 2.0deep-learningbook
zh.d2l.ai
79.1k12.3k
dair-ai/Prompt-Engineering-Guide

🐙 Guides, papers, lessons, notebooks and resources for prompt engineering, context engineering, RAG, and AI Agents.

MDXMIT Licensedeep-learningprompt-engineering
promptingguide.ai
76.8k8.4k
labmlai/annotated_deep_learning_paper_implementations

🧑‍🏫 60+ Implementations/tutorials of deep learning papers with side-by-side notes 📝; including transformers (original, xl, switch, feedback, vit, ...), optimizers (adam, adabelief, sophia, ...), gans(cyclegan, stylegan2, ...), 🎮 reinforcement learning (ppo, dqn), capsnet, distillation, ... 🧠

PythonPyPIMIT Licensedeep-learningdeep-learning-tutorial
nn.labml.ai
67.2k6.7k
keras-team/keras

Deep Learning for humans

PythonPyPIApache License 2.0deep-learningtensorflow
keras.io
64.2k19.7k
CorentinJ/Real-Time-Voice-Cloning

Clone a voice in 5 seconds to generate arbitrary speech in real-time

PythonPyPIOtherdeep-learningpytorch
60k9.4k