-
Notifications
You must be signed in to change notification settings - Fork 2
/
postprocess
executable file
·152 lines (120 loc) · 6.11 KB
/
postprocess
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys, argparse, fileinput, collections, sys
from math import ceil
def select_first_most_common(l):
l = list(l)
c = collections.Counter(l)
m = max(c.values())
c = [k for k,v in c.most_common() if v==m]
c = [k for k in l if k in c]
return c[0]
def major_print_window(window, args):
pred = collections.Counter(l2 for (l1,l2) in window).most_common(1)[0][0]
pred = select_first_most_common(l2 for (l1,l2) in window)
uniq = collections.OrderedDict((l1,None) for (l1,l2) in window)
args.strategy(uniq, pred, args.no_warn)
def confidence_print_window(window, args):
g = collections.OrderedDict([(g,None) for g,l,c in window])
t,l,_ = max(window, key=lambda c: c[2])
args.strategy(g, l, args.no_warn)
def score_print_window(window, args):
g = collections.OrderedDict([(g,None) for g,l,c in window])
d = { key: 0 for (_,key,_) in window }
for _,key,c in window: d[key] += float(c)
l = max(d.items(), key=lambda i: i[1])[0]
args.strategy(g, l, args.no_warn)
def change_print_window(window, args):
if not hasattr(args, "lastline"):
args.lastline = window[0]
return
if window[0] != args.lastline:
print("\t".join(args.lastline))
args.lastline = window[0]
def hierarchical_lazy_window(window, args):
sys.stderr.write("hierarchical_lazy_window not implemented\n")
sys.exit(-1)
def main_loop(print_window, args):
# sanity check for supplied arguments
if args.overlap < 0 or args.overlap >=1:
sys.stderr.write("-O|--overlap must be in the range [0,1)\n")
sys.exit(-1)
if args.window <= 0:
sys.stderr.write("-W|--window must be larger than 0\n")
sys.exit(-1)
window = []
for line in fileinput.input(args.files):
if line.strip()=="" or line.strip()[0] == "#":
continue
window.append(line.split())
if len(window) == args.window:
print_window(window,args)
window = window[int(ceil(len(window)*(1-args.overlap))):]
if len(window) > 0:
try: print_window(window,args,True)
except TypeError: print_window(window,args)
def overlap_major(groundtruth, prediction, no_warn=False):
gtl = select_first_most_common(groundtruth)
if len(groundtruth) and not no_warn:
sys.stderr.write("WARNING: multiple groundtruth labels per frame, selecting most commone one: %s\n" % gtl)
print( gtl, prediction )
def overlap_duplicate(groundtruth, prediction, no_warn=False):
if len(groundtruth) and not no_warn:
sys.stderr.write("WARNING: multiple groundtruth labels per frame, duplicating!\n")
for gt in groundtruth:
print( gt, prediction )
if __name__=="__main__":
overlap_strategies = {
'major' : overlap_major,
'duplicate' : overlap_duplicate
}
cmds = {
'majority' : major_print_window,
'confidence' : confidence_print_window,
'score' : score_print_window,
'change' : change_print_window,
'hls' : hierarchical_lazy_window,
}
cmdline = argparse.ArgumentParser('postprocess label outputs')
subpars = cmdline.add_subparsers(help="type of post-processing",dest="command")
cmdline.add_argument("-w","--no-warn", action="store_true", help="suppress warnings")
cmdline.add_argument("-s","--strategy", choices=list(overlap_strategies.keys()),
help="the strategy to resolve multiple groundtruth labels", default="major")
major = subpars.add_parser("majority", help="returns only the label with maximum occurence in the given window" )
major.add_argument("-W","--window", type=int, default=10, help="window size in frames to consider")
major.add_argument("-O","--overlap", type=float, default=0, help="window overlap")
major.add_argument('files', metavar='FILES', type=str, nargs='*', help="input files or - for stdin")
change = subpars.add_parser("change", help="return only changed labels")
change.add_argument('files', metavar='FILES', type=str, nargs='*', help="input files or - for stdin")
mle = subpars.add_parser("confidence", help="maximum likelihood of estimate")
mle.add_argument("-W","--window", type=int, default=10, help="window size in frames to consider")
mle.add_argument("-O","--overlap", type=float, default=0, help="window overlap")
mle.add_argument('files', metavar='FILES', type=str, nargs='*', help="input files or - for stdin")
mlc = subpars.add_parser("score", help="maximum likelihood of class")
mlc.add_argument("-W","--window", type=int, default=10, help="window size in frames to consider")
mlc.add_argument("-O","--overlap", type=float, default=0, help="window overlap")
mlc.add_argument('files', metavar='FILES', type=str, nargs='*', help="input files or - for stdin")
evt = subpars.add_parser("sum", help="threshold on cumulative sum of probabilities")
evt.add_argument("-t", "--threshold", type=float, default=5,
help="threhold that needs to be reached prior to generating output")
evt.add_argument("-r", "--reset", type=float, default=-10,
help="value to reset the class probability after generating output")
evt.add_argument('files', metavar='FILES', type=str, nargs='*',
help="input files or - for stdin")
evt.add_argument("-d", "--debug", nargs='*',
help="create a file for tracing the postprocessing execution", type=str)
hls = subpars.add_parser("hls", help="hierachical lazy detector")
hls.add_argument("-W","--window", type=int, default=10, help="window size in frames to consider")
hls.add_argument('files', metavar='FILES', type=str, nargs='*', help="input files or - for stdin")
args = cmdline.parse_args()
args.strategy = overlap_strategies[args.strategy]
if "change" == args.command:
args.window, args.overlap = 1, 0
try:
main_loop(cmds[args.command], args)
except ValueError as e:
msg = e.args[0]
if not "not enough values" in msg:
raise(e)
sys.stderr.write("ERROR: classfification confidence missing (run predict with -l)\n")
sys.exit(-1)