forked from wzmsltw/BSN-boundary-sensitive-network.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_processing.py
executable file
·176 lines (150 loc) · 5.91 KB
/
post_processing.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
# -*- coding: utf-8 -*-
import json
import multiprocessing as mp
import numpy as np
import os
import pandas as pd
def load_json(file):
with open(file) as json_file:
data = json.load(json_file)
return data
def get_dataset_dict(opt):
df = pd.read_csv(opt["video_info"])
database = load_json(opt["video_anno"])
video_dict = {}
for i in range(len(df)):
video_name = df.video.values[i]
video_info = database[video_name]
video_new_info = {}
video_new_info['duration_frame'] = video_info['duration_frame']
video_new_info['duration_second'] = video_info['duration_second']
video_new_info["feature_frame"] = video_info['feature_frame']
if 'thumos' in opt['dataset']:
video_subset = video_name.split('_')[1].replace('validation', 'train')
else:
video_subset = df.subset.values[i]
print(video_subset, opt['pem_inference_subset'])
video_new_info['annotations'] = video_info['annotations']
if opt["pem_inference_subset"] == 'full' or video_subset == opt["pem_inference_subset"]:
video_dict[video_name] = video_new_info
return video_dict
def iou_with_anchors(anchors_min, anchors_max, len_anchors, box_min, box_max):
"""Compute jaccard score between a box and the anchors.
"""
int_xmin = np.maximum(anchors_min, box_min)
int_xmax = np.minimum(anchors_max, box_max)
inter_len = np.maximum(int_xmax - int_xmin, 0.)
union_len = len_anchors - inter_len + box_max - box_min
#print inter_len,union_len
jaccard = np.divide(inter_len, union_len)
return jaccard
def Soft_NMS(df, opt):
try:
df = df.sort_values(by="score", ascending=False)
except KeyError:
df['score'] = df.xmin_score * df.xmax_score
df = df.sort_values(by="score", ascending=False)
tstart = list(df.xmin.values[:])
tend = list(df.xmax.values[:])
tscore = list(df.score.values[:])
rstart = []
rend = []
rscore = []
while len(tscore) > 0 and len(rscore) <= opt["post_process_top_K"]:
max_index = np.argmax(tscore)
tmp_width = tend[max_index] - tstart[max_index]
iou_list = iou_with_anchors(tstart[max_index], tend[max_index],
tmp_width, np.array(tstart), np.array(tend))
iou_exp_list = np.exp(-np.square(iou_list) / opt["soft_nms_alpha"])
for idx in range(0, len(tscore)):
if idx != max_index:
tmp_iou = iou_list[idx]
if tmp_iou > opt["soft_nms_low_thres"] + (
opt["soft_nms_high_thres"] -
opt["soft_nms_low_thres"]) * tmp_width:
tscore[idx] = tscore[idx] * iou_exp_list[idx]
rstart.append(tstart[max_index])
rend.append(tend[max_index])
rscore.append(tscore[max_index])
tstart.pop(max_index)
tend.pop(max_index)
tscore.pop(max_index)
newDf = pd.DataFrame()
newDf['score'] = rscore
newDf['xmin'] = rstart
newDf['xmax'] = rend
return newDf
def video_post_process(opt, video_list, video_dict):
pem_inference_results = opt['pem_inference_results_dir']
for video_name in video_list:
try:
df = pd.read_csv(os.path.join(pem_inference_results, video_name + ".csv"))
except FileNotFoundError as e:
print("Nothing for this video ... %s" % video_name)
result_dict[video_name[2:]] = []
continue
df['score'] = df.iou_score.values[:] * df.xmin_score.values[:] * df.xmax_score.values[:]
if len(df) > 1:
df = Soft_NMS(df, opt)
df = df.sort_values(by="score", ascending=False)
video_info = video_dict[video_name]
video_duration = float(
video_intfo["duration_frame"] / 16 *
16) / video_info["duration_frame"] * video_info["duration_second"]
proposal_list = []
for j in range(min(opt["post_process_top_K"], len(df))):
tmp_proposal = {}
tmp_proposal["score"] = df.score.values[j]
tmp_proposal["segment"] = [
max(0, df.xmin.values[j]) * video_duration,
min(1, df.xmax.values[j]) * video_duration
]
proposal_list.append(tmp_proposal)
result_dict[video_name[2:]] = proposal_list
def BSN_post_processing(opt):
video_dict = get_dataset_dict(opt)
video_list = sorted(video_dict.keys()) #[:100]
global result_dict
result_dict = mp.Manager().dict()
num_videos = len(video_list)
num_threads = min(num_videos, opt['post_process_thread'])
num_videos_per_thread = int(num_videos / num_threads)
processes = []
for tid in range(num_threads - 1):
tmp_video_list = video_list[tid * num_videos_per_thread:(tid + 1) *
num_videos_per_thread]
p = mp.Process(target=video_post_process,
args=(
opt,
tmp_video_list,
video_dict,
))
p.start()
processes.append(p)
tmp_video_list = video_list[(num_threads - 1) *
num_videos_per_thread:]
p = mp.Process(target=video_post_process,
args=(
opt,
tmp_video_list,
video_dict,
))
p.start()
processes.append(p)
for p in processes:
p.join()
result_dict = dict(result_dict)
output_dict = {
"version": "VERSION 1.3",
"results": result_dict,
"external_data": {}
}
output_dir = opt['postprocessed_results_dir']
if not os.path.exists(output_dir):
os.makedirs(output_dir)
outfile = os.path.join(output_dir, 'result.json')
with open(outfile, 'w') as f:
json.dump(output_dict, f)
#opt = opts.parse_opt()
#opt = vars(opt)
#BSN_post_processing(opt)