/
attention_weights.py
260 lines (231 loc) · 12.6 KB
/
attention_weights.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os
import pickle
import statistics
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.font_manager
import matplotlib.pyplot as plt
nice_fonts = {
"text.usetex": False,
"font.family": "serif",
"font.serif" : "Arial",
}
matplotlib.rcParams.update(nice_fonts)
plt.rcParams['font.size'] = 30
plt.rcParams['axes.linewidth'] = 2
from sklearn.metrics import f1_score
import glob
import torch
from torch.utils.data import DataLoader
from models.track_mpnn import TrackMPNN
from models.loss import create_targets
from dataset.kitti_mot import KittiMOTDataset, store_kitti_results
from utils.graph import initialize_graph, update_graph, prune_graph, decode_tracks
from utils.metrics import create_mot_accumulator, calc_mot_metrics, compute_map
from utils.infer_options import args
kwargs_train = {'batch_size': 1, 'shuffle': True}
kwargs_val = {'batch_size': 1, 'shuffle': False}
if 'vis' in args.feats:
vis_snapshot = os.path.join(os.path.dirname(args.snapshot), 'vis-net_' + args.snapshot[-8:])
train_loader = DataLoader(KittiMOTDataset(args.dataset_root_path, 'train', args.category, args.detections, args.feats,
args.embed_arch, args.cur_win_size, args.ret_win_size, vis_snapshot, False, args.cuda), **kwargs_train)
val_loader = DataLoader(KittiMOTDataset(args.dataset_root_path, 'val', args.category, args.detections, args.feats,
args.embed_arch, args.cur_win_size, args.ret_win_size, vis_snapshot, False, args.cuda), **kwargs_val)
val_loader.dataset.embed_net = train_loader.dataset.embed_net # use trained embedding net for the val loader
train_loader.dataset.embed_net = None # set trained embedding net to None to save memory
else:
val_loader = DataLoader(KittiMOTDataset(args.dataset_root_path, 'val', args.category, args.detections, args.feats,
args.embed_arch, args.cur_win_size, args.ret_win_size, None, False, args.cuda), **kwargs_val)
# create file handles
f_log = open(os.path.join(args.output_dir, "logs.txt"), "w")
# random seed function (https://docs.fast.ai/dev/test.html#getting-reproducible-results)
def random_seed(seed_value, use_cuda):
torch.manual_seed(seed_value)
if use_cuda:
torch.backends.cudnn.deterministic = True #needed
def store_att_weights(folder, sequence_index, data):
# labels, y_pred
labels = data[0]
y_pred = data[1]
# if using multiple sets of features, choose which attention weights to save
feature_set = 0
if len(data) == 3:
attention = [att.cpu().detach().numpy() for att in data[2][feature_set]]
dict_to_pickle = {'labels' : labels.cpu().numpy(), 'y_pred' : y_pred.cpu().numpy(),
'attention' : attention}
else:
dict_to_pickle = {'labels' : labels.cpu().numpy(), 'y_pred' : y_pred.cpu().numpy()}
path = os.path.join(folder, f"{sequence_index}.p")
with open(path, "wb" ) as f:
pickle.dump( dict_to_pickle, f)
def plot_att_distribution():
results = [{'tp' : [], 'fp' : []} for i in range(args.num_att_heads)]
filelist = glob.glob(os.path.join(args.output_dir, '*.p'))
for count, file in enumerate(filelist):
with open(file, 'rb') as f:
data = pickle.load(f)
labels = data['labels']
y_pred = data['y_pred']
attention = data['attention']
N = attention[0].shape[0]
for row_index in range(N):
if y_pred[row_index][0] != -1:
for col_index in range(N):
for i, attention_i in enumerate(attention):
if attention_i[row_index][col_index] > 0 and y_pred[col_index][0] == -1:
if labels[col_index] == 1:
results[i]['tp'].append(attention_i[row_index][col_index])
else:
results[i]['fp'].append(attention_i[row_index][col_index])
print("Completed processing file %d/%d..." % (count, len(filelist)))
fig, ax = plt.subplots(args.num_att_heads, 2, sharex=True, figsize=(4.6*len(results), 5.2*args.num_att_heads))
num_bins = 25
for i in range(args.num_att_heads):
ax[i, 0].hist(results[i]['tp'], num_bins, color='gray', range=(0.0, 1.0),
density=False, stacked=True, edgecolor='black', linewidth=1.2,
weights=np.ones_like(results[i]['tp'])/float(len(results[i]['tp'])))
ax[i, 0].grid(True)
ax[i, 1].hist(results[i]['fp'], num_bins, color='gray', range=(0.0, 1.0),
density=False, stacked=True, edgecolor='black', linewidth=1.2,
weights=np.ones_like(results[i]['fp'])/float(len(results[i]['fp'])))
ax[i, 1].grid(True)
ax[i, 0].set_ylabel('Normalized count for \nattention head #%d' % (i, ))
ax[args.num_att_heads-1, 0].set_xlabel('Attention weights for\ncorrect associations')
ax[args.num_att_heads-1, 1].set_xlabel('Attention weights for\nincorrect associations')
fig.savefig(os.path.join(args.output_dir, 'att_dist.png'), transparent=False, bbox_inches='tight')
plt.close('all')
def val(model):
epoch_f1 = list()
accs = []
model.eval() # set TrackMPNN model to eval mode
if 'vis' in args.feats:
val_loader.dataset.embed_net.eval()
bbox_pred_dict, bbox_gt_dict = {}, {} # initialize dictionaries for computing mAP
_att_ind = 0
for b_idx, (X_seq, bbox_pred, bbox_gt, _) in enumerate(val_loader):
# if no detections in sequence
if X_seq.size()[1] == 0 or bbox_gt.shape[1] == 0:
print('No detections available for sequence...')
continue
y_seq = bbox_pred[:, :, :2]
# initaialize output array tracks to -1s
y_out = y_seq.squeeze(0).detach().cpu().numpy().astype('int64')
y_out[:, 1] = -1
# intialize graph and run first forward pass
y_pred, feats, node_adj, edge_adj, labels, t_st, t_end = initialize_graph(X_seq, y_seq, t_st=0, mode='test', cuda=args.cuda)
if y_pred is None:
continue
# compute the classification scores
scores, logits, states, _ = model(feats, None, node_adj, edge_adj)
scores = torch.cat((1-scores, scores), dim=1)
idx_edge = torch.nonzero((y_pred[:, 0] == -1))[:, 0]
idx_node = torch.nonzero((y_pred[:, 0] != -1))[:, 0]
# calculate targets for computing metrics
targets = create_targets(labels, node_adj, idx_node)
if args.tp_classifier:
idx = torch.cat((idx_node, idx_edge))
else:
scores[idx_node, 0] = 0
scores[idx_node, 1] = 1
idx = idx_edge
# compute the f1 score
pred = scores.data.max(1)[1] # get the index of the max log-probability
epoch_f1.append(f1_score(targets[idx].detach().cpu().numpy(), pred[idx].detach().cpu().numpy(), zero_division=0))
# loop through all frames
t_skip = t_st
for t_cur in range(t_st, t_end):
if t_cur < t_skip: # if timestep has already been processed
continue
# if no new detections found and no carried over detections
if feats.size()[0] == 0 and states.size()[0] == 0:
# reinitialize graph
y_pred, feats, node_adj, edge_adj, labels, t_skip, _ = initialize_graph(X_seq, y_seq, t_st=t_cur, mode='test', cuda=args.cuda)
if y_pred is None:
break
states = None
else:
# update graph for next timestep
y_pred, feats, node_adj, edge_adj, labels = update_graph(node_adj, labels, scores, y_pred, X_seq, y_seq, t_cur,
use_hungraian=args.hungarian, mode='test', cuda=args.cuda)
# run forward pass
scores, logits, states, attention = model(feats, states, node_adj, edge_adj)
store_att_weights(args.output_dir, _att_ind, [labels, y_pred, attention])
_att_ind += 1
scores = torch.cat((1-scores, scores), dim=1)
idx_edge = torch.nonzero((y_pred[:, 0] == -1))[:, 0]
idx_node = torch.nonzero((y_pred[:, 0] != -1))[:, 0]
# calculate targets for computing metrics
targets = create_targets(labels, node_adj, idx_node)
if args.tp_classifier:
idx = torch.cat((idx_node, idx_edge))
else:
scores[idx_node, 0] = 0
scores[idx_node, 1] = 1
idx = idx_edge
# compute the f1 score
pred = scores.data.max(1)[1] # get the index of the max log-probability
epoch_f1.append(f1_score(targets[idx].detach().cpu().numpy(), pred[idx].detach().cpu().numpy(), zero_division=0))
if t_cur == t_end - 1:
y_pred, y_out, states, node_adj, labels, scores = decode_tracks(states, node_adj, labels, scores, y_pred, y_out, t_end,
args.ret_win_size, use_hungraian=args.hungarian, cuda=args.cuda)
else:
y_pred, y_out, states, node_adj, labels, scores = decode_tracks(states, node_adj, labels, scores, y_pred, y_out,
t_cur - args.cur_win_size + 2, args.ret_win_size, use_hungraian=args.hungarian, cuda=args.cuda)
print("Sequence {}, generated tracks upto t = {}/{}...".format(b_idx + 1, max(0, t_cur - args.cur_win_size + 1), t_end))
print("Sequence {}, generated tracks upto t = {}/{}...".format(b_idx + 1, t_end, t_end))
# create results accumulator using predictions and GT for evaluation
bbox_pred = bbox_pred[0, :, 2:].detach().cpu().numpy().astype('float32')
y_gt = bbox_gt[0, :, :2].detach().cpu().numpy().astype('int64')
bbox_gt = bbox_gt[0, :, 2:].detach().cpu().numpy().astype('float32')
acc = create_mot_accumulator(bbox_pred, bbox_gt, y_out, y_gt)
if acc is not None:
accs.append(acc)
# store values for computing mAP
bbox_pred_dict[str(b_idx)] = (y_out[y_out[:, 1] >= 0, :], bbox_pred[y_out[:, 1] >= 0, :])
bbox_gt_dict[str(b_idx)] = (y_gt, bbox_gt)
print('Done with sequence {} of {}...'.format(b_idx + 1, len(val_loader.dataset)))
# Calculate F1-score
val_f1 = statistics.mean(epoch_f1)
# Calculate MOTA
if len(accs) > 0:
val_motas = [100.0 * calc_mot_metrics([_])['mota'] for _ in accs]
val_metrics = calc_mot_metrics(accs)
else:
mota = -1
# Calculate mAP
val_map = 100.0 * compute_map(bbox_pred_dict, bbox_gt_dict)
print("------------------------\nValidation F1 score = {:.4f}".format(val_f1))
for seq_num, _ in enumerate(val_motas):
print("Validation MOTA for sequence {:d} = {:.2f}%".format(seq_num, val_motas[seq_num]))
print("Validation MOTA = {:.2f}".format(100.*val_metrics['mota']))
print("Validation MOTP = {:.4f}".format(val_metrics['motp']))
print("Validation MT = {:.2f}%".format(100.*val_metrics['mostly_tracked']/val_metrics['num_unique_objects']))
print("Validation ML = {:.2f}%".format(100.*val_metrics['mostly_lost']/val_metrics['num_unique_objects']))
print("Validation IDS = {:d}".format(val_metrics['num_switches']))
print("Validation FRAG = {:d}".format(val_metrics['num_fragmentations']))
print("Validation mAP = {:.2f}\n------------------------\n".format(val_map))
f_log.write("------------------------\nValidation F1 score = {:.4f}\n".format(val_f1))
for seq_num, _ in enumerate(val_motas):
f_log.write("Validation MOTA for sequence {:d} = {:.2f}%\n".format(seq_num, val_motas[seq_num]))
f_log.write("Validation MOTA = {:.2f}\n".format(100.*val_metrics['mota']))
f_log.write("Validation MOTP = {:.4f}\n".format(val_metrics['motp']))
f_log.write("Validation MT = {:.2f}%\n".format(100.*val_metrics['mostly_tracked']/val_metrics['num_unique_objects']))
f_log.write("Validation ML = {:.2f}%\n".format(100.*val_metrics['mostly_lost']/val_metrics['num_unique_objects']))
f_log.write("Validation IDS = {:d}\n".format(val_metrics['num_switches']))
f_log.write("Validation FRAG = {:d}\n".format(val_metrics['num_fragmentations']))
f_log.write("Validation mAP = {:.2f}\n------------------------\n\n".format(val_map))
return
if __name__ == '__main__':
# for reproducibility
random_seed(args.seed, args.cuda)
# get the model, load pretrained weights, and convert it into cuda for if necessary
model = TrackMPNN(features=args.feats, ncategories=len(val_loader.dataset.class_dict),
nhidden=args.num_hidden_feats, nattheads=args.num_att_heads, msg_type=args.msg_type)
if args.snapshot is not None:
model.load_state_dict(torch.load(args.snapshot), strict=True)
if args.cuda:
model.cuda()
print(model)
val(model)
plot_att_distribution()