|
| 1 | +import fnmatch |
| 2 | +from collections import OrderedDict |
| 3 | +from typing import Union, Optional, List |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | + |
| 8 | +class AttentionExtract(torch.nn.Module): |
| 9 | + # defaults should cover a significant number of timm models with attention maps. |
| 10 | + default_node_names = ['*attn.softmax'] |
| 11 | + default_module_names = ['*attn_drop'] |
| 12 | + |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + model: Union[torch.nn.Module], |
| 16 | + names: Optional[List[str]] = None, |
| 17 | + mode: str = 'eval', |
| 18 | + method: str = 'fx', |
| 19 | + hook_type: str = 'forward', |
| 20 | + ): |
| 21 | + """ Extract attention maps (or other activations) from a model by name. |
| 22 | +
|
| 23 | + Args: |
| 24 | + model: Instantiated model to extract from. |
| 25 | + names: List of concrete or wildcard names to extract. Names are nodes for fx and modules for hooks. |
| 26 | + mode: 'train' or 'eval' model mode. |
| 27 | + method: 'fx' or 'hook' extraction method. |
| 28 | + hook_type: 'forward' or 'forward_pre' hooks used. |
| 29 | + """ |
| 30 | + super().__init__() |
| 31 | + assert mode in ('train', 'eval') |
| 32 | + if mode == 'train': |
| 33 | + model = model.train() |
| 34 | + else: |
| 35 | + model = model.eval() |
| 36 | + |
| 37 | + assert method in ('fx', 'hook') |
| 38 | + if method == 'fx': |
| 39 | + # names are activation node names |
| 40 | + from timm.models._features_fx import get_graph_node_names, GraphExtractNet |
| 41 | + |
| 42 | + node_names = get_graph_node_names(model)[0 if mode == 'train' else 1] |
| 43 | + matched = [] |
| 44 | + names = names or self.default_node_names |
| 45 | + for n in names: |
| 46 | + matched.extend(fnmatch.filter(node_names, n)) |
| 47 | + if not matched: |
| 48 | + raise RuntimeError(f'No node names found matching {names}.') |
| 49 | + |
| 50 | + self.model = GraphExtractNet(model, matched) |
| 51 | + self.hooks = None |
| 52 | + else: |
| 53 | + # names are module names |
| 54 | + assert hook_type in ('forward', 'forward_pre') |
| 55 | + from timm.models._features import FeatureHooks |
| 56 | + |
| 57 | + module_names = [n for n, m in model.named_modules()] |
| 58 | + matched = [] |
| 59 | + names = names or self.default_module_names |
| 60 | + for n in names: |
| 61 | + matched.extend(fnmatch.filter(module_names, n)) |
| 62 | + if not matched: |
| 63 | + raise RuntimeError(f'No module names found matching {names}.') |
| 64 | + |
| 65 | + self.model = model |
| 66 | + self.hooks = FeatureHooks(matched, model.named_modules(), default_hook_type=hook_type) |
| 67 | + |
| 68 | + self.names = matched |
| 69 | + self.mode = mode |
| 70 | + self.method = method |
| 71 | + |
| 72 | + def forward(self, x): |
| 73 | + if self.hooks is not None: |
| 74 | + self.model(x) |
| 75 | + output = self.hooks.get_output(device=x.device) |
| 76 | + else: |
| 77 | + output = self.model(x) |
| 78 | + output = OrderedDict(zip(self.names, output)) |
| 79 | + return output |
0 commit comments