-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·126 lines (98 loc) · 3.94 KB
/
utils.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
import torch
import torch.nn as nn
import numpy as np
from scipy.interpolate import interp1d
import os
import sys
import random
import config
from matplotlib import pyplot as plt
def upgrade_resolution(arr, scale):
x = np.arange(0, arr.shape[0])
f = interp1d(x, arr, kind='linear', axis=0, fill_value='extrapolate')
scale_x = np.arange(0, arr.shape[0], 1 / scale)
up_scale = f(scale_x)
return up_scale
def get_proposal_oic(tList, wtcam, final_score, c_pred, scale, v_len, sampling_frames, num_segments, lambda_=0.25, gamma=0.2):
t_factor = (16 * v_len) / (scale * num_segments * sampling_frames)
temp = []
for i in range(len(tList)):
c_temp = []
temp_list = np.array(tList[i])[0]
if temp_list.any():
grouped_temp_list = grouping(temp_list)
for j in range(len(grouped_temp_list)):
inner_score = np.mean(wtcam[grouped_temp_list[j], i, 0])
len_proposal = len(grouped_temp_list[j])
outer_s = max(0, int(grouped_temp_list[j][0] - lambda_ * len_proposal))
outer_e = min(int(wtcam.shape[0] - 1), int(grouped_temp_list[j][-1] + lambda_ * len_proposal))
outer_temp_list = list(range(outer_s, int(grouped_temp_list[j][0]))) + list(range(int(grouped_temp_list[j][-1] + 1), outer_e + 1))
if len(outer_temp_list) == 0:
outer_score = 0
else:
outer_score = np.mean(wtcam[outer_temp_list, i, 0])
c_score = inner_score - outer_score + gamma * final_score[c_pred[i]]
t_start = grouped_temp_list[j][0] * t_factor
t_end = (grouped_temp_list[j][-1] + 1) * t_factor
c_temp.append([c_pred[i], c_score, t_start, t_end])
temp.append(c_temp)
return temp
def result2json(result):
result_file = []
for i in range(len(result)):
for j in range(len(result[i])):
line = {'label': config.class_dict[result[i][j][0]], 'score': result[i][j][1],
'segment': [result[i][j][2], result[i][j][3]]}
result_file.append(line)
return result_file
def grouping(arr):
return np.split(arr, np.where(np.diff(arr) != 1)[0] + 1)
def save_best_record_ucf(test_info, file_path):
fo = open(file_path, "w")
fo.write("Step: {}\n".format(test_info["step"][-1]))
fo.write("accuracy: {:.4f}\n".format(test_info["accuracy"][-1]))
fo.write("auc: {:.4f}\n".format(test_info["auc"][-1]))
fo.close()
def minmax_norm(act_map):
max_val = nn.ReLU()(torch.max(act_map, dim=1)[0])
min_val = nn.ReLU()(torch.min(act_map, dim=1)[0])
delta = max_val - min_val
delta[delta <=0] = 1
ret = (act_map - min_val) / delta
return ret
def nms(proposals, thresh):
proposals = np.array(proposals)
x1 = proposals[:, 2]
x2 = proposals[:, 3]
scores = proposals[:, 1]
areas = x2 - x1 + 1
order = scores.argsort()[::-1]
keep = []
while order.size > 0:
i = order[0]
keep.append(proposals[i].tolist())
xx1 = np.maximum(x1[i], x1[order[1:]])
xx2 = np.minimum(x2[i], x2[order[1:]])
inter = np.maximum(0.0, xx2 - xx1 + 1)
iou = inter / (areas[i] + areas[order[1:]] - inter)
inds = np.where(iou < thresh)[0]
order = order[inds + 1]
return keep
def set_seed(seed):
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
torch.backends.cudnn.deterministic=True
torch.backends.cudnn.benchmark=False
def plot_conf(cm,labels,title):
cm=cm.astype('float')/cm.sum(axis=1)[:,np.newaxis]
cmap=plt.cm.get_cmap("Reds")
plt.imshow(cm,cmap=cmap,interpolation='nearest')
plt.title(title)
plt.colorbar()
num_local=np.array(range(len(labels)))
plt.xticks(num_local,labels,rotation=90)
plt.yticks(num_local,labels)
plt.ylabel("True label")
plt.xlabel("Predicted label")