-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
170 lines (146 loc) · 7.02 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import argparse
import torch
import torchvision
from torch import nn
from torch.utils.data import Dataset, DataLoader
import os
import time
import copy
from tqdm import tqdm
import pickle
import numpy as np
import pandas as pd
import csv
import json
import warnings
warnings.filterwarnings("ignore")
import sys
sys.path.append(".")
sys.path.append("..")
from path_dict import PathDict
path_dict = PathDict()
proj_root = path_dict.proj_root
from utils.ImageShow import *
from visual_meth.integrated_grad import integrated_grad
from visual_meth.smooth_grad import smooth_grad
from visual_meth.gradients import gradients
from visual_meth.perturbation import video_perturbation
from visual_meth.grad_cam import grad_cam
parser = argparse.ArgumentParser()
parser.add_argument("--videos_dir", type=str, default='')
parser.add_argument("--model", type=str, default='r2plus1d',
choices=['r2plus1d', 'r3d', 'mc3', 'i3d', 'tsn', 'trn', 'tsm'])
parser.add_argument("--pretrain_dataset", type=str, default='kinetics',
choices=['', 'kinetics', 'epic-kitchens-verb', 'epic-kitchens-noun'])
parser.add_argument("--vis_method", type=str, default='integrated_grad',
choices=['grad', 'grad*input', 'integrated_grad', 'smooth_grad', 'grad_cam', 'step', '3d_ep', '2d_ep'])
parser.add_argument("--save_label", type=str, default='')
parser.add_argument("--no_gpu", action='store_true')
parser.add_argument("--num_iter", type=int, default=2000)
parser.add_argument('--perturb_area', type=float, default=0.1,
choices=[0.01, 0.02, 0.05, 0.1, 0.15, 0.2])
parser.add_argument('--polarity', type=str, default='both',
choices=['positive', 'negative', 'both'])
args = parser.parse_args()
if args.no_gpu:
device = torch.device("cpu")
num_devices = 0
else:
device = torch.device("cuda")
num_devices = 1
assert os.path.isdir(args.videos_dir), \
f'Given directory of data does not exist: {args.videos_dir}.'
if args.pretrain_dataset == 'kinetics':
if args.model == 'i3d':
from model_def.i3d import I3D as model
model_ft = model(num_classes=400)
i3d_pt_dir = os.path.join(proj_root, 'model_param/kinetics400_rgb_i3d.pth')
model_ft.load_state_dict(torch.load(i3d_pt_dir))
clip_length = 16
elif args.model == 'tsm':
from model_def.tsm import tsm as model
model_ft = model(400, segment_count=8, pretrained=args.pretrain_dataset)
clip_length = 8
else: # Load pretrained models from PyTorch directly
clip_length = 16
if args.model == 'r2plus1d':
from torchvision.models.video import r2plus1d_18 as model
elif args.model == 'mc3':
from torchvision.models.video import mc3_18 as model
elif args.model == 'r3d':
from torchvision.models.video import r3d_18 as model
else:
raise Exception(f'Given model of {args.model} has no pretrain on {args.pretrain_dataset}.')
model_ft = model(pretrained=True)
model_ft = model_ft.to(device)
model_ft.eval()
# if multi_gpu:
# model_ft = nn.DataParallel(model_ft, device_ids=list(range(num_devices)))
kinetics400_classes = os.path.join(proj_root, 'test_data/kinetics/classes.json')
class_namelist = json.load(open(kinetics400_classes))
elif 'epic-kitchens' in args.pretrain_dataset:
if 'noun' in args.pretrain_dataset:
epic_classes = os.path.join(proj_root, 'test_data/epic-kitchens-noun/EPIC_noun_classes.csv')
elif 'verb' in args.pretrain_dataset:
epic_classes = os.path.join(proj_root, 'test_data/epic-kitchens-verb/EPIC_verb_classes.csv')
else:
raise Exception(f'EPIC-Kitchens only supports two sub-tasks (noun & verb), given {args.pretrain_dataset}.')
class_namelist = [row['class_key'] for ridx, row in pd.read_csv(epic_classes).iterrows()]
class_num = len(class_namelist)
if args.model == 'tsm':
from model_def.tsm import tsm as model
model_ft = model(class_num, segment_count=8, pretrained=args.pretrain_dataset)
clip_length = 8
else:
raise Exception(f'{args.pretrain_dataset} has only pretrained TSM model. Given {args.model}.')
model_ft = model_ft.to(device)
model_ft.eval()
from datasets.universal_dataset import UniversalDataset as dataset
test_dataset = dataset(args.videos_dir, args.model, class_namelist, clip_length=clip_length)
test_dataloader = DataLoader(test_dataset, batch_size=1, shuffle=False)
print(f'Num of test samples:{len(test_dataset)}')
for sample in tqdm(test_dataloader):
inp = sample[0].to(device)
label = sample[1].to(dtype=torch.long)
inp_np = voxel_tensor_to_np(inp[0].detach().cpu()) # 3 x num_f x 224 224
if args.vis_method == 'integrated_grad':
res = integrated_grad(inp, label, model_ft, device, steps=25, polarity=args.polarity)
heatmap_np = res[0].numpy()
elif args.vis_method == 'smooth_grad':
res = smooth_grad(inp, label, model_ft, device, variant='square')
heatmap_np = res[0].numpy()
elif args.vis_method == 'grad':
res = gradients(inp, label, model_ft, device, polarity=args.polarity)
heatmap_np = res[0].numpy()
elif args.vis_method == 'grad*input':
res = gradients(inp, label, model_ft, device, multiply_input=True, polarity=args.polarity)
heatmap_np = res[0].numpy()
elif args.vis_method == 'grad_cam':
if args.model in ['i3d']:
layer_name = ['mixed_5c']
elif args.model in ['r2plus1d', 'mc3', 'r3d']: # Load pretrained models from PyTorch directly
layer_name = ['layer4']
# elif args.model in ['tsm', 'tsn']:
# layer_name = ['model', 'base_model', 'layer4']
else:
raise Exception(f'Grad-CAM does not support {args.model} currently')
res = grad_cam(inp, label, model_ft, device, layer_name=layer_name, norm_vis=True)
heatmap_np = overlap_maps_on_voxel_np(inp_np, res[0,0].cpu().numpy(), norm_map=False)
elif 'ep' in args.vis_method:
sigma = 11 if inp.shape[-1] == 112 else 23
res = video_perturbation(
model_ft, inp, label, method=args.vis_method, areas=[args.perturb_area],
sigma=sigma, max_iter=args.num_iter, variant="preserve",
num_devices=num_devices, print_iter=200, perturb_type="blur")[0]
heatmap_np = overlap_maps_on_voxel_np(inp_np, res[0,0].cpu().numpy(), norm_map=False)
sample_name = sample[2][0].split("/")[-1]
plot_save_name = f"{sample_name}.png"
if args.vis_method in ['grad', 'grad*input', 'integrated_grad']:
plot_save_name = plot_save_name.replace('.png', f'_{args.polarity}.png')
plot_save_dir = os.path.join(proj_root, "visual_res", args.vis_method, args.model)
if args.save_label != '':
plot_save_dir = os.path.join(plot_save_dir, args.save_label)
os.makedirs(plot_save_dir, exist_ok=True)
show_txt = f"{sample_name}"
plot_voxel_np(inp_np, heatmap_np, title=show_txt,
save_path=os.path.join(plot_save_dir, plot_save_name) )