mmaction2 icon indicating copy to clipboard operation
mmaction2 copied to clipboard

Grad-CAM target layer name for timeSformer model

Open YNawal opened this issue 4 years ago • 3 comments

Hello everybody Please what is the appropriate target layer name to visualise the grad cam of a video Thanks for your help

YNawal avatar Jul 27 '21 16:07 YNawal

I am not so sure what you mean by "appropriate"? I guess, what you can do is, look into all the layers of a model and pick one to visualise. If you want the end result, pick one of the final layers.

Small script to get all the layers of a model:

import argparse
from mmaction.apis import init_recognizer


def parse_args():
    parser = argparse.ArgumentParser(prog='model layer printer')
    parser.add_argument('config', help='config file path')
    parser.add_argument('checkpoint', help='checkpoint file')
    parser.add_argument(
        '--device', type=str, default='cuda:0', help='CPU/CUDA device option')
    args = parser.parse_args()
    return args


def print_layers(model, layer_name):
    if len(model._modules) == 0:
        print(layer_name)
    else:
        for key in model._modules:
            name = key if len(layer_name) == 0 else layer_name + '/' + key
            print_layers(model._modules[key], name)


def main():
    args = parse_args()
    model = init_recognizer(args.config, args.checkpoint, device=args.device)
    print_layers(model, '')


if __name__ == '__main__':
    main()

get_flops.py does sth similar.

Ref. #903

rlleshi avatar Aug 12 '21 10:08 rlleshi

@rlleshi Thnaks for your reply My problem now is how we can plot the self attentions as in vision transformer or grad cam in other models.

YNawal avatar Aug 12 '21 14:08 YNawal

Hi @YNawal can you do this? if yes, can you give me a little advice? thank you.

JoseMiguelCh avatar May 25 '22 01:05 JoseMiguelCh