vit explain
1.0.0
該存儲庫實現了視覺變壓器中解釋性的方法。
另請參見https://jacobgil.github.io/deeplearning/vision-transformer-ecplainability
注意推出。
特定於類的解釋性的梯度注意力推出。這是我們試圖進一步建立並改善注意力推廣的嘗試。
TBD注意力流正在進行中。
包括一些調整和技巧以使其正常工作:
from vit_grad_rollout import VITAttentionGradRollout
model = torch . hub . load ( 'facebookresearch/deit:main' ,
'deit_tiny_patch16_224' , pretrained = True )
grad_rollout = VITAttentionGradRollout ( model , discard_ratio = 0.9 , head_fusion = 'max' )
mask = grad_rollout ( input_tensor , category_index = 243 ) python vit_explain.py --image_path <image path> --head_fusion <mean, min or max> --discard_ratio <number between 0 and 1> --category_index <category_index>
如果未指定category_index,將使用注意力推廣,否則將使用梯度注意力推出。
請注意,默認情況下,這使用了訓練數據效率圖像變壓器的“微小”模型,並通過託管在Torch Hub上的注意力進行蒸餾。
| 圖像 | 香草注意推廣 | 使用dost_ratio+最大融合 |
|---|---|---|
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
變壓器中流動的注意力沿著屬於不同類別的信息傳遞。漸變推出使我們可以看到網絡的關注位置,但是它沒有告訴我們是否最終使用這些位置進行最終分類。
我們可以將注意力乘以目標類輸出的梯度,並在註意力頭(同時掩蓋負面注意力)中的平均值,以保持對目標類別(或類別)的注意力。




--discard_ratio <value between 0 and 1>
通過保持最強的關注來消除噪音。
不同值的結果:


注意力推廣方法表明,將平均注意力吸引到注意力方面,
但是,皇帝看起來像是採用最低值或最大值與 - discard_ratio相結合,效果更好。
--head_fusion <mean, min or max>
| 圖像 | 平均融合 | 最小融合 |
|---|---|---|
![]() | ![]() | ![]() |
量化變壓器的注意力流動
Timm:Pytorch中的大量模型,尤其是Vision Transformer實施
圖像值得16x16單詞:用於大規模圖像識別的變壓器
https://github.com/jeonsworld/vit-pytorch的榮譽是一個很好的起點。
pip install timm