-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
executable file
·221 lines (166 loc) · 6.17 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
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
import os
import json
import random
import torch
import math
import numpy as np
from scipy import stats
from matplotlib import pyplot as plt
from scipy.ndimage import generic_filter
def load_config_file(config_file):
all_params = json.load(open(config_file))
if 'result_dir' not in all_params:
all_params['result_dir'] = 'result'
if 'log_train_results' not in all_params:
all_params['log_train_results'] = True
if 'soft_label' not in all_params:
all_params['soft_label'] = None
if 'postprocess' not in all_params:
all_params['postprocess'] = {
'type': None,
'value': None
}
if 'use_instance_norm' not in all_params['encoder_params']:
all_params['encoder_params']['use_instance_norm'] = False
if 'detach_decoder' not in all_params['diffusion_params']:
all_params['diffusion_params']['detach_decoder'] = False
assert all_params['loss_weights']['encoder_boundary_loss'] == 0
return all_params
def set_random_seed(seed):
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def mode_filter(x, size):
def modal(P):
mode = stats.mode(P)
return mode.mode[0]
result = generic_filter(x, modal, size)
return result
############# Modified from ASFormer/MSTCN #################
def read_file(path):
with open(path, 'r') as f:
content = f.read()
f.close()
return content
def get_labels_start_end_time(frame_wise_labels, bg_class=["background"]):
labels = []
starts = []
ends = []
last_label = frame_wise_labels[0]
if frame_wise_labels[0] not in bg_class:
labels.append(frame_wise_labels[0])
starts.append(0)
for i in range(len(frame_wise_labels)):
if frame_wise_labels[i] != last_label:
if frame_wise_labels[i] not in bg_class:
labels.append(frame_wise_labels[i])
starts.append(i)
if last_label not in bg_class:
ends.append(i)
last_label = frame_wise_labels[i]
if last_label not in bg_class:
ends.append(i)
return labels, starts, ends
def levenstein(p, y, norm=False):
m_row = len(p)
n_col = len(y)
D = np.zeros([m_row+1, n_col+1], np.float)
for i in range(m_row+1):
D[i, 0] = i
for i in range(n_col+1):
D[0, i] = i
for j in range(1, n_col+1):
for i in range(1, m_row+1):
if y[j-1] == p[i-1]:
D[i, j] = D[i-1, j-1]
else:
D[i, j] = min(D[i-1, j] + 1,
D[i, j-1] + 1,
D[i-1, j-1] + 1)
if norm:
score = (1 - D[-1, -1]/max(m_row, n_col)) * 100
else:
score = D[-1, -1]
return score
def edit_score(recognized, ground_truth, norm=True, bg_class=["background"]):
P, _, _ = get_labels_start_end_time(recognized, bg_class)
Y, _, _ = get_labels_start_end_time(ground_truth, bg_class)
return levenstein(P, Y, norm)
def f_score(recognized, ground_truth, overlap, bg_class=["background"]):
p_label, p_start, p_end = get_labels_start_end_time(recognized, bg_class)
y_label, y_start, y_end = get_labels_start_end_time(ground_truth, bg_class)
tp = 0
fp = 0
hits = np.zeros(len(y_label))
for j in range(len(p_label)):
intersection = np.minimum(p_end[j], y_end) - np.maximum(p_start[j], y_start)
union = np.maximum(p_end[j], y_end) - np.minimum(p_start[j], y_start)
IoU = (1.0*intersection / union)*([p_label[j] == y_label[x] for x in range(len(y_label))])
# Get the best scoring segment
idx = np.array(IoU).argmax()
if IoU[idx] >= overlap and not hits[idx]:
tp += 1
hits[idx] = 1
else:
fp += 1
fn = len(y_label) - sum(hits)
return float(tp), float(fp), float(fn)
def func_eval(label_dir, pred_dir, video_list):
overlap = [.1, .25, .5]
tp, fp, fn = np.zeros(3), np.zeros(3), np.zeros(3)
correct = 0
total = 0
edit = 0
for vid in video_list:
gt_file = os.path.join(label_dir, f'{vid}.txt')
gt_content = read_file(gt_file).split('\n')[0:-1]
pred_file = os.path.join(pred_dir, f'{vid}.txt')
pred_content = read_file(pred_file).split('\n')[1].split()
assert(len(gt_content) == len(pred_content))
for i in range(len(gt_content)):
total += 1
if gt_content[i] == pred_content[i]:
correct += 1
edit += edit_score(pred_content, gt_content)
for s in range(len(overlap)):
tp1, fp1, fn1 = f_score(pred_content, gt_content, overlap[s])
tp[s] += tp1
fp[s] += fp1
fn[s] += fn1
acc = 100 * float(correct) / total
edit = (1.0 * edit) / len(video_list)
f1s = np.array([0, 0 ,0], dtype=float)
for s in range(len(overlap)):
precision = tp[s] / float(tp[s] + fp[s])
recall = tp[s] / float(tp[s] + fn[s])
f1 = 2.0 * (precision * recall) / (precision + recall)
f1 = np.nan_to_num(f1) * 100
f1s[s] = f1
return acc, edit, f1s
############# Visualization #################
def plot_barcode(class_num, gt=None, pred=None, show=True, save_file=None):
if class_num <= 10:
color_map = plt.cm.tab10
elif class_num > 20:
color_map = plt.cm.gist_ncar
else:
color_map = plt.cm.tab20
axprops = dict(xticks=[], yticks=[], frameon=False)
barprops = dict(aspect='auto', cmap=color_map,
interpolation='nearest', vmin=0, vmax=class_num-1)
fig = plt.figure(figsize=(18, 4))
# a horizontal barcode
if gt is not None:
ax1 = fig.add_axes([0, 0.45, 1, 0.2], **axprops)
ax1.set_title('Ground Truth')
ax1.imshow(gt.reshape((1, -1)), **barprops)
if pred is not None:
ax2 = fig.add_axes([0, 0.15, 1, 0.2], **axprops)
ax2.set_title('Predicted')
ax2.imshow(pred.reshape((1, -1)), **barprops)
if save_file is not None:
fig.savefig(save_file, dpi=400)
if show:
plt.show()
plt.close(fig)